Dynamic schema based on data

4 posts, 0 answers
  1. Jeff
    Jeff avatar
    7 posts
    Member since:
    Mar 2017

    Posted 27 Nov Link to this post

    I've built a function that loops through my data set and builds an array of the schema and detects what the data type is for each column (this is similar to excel in that it loops through the first few rows of data to determine its data type). This function returns an array that is similar structure to how the datasource schema is defined.

    My problem is I cannot then figure out how to get this information into the data source configuration. calling the array directly into the datasource doesn't seem to work.

    Below you can see I'm assigning the variable schema to another function that has a response similar to the one in the code block at the bottom of this post.

    The full function is like so

     

    01.function create_kendo_data_source(path, pagesize, rows_to_check_for_schema, callback) {
    02.    if(pagesize == undefined) {
    03.        pagesize = 20;
    04.         
    05.    }
    06.     
    07.    ajax_json_post(path, null, function(response) { //success
    08.        var schema = create_kendo_data_source_schema(response);
    09.         
    10.        console.log(schema);
    11.         
    12.        var new_schema = {
    13.            id: "data",
    14.            fields: []
    15.             
    16.        };
    17.         
    18.        for(var i in schema) {
    19.            var item = schema[i];
    20.             
    21.            new_schema.fields.push({
    22.                [item.column]: {
    23.                    type: item.type
    24.                     
    25.                }
    26.                 
    27.            });
    28.             
    29.        }
    30. 
    31.        var ds = new kendo.data.DataSource({
    32.            transport: {
    33.                read: function (options) {
    34.                    options.success(response);
    35.                     
    36.                }
    37.                 
    38.            },
    39.            pageSize: pagesize,
    40.            schema: { new_schema
    41.                 
    42.            }
    43. 
    44.        });
    45.          
    46.        ds.read();
    47. 
    48.        console.log(ds);
    49.          
    50.        if(typeof callback === "function") {
    51.            callback(ds);
    52.             
    53.            return true;
    54.             
    55.        } else {
    56.            return false;
    57.             
    58.        }
    59.         
    60.    }, function() { //error
    61.         return false;
    62.          
    63.    });
    64.                 
    65.}
    1.1
    2.:
    3.{column: "column1_name", type: "string"}
    4.2
    5.:
    6.{column: "column2_name", type: "number"}
  2. Jeff
    Jeff avatar
    7 posts
    Member since:
    Mar 2017

    Posted 27 Nov in reply to Jeff Link to this post

    I also tried defining a new model from my array, but nothing really seems to happen differently on my grid. I've got specific filters defined for each datatype, and they're all still followinga string as their type.

    01.var columns = create_kendo_data_source_schema(response);
    02. 
    03.var schema = {
    04.    id: "data",
    05.    fields: []
    06.     
    07.};
    08. 
    09.for(var i in columns) {
    10.    var item = columns[i];
    11.     
    12.    schema.fields.push({
    13.        [item.column]: {
    14.            type: item.type
    15.             
    16.        }
    17.         
    18.    });
    19.     
    20.}
    21. 
    22.var new_schema2 = kendo.data.Model.define({
    23.    schema
    24.     
    25.});
    26. 
    27.var ds = new kendo.data.DataSource({
    28.    transport: {
    29.        read: function (options) {
    30.            options.success(response);
    31.             
    32.        }
    33.         
    34.    },
    35.    pageSize: pagesize,
    36.    schema: {
    37.        id: "data",
    38.        model: new_schema2
    39.         
    40.    }
    41. 
    42.});
  3. Stefan
    Admin
    Stefan avatar
    1394 posts

    Posted 29 Nov Link to this post

    Hello, Jeff,

    If the schema can be dynamic I can suggest checking the following resources with examples:

    https://docs.telerik.com/kendo-ui/controls/data-management/grid/how-to/various/create-with-dynamic-columns-and-data-types

    https://www.telerik.com/blogs/dynamic-data-in-the-kendo-ui-grid

    I hope this is helpful.

    Regards,
    Stefan
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  4. Jeff
    Jeff avatar
    7 posts
    Member since:
    Mar 2017

    Posted 29 Nov Link to this post

    Thanks for sharing the links, I gave up on finding a solution and accidentally stumbled upon your first link by searching for something unrelated. That first link gave me the epiphany of creating an array of columns with my specific filters defined there, then I apply those columns to the grid itself, and that is working great so far. 
Back to Top