How can I force Kendo Grid to use a numeric filter

2019-05-14 16:14发布

问题:

Using the following code, Kendo Grid uses the string filter interface for t.Files.Count even though the type is an int. How can I force the grid to use the numeric filter UI instead?

@(Html.Kendo().Grid<GJW_Site.Web.Models.TargetsModel>()    
    .Name("grid")
    .Columns(columns => {
        columns.Bound(t => t.ID).Width(80);
        columns.Bound(t => t.OrbitalPeriod);
        columns.Bound(t => t.Files.Count);
    })
    .Sortable()
    .Filterable()
    .DataSource(dataSource => dataSource.Ajax()
        .PageSize(20)
        .Read(read => read.Action("Targets_Read", "Targets"))
    )
    .Resizable(o => o.Columns(true))
    .ColumnMenu()
)

Produces a filter menu for strings:

I am using Kendo.MVC 2013.1.514.340

回答1:

The solution is to specify that the value is an int in the model - change the DataSource method as so:

.DataSource(dataSource => dataSource.Ajax()
    .PageSize(20)
    .Read(read => read.Action("Targets_Read", "Targets"))
    .Model(m => {
        m.Field<int>(t => t.Files.Count);
    })
)


回答2:

You can also use this solution:

.DataSource(dataSource => dataSource.Ajax()
    .PageSize(20)
    .Read(read => read.Action("Targets_Read", "Targets"))
    .Model(m => {
        m.Field("Files.Count", typeof(System.Int32));
    })
)


回答3:

I have faced similar issue when the filter are coming for data type string while my data type is "number", this happens if there is a discrepancy in declaring the schema, make sure the field declared in schema are correct i.e. have same name as the data you are getting/supplying to grid.



回答4:

Kendo will be able to select a right filter type for each column, but you have to use properties instead of fields in TargetsModel.

For example, use this:

public class TargetsModel
{
    public int ID { get; set; }
    public string OrbitalPeriod { get; set; }
    ...
}

instead of this:

public class TargetsModel
{
    public int ID;
    public string OrbitalPeriod;
    ...
}

If for some reason you are unable to do this, the .Model() approach is probably the best way.