jQuery EasyUI Data Grid -Операции со столбцами
В этом руководстве вы узнаете, как добавить столбец операций в редактируемую таблицу данных (datagrid). Столбец операций обычно содержит значения, вычисляемые из одного или нескольких других столбцов.
Сначала создайте редактируемую таблицу данных (datagrid). Здесь мы создали несколько редактируемых столбцов: 'listprice', 'amount' и 'unitcost', определённых как типы редактирования numberbox. Столбец операций — это поле 'unitcost', которое будет результатом умножения listprice на столбец amount.
<table id="tt" style="width:600px;height:auto"
title="Editable DataGrid with Calculated Column" iconCls="icon-edit" singleSelect="true"
idField="itemid" url="data/datagrid_data.json">
<thead>
<tr>
<th field="itemid" width="80">Item ID</th>
<th field="listprice" width="80" align="right" editor="{type:'numberbox',options:{precision:1}}">List Price</th>
<th field="amount" width="80" align="right" editor="{type:'numberbox',options:{precision:0}}">Amount</th>
<th field="unitcost" width="80" align="right" editor="numberbox">Unit Cost</th>
<th field="attr1" width="150" editor="text">Attribute</th>
<th field="status" width="60" align="center" editor="{type:'checkbox',options:{on:'P',off:''}}">Status</th>
</tr>
</thead>
</table>
Когда пользователь щёлкает по строке, мы запускаем действие редактирования.
var lastIndex;
$('#tt').datagrid({
onClickRow:function(rowIndex){
if (lastIndex != rowIndex){
$('#tt').datagrid('endEdit', lastIndex);
$('#tt').datagrid('beginEdit', rowIndex);
setEditing(rowIndex);
}
lastIndex = rowIndex;
}
});
Чтобы создать расчётные связи между некоторыми столбцами, нам следует получить текущие редакторы и привязать к ним некоторые события.
function setEditing(rowIndex){
var editors = $('#tt').datagrid('getEditors', rowIndex);
var priceEditor = editors[0];
var amountEditor = editors[1];
var costEditor = editors[2];
priceEditor.target.bind('change', function(){
calculate();
});
amountEditor.target.bind('change', function(){
calculate();
});
function calculate(){
var cost = priceEditor.target.val() * amountEditor.target.val();
$(costEditor.target).numberbox('setValue',cost);
}
}
Скачать примеры jQuery EasyUI
jeasyui-datagrid-datagrid15.zip
Другие расширения