Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery EasyUI DataGrid - Column Calculation

jQuery EasyUI DataGrid - Column Calculation

In this tutorial, you will learn how to include a calculated column in an editable datagrid. A calculated column typically contains values computed from one or more other columns.

First, create an editable datagrid. Here we create some editable columns; the 'listprice', 'amount', and 'unitcost' columns are defined as numberbox editor types. The calculated column is the 'unitcost' field, which is the result of listprice multiplied by the amount column.



    

        

            

                

                

                

                

                

                

            

        

    
Item IDList PriceAmountUnit CostAttributeStatus

When the user clicks a row, we start an editing action.



    var lastIndex;

    $('#tt').datagrid({

        onClickRow:function(rowIndex){

            if (lastIndex != rowIndex){

                $('#tt').datagrid('endEdit', lastIndex);

                $('#tt').datagrid('beginEdit', rowIndex);

                setEditing(rowIndex);

            }

            lastIndex = rowIndex;

        }

    });

To create a calculation relationship between some columns, we should get the current editors and bind some events to them.



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

        }

    }

Download the jQuery EasyUI example

jeasyui-datagrid-datagrid15.zip