[转载]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.
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
- <table id=“tt” title=“Formatting Columns” class=“easyui-datagrid” style=“width:550px;height:250px”
- url=“data/datagrid_data.json”
- singleSelect=“true” iconCls=“icon-save”>
- <thead>
- <tr>
- <th field=“itemid” width=“80”>Item ID</th>
- <th field=“productid” width=“80”>Product ID</th>
- <th field=“listprice” width=“80” align=“right” formatter=“formatPrice”>List Price</th>
- <th field=“unitcost” width=“80” align=“right”>Unit Cost</th>
- <th field=“attr1” width=“100”>Attribute</th>
- <th field=“status” width=“60” align=“center”>Stauts</th>
- </tr>
- </thead>
- </table>
Notice that the ‘listprice’ field has the ‘formatter’ attribute that indicate the format function.
Write format function
- function formatPrice(val,row){
- if (val < 20){
- return ‘<span style=“color:red;”>(‘+val+’)</span>‘;
- } else {
- return val;
- }
- }