折翼的飞鸟

导航

Extjs grid表格中单元格编辑,触发store修改

var me = this;
var fields = ['ActuallyQty', 'Price', 'ActuallyAmt'];
var columns = [
  {
        text: '实盘数量',
        dataIndex: 'ActuallyQty',
        style: "text-align:center;",
        align: 'right',
        sortable: true,
        width: 105,
        getEditor: function(record) {
            // 创建编辑的表格列
            return Ext.create('Ext.grid.CellEditor', {
                field: Ext.create('Ext.form.field.Number', {
                    decimalPrecision: 4,
                    maxLength: 12,
                    minValue: 0,
                    fieldCls: 'can-edit',
                    allowBlank: false,
                    blankText: '实盘数量不可为空',
                    recordData: record.data,
                    listeners: {
                        change: function (obj, newValue, oldValue) {
                            CalculDiffAmt(newValue, "actually", obj.recordData);
                        }
                    }
                })
            });
        },
        renderer: function (v, m, record) {
            if (!v) { v = 0; } 
            return parseFloat(v).toFixed(4);
        }
    }, {
        text: '成本单价',
        dataIndex: 'Price',
        style: "text-align:center;",
        align: 'right',
        sortable: true,
        width: 105,  
    }, {
        text: '实盘金额',
        dataIndex: 'ActuallyAmt',
        style: "text-align:center;",
        align: 'right',
        sortable: true,
        width: 120, 
    }      
]

 
     /**
     * 创建带合计的grid
     */
    CreateSummaryGrid: function(height, gridId, filds, columns, dockedText, isSelModel) {
        var dockHidden = !dockedText || dockedText == null ? true : false;
        dockedText = "共选择" + dockedText;
        var selModel = null;
        if (isSelModel) {
            selModel = {
                selType: 'checkboxmodel',
                mode: 'SIMPLE',
                checkOnly: true
            };
        }
        var store = Ext.create("Ext.data.Store", {
                fields: filds
            }),
            grid = Ext.create("Ext.grid.Panel", {
                columnLines: true,
                store: store,
                columns: columns, //显示字段
                name: gridId,
                loadMask: true,
                cls: 'main-grid',
                style: "border-top:1px solid #ddd;margin-left:auto;margin-right:auto",
                height: height,
                selModel: selModel == null ? Ext.create('Ext.selection.RowModel', {
                    mode: "MULTI"
                }) : selModel,
                plugins: [
                    Ext.create('Ext.grid.plugin.CellEditing', {
                        clicksToEdit: 1
                    })
                ],
                dockedItems: [{
                    xtype: 'toolbar',
                    dock: "bottom",
                    hidden: dockHidden,
                    items: [{
                        xtype: "box",
                        width: 10,
                        html: " "
                    }, {
                        xtype: 'label',
                        text: dockedText,
                        style: {
                            color: "#15A49E"
                        }
                    }, {
                        xtype: 'label',
                        name: "selectTotal",
                        text: "0",
                        style: {
                            color: "#15A49E",
                            fontWeight: "bold"
                        }
                    }, {
                        xtype: 'label',
                        text: "项",
                        style: {
                            color: "#15A49E"
                        }
                    }]
                }],
                features: [{
                    ftype: 'summary'
                }]
            });
        return grid;
    },


    var CaseDetailGrid = this.CreateSummaryGrid(500, 'detailGrid', fields, columns);


    //计算实盘金额
    CalculDiffAmt: function (value, type, record) {
      
        var me = this;
        var row;
        var rowIdx = -1;
        if (record) {
            var storeData = CaseDetailGrid.store.data.items;
            for (var i=0;i<storeData.length;i++) {
                if (storeData[i].get('CheckDtlId') == record['CheckDtlId']) {
                    rowIdx = i;
                    break;
                }
            }
        }
        if (rowIdx != -1) {
            row = CaseDetailGrid.getStore().getAt(rowIdx);
        }
        if (!row) {
            return false;
        }
        var price = Number(row.data["Price"] || 0);
           
        row.set("ActuallyAmt", price * value);     
        row.commit();  // 提交store
    },

 

   1、修改store数据,找出store中修改数据的行对象

row = CaseDetailGrid.getStore().getAt(rowIdx);

2、修改对应单元格
row.set("ActuallyAmt", price * value);     

   3、提交数据

 row.commit();

 

posted on 2022-01-06 16:15  折翼的飞鸟  阅读(479)  评论(0编辑  收藏  举报