【问题标题】:Kendo UI Grid : add row including variable which impact on templateKendo UI Grid:添加行,包括影响模板的变量
【发布时间】:2013-10-23 12:42:21
【问题描述】:

我的问题与我的网格更新有关。这是我目前的网格生成程序:

leTle[0] = {index : 1, tle : tleId, nom : nomChamp, estar : "", fige : tleFixe, position : positionParam, longueur : longueurParam};               

   var fige;

   if (tleFixe == "true")
        fige = true; 
   else
        fige = false;

    $("#" + theId).kendoGrid({
        columns:[{
                    field: "index",
                    title: "Index",
                    template: "<span style='font-size: 12px;' title='#= index #'>#= index # </span>",
                    width: 40
                },{
                    field: "tle",
                    title: "TLE Id",
                    template: "<span style='font-size: 12px;' title='#= tle #'>#= tle # </span>",
                    width: 100
                },{
                    field: "nom",
                    title: "Intitulé",
                    template: "<span style='font-size: 12px;' title='#= nom #'>#= nom # </span>"
                },{
                    field : "position",
                    title : "Position",
                    width : 70,
                    attributes : {
                        "class" : fige ? " " : "fondRougePale"
                    }
                },{
                    field : "longueur",
                    title : "Longueur",
                    width : 70,
                    attributes : {
                        "class" : fige ? " " : "fondRougePale"
                    }
                },{
                    field :"estar",
                    title :"Estar",
                    template: "<span class='eStar'><input class='inputeStar' disabled type='text' /> </span>",
                    width : 250
                },{
                    command: {
                        name : "destroy",
                        template: "<a class=\"btn btn-warning btn-mini k-grid-delete\">"
                                + "<i class=\"icon-minus-sign icon-white\"></i></a>"
                    },

                    title: " ",
                    width: 40
                }
        ],
        dataSource: {
          data: leTle,
          schema: {
              model: {
                  fields: {
                      tle: {editable: false},
                      nom: {editable: false},
                      estar: {editable: false},
                      longueur: {editable: fige},
                      position: {editable: fige}
                  }
              }
          }
        },
        editable:  {
            mode: "incell",
            confirmation: false
        },
        scrollable : false
    });

如您所见,如果我的变量“fige”等于 false,我的某些单元格可以被禁用。当我尝试使用手动编写的基本数据源构建网格时,网格还可以。一行接一行,当必须禁用单元格时,它们就是。

不幸的是,当我在构建网格后尝试添加行时,我的变量从不包括设置单元格的时间。

代码如下:

var vgrid = $("#tleSelected").data("kendoGrid");
            var datasource = vgrid.dataSource;
            var nbLines = vgrid.dataSource.total();
            //Booleen de test
            if (fige == "true")
                tfige = true; 
            else
                tfige = false;
            var newRecord = { index : nbLines+1, tle : tleId, nom: nomChamp, estar: "", position: position, longueur: longueur, fige: tfige}
            datasource.insert(newRecord);

所以,我的变量是好的,但新行不是。

您知道这种情况的解决方案,而不是破坏我的网格并在更新数据后重建它们吗?

非常感谢。

【问题讨论】:

    标签: jquery grid kendo-ui row kendo-grid


    【解决方案1】:

    这可能不是您的答案,但可能会帮助您实现目标。每次grid的数据源发生变化都会调用formatMe函数,它的返回值会显示在grid的单元格中。

    function formatMe(data){
        return data + " bla bla";
    }
    
    var grid = $("#grid").kendoGrid({
        dataSource: {
            data: createRandomData(50)
        },
        columns: [
            { field: "YourFieldName","template:"#= formatMe(data) #" }]
    }).data("kendoGrid");
    

    【讨论】:

    • 谢谢,我现在试试你的建议。我不认为在 #= # 中使用函数。
    • 所以,我可以按照您的建议在 Columns 字段中添加一个类。但是,当我想使用相同的过程更正 schema.model.fields.editable 时,这并不好。我想我会直接用 data-role="editable" ...
    • 对不起,我现在太累了(写代码 10 小时)。我认为您应该使用 datasource object 并将其绑定到您的网格,然后每次您从数据源中编辑或添加或删除它时,它都应该自动更新网格,并且 #= formatMe(data) # 将被自动调用。
    • 我不确定是否理解数据源对象的兴趣。我想我已经理解了您描述的解决方案。事实上,我已经创建了一个网格,它在列上具有不同的类,具体取决于行和“#= formatMe(data)#”的返回。对于模板来说很好。之后,当我想选择我的单元格是否应可编辑时,相同的机器不起作用。我希望我清楚,否则我会回答我当前的代码。再次感谢。
    【解决方案2】:

    最后,我找到了解决这个问题的替代方案。

    变量“fige”是我们选择单元格时允许编辑的变量。 我更喜欢自己制作解决方案,而不是使用 KendoUi。

    当我建立“位置”和“长”列时:

    {
        field : "position",
        title : "Position",
        width : 70,
        template : "<span class='position#= index #' ><input style='width:70px' type='text' value='#= position#' #= readOnly(fige) # /></span>"
    },{
        field : "longueur",
        title : "Longueur",
        width : 70,
        template : "<span class='longueur#= index #'><input style='width:70px' type='text' value='#= longueur#' #= readOnly(fige) # /></span>"
    }
    

    还有 readOnly(fige) 函数:

    function readOnly(fige)
    {  
        if (fige == "true")
            return "readonly";
        else
            return ""; 
    }
    

    借助此功能,我动态地获得可编辑或不可编辑的单元格。也许它不如 KendoUI 默认解决方案好,但我不认为这个特殊功能是可能的,这要归功于 KendoUI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多