[转载]Formatting DataGrid columns - jQuery EasyUI

[转载]Formatting DataGrid columns – jQuery EasyUI.

The following example formats a column in easyui DataGrid and uses a custom column formatter to color the text red if a price is below than 20.

View Demo

To format a DataGrid column, we should set the formatter property which is a function. The format function contains three parameters:

  • value: The current column value responding to field.
  • row: The current row record data.
  • index: The current row index.

Create DataGrid

  1. <table id=“tt” title=“Formatting Columns” class=“easyui-datagrid” 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>

Notice that the ‘listprice’ field has the ‘formatter’ attribute that indicate the format function.

Write format function

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

Download the EasyUI example:

easyui-datagrid-demo.zip
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏