Search code examples
jqueryasp.netkendo-uikendo-gridkendo-asp.net-mvc

Kendo Grid Json Data for Header and Content


I am working on Asp.Net MVC 5 project where I need to change the tables with Kendo Grid Jquery.

Currently I am returning data from an Api controller that looks like this:

[
headers:{{
            "label": "Name",
            "id": "name",
            "isfixed": true,
            "width": "200px"
        },
        {
            "width": "200px",
            "isfixed": false,
            "id": "city",
            "label": "City"
        },
        },
rows:{
        {name:"Joe",city:"Berlin"},
        {name:"Doe",city:"London"},
        ...
        }
]

Kendo doesn't seem to like this format. It works only if I send JSON like this:

[
  { name: "Joe", City: "Berlin" },
  { name: "Doe", City: "Londo" }
]

Question: Can I make the Kendo grid work with the first type of data as I need dynamic columns and data for the different tables?


Solution

  • How to accomplish this is detailed here: https://docs.telerik.com/kendo-ui/knowledge-base/create-with-dynamic-columns-and-data-types

    Code to accomplish it:

    
        <div id="grid" style="width:1000px;"></div>
    
        <script>
          var isDateField =[];
          $.ajax({
            url: "https://www.mocky.io/v2/5835e736110000020e0c003c",
            dataType: "jsonp",
            success: function(result) {
              generateGrid(result);
            }
          });
    
          function generateGrid(response) {
            var model = generateModel(response);
            var columns = generateColumns(response);
    
            var grid = $("#grid").kendoGrid({
              dataSource: {
                transport:{
                  read:  function(options){
                    options.success(response.data);
                  }
                },
                pageSize: 5,
                schema: {
                  model: model
                }
              },
              columns: columns,
              pageable: true,
              editable:true
            });
          }
    
          function generateColumns(response){
            var columnNames = response["columns"];
            return columnNames.map(function(name){
              return { field: name, format: (isDateField[name] ? "{0:D}" : "") };
            })
          }
    
          function generateModel(response) {
    
            var sampleDataItem = response["data"][0];
    
            var model = {};
            var fields = {};
            for (var property in sampleDataItem) {
              if(property.indexOf("ID") !== -1){
                model["id"] = property;
              }
              var propType = typeof sampleDataItem[property];
    
              if (propType === "number" ) {
                fields[property] = {
                  type: "number",
                  validation: {
                    required: true
                  }
                };
                if(model.id === property){
                  fields[property].editable = false;
                  fields[property].validation.required = false;
                }
              } else if (propType === "boolean") {
                fields[property] = {
                  type: "boolean"
                };
              } else if (propType === "string") {
                var parsedDate = kendo.parseDate(sampleDataItem[property]);
                if (parsedDate) {
                  fields[property] = {
                    type: "date",
                    validation: {
                      required: true
                    }
                  };
                  isDateField[property] = true;
                } else {
                  fields[property] = {
                    validation: {
                      required: true
                    }
                  };
                }
              } else {
                fields[property] = {
                  validation: {
                    required: true
                  }
                };
              }
            }
    
            model.fields = fields;
    
            return model;
          }
        </script>