Kendo UI grid rowTemplate - calling a function to affect the css of td cells

后端 未结 2 1157
温柔的废话
温柔的废话 2021-01-26 06:54

My Kendo UI grid is dynamic, where the columns can be defined as field0, field1 through field[n] ; I do NOT know the number

2条回答
  •  清歌不尽
    2021-01-26 06:56

    By looking at the first row of data from the datasource, you can iterate the properties to get all the fields and build dynamically the columns and row template of the grid and the model fields of the dataSource:

    var ColsFieldsRowTemplate = {}
      function GetRowTemplate(){
        var obj = {};
        var columns= [];
        var fields = {};
    
    
        var t = '';
        //use first row of data to get the fields
        var row = myData[0];
        for (var key in row) {
          if (key == 'field0'){
            fields[key] = { type: "string" };
            columns.push({"field": key});
            t += '#= ' + key  + '  #';
          } else if (key.indexOf("field") > -1){
            fields[key] = { type: "number" };
            columns.push({"field": key});
            t += '#= ' + key  + '  #';
          }
        }
        t += '';
    
        ColsFieldsRowTemplate.rowTemplate = t;
        ColsFieldsRowTemplate.cols = columns;
        ColsFieldsRowTemplate.fields = fields;
        console.log(ColsFieldsRowTemplate);
        return ColsFieldsRowTemplate;
      }
      GetRowTemplate();
    
      vm.userGridOptions = {
                selectable: true,
                sortable: true,
                pageable: true,       
                rowTemplate: ColsFieldsRowTemplate.rowTemplate,
                columns: ColsFieldsRowTemplate.cols,
                editable: {
                    mode: "popup"
                    //template: kendo.template($("#editorTemplate").html())   // loaded up in index.html (alt. kendo.template('
    ..
    ') ) }, toolbar: ["create"] }; // DEFINE DATA SOURCE FOR USER KRIs vm.gridDS = new kendo.data.DataSource({ // customized User KRI grid; pull from server or localStorage pageSize: 10, transport: { read: function(options){ options.success(myData) } }, schema: { model: { id: "id", fields: ColsFieldsRowTemplate.fields } } });

    Updated PLUNKER

提交回复
热议问题