Extjs 更改Ext.data.Model中的数据

Extjs 更改Ext.data.Model中的数据,extjs,extjs4,extjs4.1,Extjs,Extjs4,Extjs4.1,以下是我拥有的两种型号: Ext.define('Company', { extend: 'Ext.data.Model', fields: [ {name: 'company'}, {name: 'price', type: 'float'}, {name: 'change', type: 'float'}, {name: 'pctChange', type: 'float'}, {name:

以下是我拥有的两种型号:

Ext.define('Company', {        extend: 'Ext.data.Model',
    fields: [
        {name: 'company'},
        {name: 'price', type: 'float'},
        {name: 'change', type: 'float'},
        {name: 'pctChange', type: 'float'},
        {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
        {name: 'industry'},
        {name: 'desc'}
     ]
});

Ext.define('CompanyDemo', {        extend: 'Ext.data.Model',
    fields: [
        {name: 'company'},
        {name: 'price', type: 'float'},
        {name: 'change', type: 'float'},
        {name: 'pctChange', type: 'float'},
        {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
        {name: 'total'}
     ]
});
这两个模型与两个不同的网格相关联。我将从一个网格(与公司模型关联)中选择一些行,单击按钮(与公司模型关联,最初该模型为空且没有任何数据),第二个网格中将仅显示所选行

这是具有公司模型的网格:

var grid2 = Ext.create('Ext.grid.Panel', {      id: 'grid2',
    store: getLocalStore(),
    selModel: sm,
    columns: [
        {text: "Company", width: 200, dataIndex: 'company'},
        {text: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
        {text: "Change", dataIndex: 'change'},
        {text: "% Change", dataIndex: 'pctChange'},
        {text: "Last Updated", width: 135, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'},
        {text: "Industry", dataIndex: 'industry'},
        {text: "Description", dataIndex: 'desc'}
    ],
    columnLines: true,
    width: 600,
    height: 300,
    frame: true,
    title: 'Framed with Checkbox Selection and Horizontal Scrolling',
    iconCls: 'icon-grid',
    renderTo: 'grid'
});
我能够从该网格中获取所选数据。在获得这些数据后,我需要替换Industry和Desc数据,并将其替换为Total(根据上面给出的模型结构)。以下是我使用公司模型获取网格中选定行的方式:

var grid2 = Ext.create('Ext.grid.Panel', {      id: 'grid2',
    store: getLocalStore(),
    selModel: sm,
    columns: [
        {text: "Company", width: 200, dataIndex: 'company'},
        {text: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
        {text: "Change", dataIndex: 'change'},
        {text: "% Change", dataIndex: 'pctChange'},
        {text: "Last Updated", width: 135, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'},
        {text: "Industry", dataIndex: 'industry'},
        {text: "Description", dataIndex: 'desc'}
    ],
    columnLines: true,
    width: 600,
    height: 300,
    frame: true,
    title: 'Framed with Checkbox Selection and Horizontal Scrolling',
    iconCls: 'icon-grid',
    renderTo: 'grid'
});
编辑:

尝试使用以下代码:

Ext.widget('button', {
text: 'Click Me',   
renderTo: 'btn',
listeners: {
    click: function(this1, evnt, eOpts ){  
      var records = sm.getSelection();
      var data = [];
      Ext.each(records, function (record) {
        var modelData = record.getData(true);
        //A dummy value as of now
        modelData.total = '10';
        data.push(modelData);
      });
      // grid1.getStore().loadData(records, true);         
      grid1.getStore().loadData(data, true);
    }
}
});
但在这一行中引发了一个异常:

grid1.getStore().loadData(data, true);
我不知道如何替换第一个模型中的两个字段,并在第二个网格中显示之前在其中添加新数据


请让我知道这一点。

您的问题是给新网格的数据错误。它将需要一个CompanyDemo模型对象数组

click: function(this1, evnt, eOpts ){  
    var records = sm.getSelection(),
        demoModels = [];

    // Iterate over the selected Company records
    Ext.each(records, function (record) {
        // Copy the data from the original Company model into the demo model
        var demoModel = new CompanyDemo(record.getData());
        demoModel.set('total', 10);

        demoModels.push(demoModel);
    });

    grid1.getStore().loadData(demoModels, true);
}

你的问题是你给了新的网格错误的数据。它将需要一个CompanyDemo模型对象数组

click: function(this1, evnt, eOpts ){  
    var records = sm.getSelection(),
        demoModels = [];

    // Iterate over the selected Company records
    Ext.each(records, function (record) {
        // Copy the data from the original Company model into the demo model
        var demoModel = new CompanyDemo(record.getData());
        demoModel.set('total', 10);

        demoModels.push(demoModel);
    });

    grid1.getStore().loadData(demoModels, true);
}