数据表 jQuery 在 "sClass" 上提供动态数组索引值



给定以下代码:

var aDataSet = [
    ['1', 'Trident','Internet Explorer 4.0','Win 95+','4','X'],
    ['2', 'Fish','Internet Explorer 5.0','Win 95+','5','C'],
    ['3', 'Pony','Internet Explorer 5.5','Win 95+','5.5','A']
];
$('#example').dataTable( {
    "aaData": aDataSet,
    "aoColumns": [
        { "sTitle": "Engine" },
        { "sTitle": "Browser" },
        { "sTitle": "Platform" },
        { "sTitle": "Version", "sClass": "customCSS-" +aDataSet['id']['Version']  }, 
        { "sTitle": "Grade", "sClass": "center" }
    ]
} );    

我希望能够查找当前正在处理的aDataSet元素并获得其Version信息,以便我可以在aoColumns样式的上下文中进行字符串连接。上面的代码不执行任何样式。这一修改:

    { "sTitle": "Version", "sClass": "customCSS-" +aDataSet[0]['Version']  }

使所有内容都匹配第0个索引(很明显),但我希望我能以某种方式强制进行查找。

嗯,老实说,我不认为你担心Version值本身,因为那只是数据表的标准虚拟数据,所以我冒昧地不使用它作为css类名称的串联的一部分。Version值包含点,这将使css混乱。然后记住这一点,并假设我理解您要做什么,下面是执行任务的一种方法,如jsbin:

所示:http://jsbin.com/onelev/2/edit

这里主要的一点是使用fnRowCallback来生成表单元格上的动态类名。

CSS设置自定义单元格颜色

.customCSS-Trident, table.dataTable tr.even td.customCSS-Trident.sorting_1, table.dataTable tr.odd td.customCSS-Trident.sorting_1 { background-color: blue; color: #fff; }
.customCSS-Fish, table.dataTable tr.even td.customCSS-Fish.sorting_1, table.dataTable tr.odd td.customCSS-Fish.sorting_1 { background-color: green; color: #fff; }
.customCSS-Pony, table.dataTable tr.even td.customCSS-Pony.sorting_1, table.dataTable tr.odd td.customCSS-Pony.sorting_1 { background-color: brown; color: yellow; }
.customCSS-Cabbage, table.dataTable tr.even td.customCSS-Cabbage.sorting_1, table.dataTable tr.odd td.customCSS-Cabbage.sorting_1 { background-color: pink; }
JavaScript

   $(document).ready( function() {
     var oTable = $('#example').dataTable( {
       "aaData": aDataSet,
       "aoColumnDefs": [ 
         {  "aTargets": [ 0 ], 
            "sTitle": "#" 
         },
         {  "aTargets": [ 1 ], 
            "sTitle": "Engine" 
         },
        { "aTargets": [ 2 ], 
           "sTitle": "Browser" 
        },
        { "aTargets": [ 3 ], 
         "sTitle": "Platform" 
        },
         { "aTargets": [ 4 ],
          "sTitle": "Version"
        },      
        { "aTargets": [ 5 ], 
          "sTitle": "Grade", 
          "sClass": "center" 
         }
       ], 
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
          $('td:eq(4)', nRow).addClass("customCSS-" + aData[1]);    
            return nRow;
        },
     });
   } );

      <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
        <thead>
          <tr>
            <th>#</th>
            <th>Engine</th>
            <th>Browser</th>
            <th>Platform(s)</th>
            <th>Engine version</th>
            <th>CSS grade</th>
          </tr>
        </thead>
        <tbody>
       </tbody>
  </table>

附录
当您对一个特定的列进行排序时,datatable动态地生成sorting_1类。如果老练的用户按住shift键,然后单击另一个列标头,那么datatable会生成另一个名为sorting_2的类,以此类推。虽然用户按多列排序的可能性很低,但是可以通过为这些额外的情况添加额外的css规则来处理这些情况。

最新更新