The following instance formats the column data in the Easyui DataGrid and uses the formatter of the custom column to turn the text red if the price is less than 20.
In order to format a data grid (DataGrid) column, we need to set the formatter property, which is a function. This format function contains three parameters:
- Value: The current column corresponds to the field value.
- Row: The current row record data.
- Index: The current row subscript.
Create a data grid (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=">"Item ID</th >
- <th field = "ProductID" width=< Span class= "ATV" > " > product id</TH>
- <th field = "ListPrice" width=< Span class= "ATV" > " align= "right" formatter= "Formatprice" >list price</TH>
- <th field="UnitCost" width= "align" = "Right"> Unit cost</th>
- <th field="attr1" width="Attribute"></th >
- <th field="status" width="Align" = "center">stauts</th>
- </tr>
- </thead>
- </table>
Note that the ' ListPrice ' field has a ' formatter ' attribute, which is used to indicate the format function.
Write formatting functions
- function formatprice(val,row) {
- if (val < ) {
- return ' <span style= ' color:red; ' > ('+val+') </span> ';
- } else {
- return Val;
- }
- }
Easyui Formatting columns