This is a migrated thread and some comments may be shown as answers.

Column filter based on data type

3 Answers 397 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mattia
Top achievements
Rank 1
Mattia asked on 22 Jun 2015, 07:37 AM

Hello, I'm using Kendo.Mvc and Kendo.UI for developing an ASP.NET MVC web application.
I have a Grid with ajax binding that reads data from a Datatable, those data could be of different types (string, int, double, bool, ecc..).
I have no model strongly typed associated for this Grid, thus I'll check for the type of each column and I do the bind for each column based of their type.

My problem is that this binding seems not to be working, because each column filter is treated as if it were of type string.

here some code snippet:

initialization: Html.Kendo().Grid<object>()
 
// other code
 
for each column:
        grid.Columns(columns =>
        {
            var bound = columns.Bound(Type.GetType("System." + col.Type), col.Name);
            // other code
            bound.Filterable(true);
        })
 
where col.Type is an enum similar to .NET TypeCode Enumeration (https://msdn.microsoft.com/en-us/library/system.typecode(v=vs.110).aspx)

I've also tried to add manually the type of column, after initialization of the grid, with some client side javascript code:

$.each(cols, function(i, e) {        
    var index = grid.columns.map(function(obj) {
            return obj.field;
        })
        .indexOf(e.Name);
 
    if(index > -1) {
        var item = grid.columns[index];
 
        switch(e.Type) {
            case "string":
                item.type = e.Type;
            break;
 
            case "double":
            case "integer":
                item.type = "number";
            break;
 
            case "boolean":
                item.type = e.Type;
            break;
 
            case "datetime":
                item.type = "date";
            break;                                               
        }    
    }
});

after doing this, I have a new "type" property for each column inside grid.columns but seems to not affect each column filter.

Some suggestion to achieve this?
Thanks a lot!

 

3 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 24 Jun 2015, 09:24 AM
Hi,

Please accept my apologies for the delayed response.

Specifying the type on the columns is not sufficient for the filter to work properly. You will also need to define the model schema in a similar way.
    .DataSource(dataSource => dataSource        
        .Custom()         
        .Batch(true)
        .PageSize(20)
        .Schema(schema => schema.Model(m => {
m.Id(p => p.ProductID);
                ...
}))
    )


See the Custom Data Source demo in the offline MVC demos.

I hope this helps.

Regards,
T. Tsonev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Mattia
Top achievements
Rank 1
answered on 25 Jun 2015, 07:32 AM
Hi T. Tsonev, thanks for the reply, I've made some progress towards a solution but I have still some incorrect behaviour.
I have a dynamic model, not strongly typed, so I define for each column the a ModelFieldDescriptor, in this way:

grid.Columns(columns =>
        {
            var bound = columns.Bound(col.Name);
             
            ModelFieldDescriptor modelField = new ModelFieldDescriptor();
            modelField.Member = col.Name;
            modelField.DefaultValue = null;
            modelField.IsEditable = false;
            modelField.MemberType = Type.GetType("System." + col.Type);
             
            columns.Container.DataSource.Schema.Model.Fields.Add(modelField);       
 
            // other code

 the datasource binding is ajax binding (I don't think I need a custom binding), thus I have this snippet of code

grid.DataSource(ds =>
{
    var ajaxDsBuilder = ds.Ajax();
    ajaxDsBuilder.Model(model => model.Id("ID")).Events(ev => ev.Error("gridOnError"));
 
    ajaxDsBuilder.Read(read =>
    {
        var controller = (ViewContext.RouteData.Values["controller"] as string).ToLower();
        var readActions = read.Action("DoSelectGrid", controller).Type(HttpVerbs.Post);         
        readActions.Data("getPoolData_" + Model.Current.Id);
    });
 
    //other code

Now the filter of the columns are displayed correctly, I mean if I define the column of type bool it displays only True or False, or if I define the columns of type int it displays a numeric textbox.
The problem now is that if I filter or sort, for example, a numeric value, it treats each value as string.
If you look the two attached screenshots you can see the problem, in the first (Immagine1.png) I've sorted a numeric column but the sorting is treating the value like string and not like numbers.
In the second image I've the same values, and I've filtered for values greater or equal than 50 but it displays only the 50 value.

Can you help me? Thanks!

Mattia T.




0
Mattia
Top achievements
Rank 1
answered on 26 Jun 2015, 09:50 AM
Ok I think I have resolved, the problem was in the creation of the datatable: the type of DataColumn was always String.
Tags
Grid
Asked by
Mattia
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Mattia
Top achievements
Rank 1
Share this question
or