jQuery EasyUI データグリッド -列のフォーマット
以下の例では、easyui DataGrid の列データをフォーマットし、カスタム列の formatter を使用します。価格が20未満の場合は、テキストが赤くなります。
データグリッド(DataGrid)列をフォーマットするには、formatter プロパティを設定する必要があります。これは関数です。このフォーマット関数には3つのパラメータがあります:
- value:現在の列に対応するフィールド値。
- row:現在の行のレコードデータ。
- index:現在の行のインデックス。
データグリッド(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>
'listprice' フィールドには 'formatter' プロパティがあり、フォーマット関数を指定するために使われることに注意してください。
フォーマット関数を書く
function formatPrice(val,row){
if (val < 20){
return '<span style="color:red;">('+val+')</span>';
} else {
return val;
}
}
jQuery EasyUI サンプルをダウンロード
jeasyui-datagrid-datagrid7.zip
その他の拡張