以下实例格式化在 easyui DataGrid 里的列数据,并使用自定义列的 formatter,如果价格小于 20 就将文本变为红色。

EasyUI 格式化列_ico

为了格式化一个数据​网格​​(DataGrid)列,我们需要设置 formatter 属性,它是一个​函数​​。这个格式化函数包含三个参数:


  • value:当前列对应字段值。
  • row:当前的行记录数据。
  • index:当前的行下标。

创建数据网格(DataGrid)


  1. <table id="tt" title="Formatting Columns" class="easyui-​data​grid" style="width:550px;height:250px"
  2. url="data/datagrid_data.json"
  3. singleSelect="true" iconCls="icon-save">
  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" formatter="formatPrice">List Price</th>
  9. <th field="unitcost" width="80" align="right">Unit Cost</th>
  10. <th field="attr1" width="100">Attribute</th>
  11. <th field="​status​" width="60" align="center">Stauts</th>
  12. </tr>
  13. </thead>
  14. </table>

请注意,'listprice' 字段有一个 'formatter' 属性,用来指明格式化​函数​​。

写格式化函数


  1. function formatPrice(val,row){
  2. if (val < 20){
  3. return '<​span​ ​style​="color:​red​;">('+val+')</span>';
  4. } else {
  5. return val;
  6. }
  7. }