条件设置行背景颜色


本教程将向您展示如何根据一些条件改变数据网格(datagrid)组件的行样式。当 listprice 值大于 50 时,我们将为该行设置不同的颜色。

数据网格(datagrid)的 rowStyler 函数的设计目的是允许您自定义行样式。以下代码展示如何改变行样式:

  1. <table id="tt" title="DataGrid" style="width:600px;height:250px"
  2. url="data/datagrid_data.json"
  3. singleSelect="true" fitColumns="true">
  4. <thead>
  5. <tr>
  6. <th field="itemid" width="80">Item ID</th>
  7. <th field="productid" width="80">Product ID</th>
  8. <th field="listprice" width="80" align="right">List Price</th>
  9. <th field="unitcost" width="80" align="right">Unit Cost</th>
  10. <th field="attr1" width="150">Attribute</th>
  11. <th field="status" width="60" align="center">Stauts</th>
  12. </tr>
  13. </thead>
  14. </table>
  1. $('#tt').datagrid({
  2. rowStyler:function(index,row){
  3. if (row.listprice>50){
  4. return 'background-color:pink;color:blue;font-weight:bold;';
  5. }
  6. }
  7. });

正如您所看到的,我们根据一些条件设置 background-color(背景色)为 pink(粉红色),设置文本颜色为 blue(蓝色)。

下载 jQuery EasyUI 实例

下载地址