Easyui to calculate the values of two columns in an editable datagrid

Source: Internet
Author: User

In this tutorial you will learn how to include a column of operations in an editable DataGrid, an operation column that typically contains some of the operands from one or more other columns.

View Demo

First, create an editable DataGrid, which is the editable columns we created, the ' listprice ', ' Amount ' and ' unitcost ' columns are defined as Numberbox edit types, the operation columns are ' unitcost ' fields,

will be the result of multiplying the ListPrice by the amount column.

<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=">item ID</th>

<th field=" ListPrice " width="  align= "right"  editor=" {type: ' Numberbox ', Options:{precision:1}} ">list price </th>   

<th field= "amount"  width= "80"  align= "right"  editor= >amount </th>   

<th field=" unitcost " width="  align= "right"  editor=" Numberbox ">unit cost</th< span class= "tag" >>   

<th field= "attr1"  width= "150"  editor= "text" >attribute</th>   

<th field="status" width="align=" Center " editor=" {type: ' checkbox ', Options:{on: ' P ', Off: '}} '>status</th>

</tr>

</thead>

</table>

<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=">item ID</th>

<th field=" ListPrice " width="  align= "right"  editor=" {type: ' Numberbox ', Options:{precision:1}} ">list price </th>    

<th field= "amount"  width= "80"  align= "right"  editor= >amount </th>    

<th field=" unitcost " width="  align= "right"  editor=" Numberbox ">unit cost</ Th>    

<th field= "attr1"  width= "150"  editor= "text" >attribute</th>    

<th field="status" width="align=" Center " editor=" {type: ' checkbox ', Options:{on: ' P ', Off: '}} '>status</th>

</tr>

</thead>

</table>

When the user clicks on a line, we start an edit action.

var LastIndex;

$ (' #tt '). DataGrid ({

Onclickrow:function (rowIndex) {

if (lastIndex! = RowIndex) {

$ (' #tt '). DataGrid (' EndEdit ', lastIndex);

$ (' #tt '). DataGrid (' BeginEdit ', rowIndex);

Setediting (RowIndex);

}

LastIndex = RowIndex;

}

});

var LastIndex;

$ (' #tt '). DataGrid ({

Onclickrow:function (rowIndex) {

if (lastIndex! = RowIndex) {

$ (' #tt '). DataGrid (' EndEdit ', lastIndex);

$ (' #tt '). DataGrid (' BeginEdit ', rowIndex);

Setediting (RowIndex);

}

LastIndex = RowIndex;

}

});

Create an operation relationship between some columns, we should get editors and bind some events to them above.

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);

}

}

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);

}

}

Easyui to calculate the values of two columns in an editable datagrid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.