2

我不确定如何提取第 3 和第 4 个单元格 (TD) 中的值来比较它们。如果它们具有相同的值,我想在行中添加一个 cssClass;也不知道该怎么做:

 $("#grid tr").each(function() {


           var theValueInCell3 = ?   // how to get the cell's value?
           var theValueInCell4 = ?   // how to get the cell's value

           if (theValueInCell3 == theValueInCell4)
             {

                  //pseudo-code to add a cssClass to the row
                  $(this).addClass('foo');
             }

    });

编辑:这是我尝试遵循@Pechka 建议的最新尝试:

   .
   .
   .
   if (grid != null) {
    grid.dataSource.data(parsedData);        
    setTimeout(StyleRows, 500);
  }


  function StyleRows() {

      var grid = $('#grid').data('kendoGrid');
      $("#grid tr").each(function () { 
         var dataItem = grid.dataItem(this);   // dataItem is undefined
         if (dataItem.PropA == dataItem.PropB) {
            $(this).addClass('foo');
        }

    });
   }

错误是dataItem未定义的。

4

2 回答 2

5

您好,我建议您使用用于检索与该行相关的底层模型的dataItem方法。例如

var grid = $('#grid').data().kendoGrid;
$('#grid tr').each(function(){
     var dataItem = grid.dataItem(this);
     if(dataItem.PropName == dataItem.SomeOtherProp){
          $(this).addClass('foo');
     }
})
于 2012-12-17T19:10:36.857 回答
2

如果您正在使用KendoGrid,您可能会DataSource受到限制。因此,每一列实际上都有一个名称,您可以从绑定值中获取值。

如果您更喜欢使用 jQuery 路径(从您的草稿代码中可以看出),您可以查看nth-child允许您获取对第 n 个单元格或表格行的引用的选择器。

$("tr", "#grid").each(function (idx, elem) {
    var theValueInCell3 = $(":nth-child(3)", elem).html();
    var theValueInCell4 = $(":nth-child(4)", elem).html();

    //pseudo-code to add a cssClass to the row
    if (theValueInCell3 === theValueInCell4) {
        $(this).addClass('foo');
    }
});

最后,为了格式化KendoGrid一行,您可以查看Grid Row Template

于 2012-12-17T18:00:05.133 回答