easyUI在可編輯的datagrid中計算兩列的值

來源:互聯網
上載者:User

@author YHC

在這個教程中你將學習如何包含一個運算的列在可編輯的datagrid中,一個運算列通常包含一些運算值從一個或多個其他列.

 


查看Demo


首先,建立一個可編輯的datagrid,這就是我們建立的一些可編輯列,'listprice','amount' 和'unitcost' 列定義為numberbox 編輯類型,運算列是 'unitcost'欄位,

將是 listprice 乘以 amount列的結果.


[html]
<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>   

<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>  當使用者點擊一行的時候,我們開始一個編輯動作.


[javascript]
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; 
    } 
});  建立運算關係在一些列之間,我們應該得到editors和綁定一些事件到他們上面.


[javascript]
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);   
    }   
}   
作者:yhc13429826359
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.