改变Ext.data.Model中的数据

时间:2012-10-06 03:47:08

标签: 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: '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'}
     ]
});

这两个模型与两个不同的网格相关联。我将从一个网格中选择一些行(与公司模型相关联),并且只有按钮单击时,所选行将显示在第二个网格中(与CompanyDemo模型相关联,最初此模型为空且没有任何行数据)

这是公司模型的网格:

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(根据上面给出的模型结构)。这就是我使用公司模型获取网格的选定行的方法:

修改

尝试了这段代码:

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

我不知道如何在第一个模型中替换这两个字段并在第二个网格中显示之前添加新数据。

请让我知道这件事。

1 个答案:

答案 0 :(得分:3)

您的问题是您为新网格提供了错误的数据。它将需要一系列的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);
}