2013-02-18 84 views
0

如何在单击保存按钮时保存编辑过的记录?将编辑后的记录保存在单元格编辑网格中

enter image description here

我的代码:

// create the Data Store 
var store = Ext.create('Ext.data.Store', { 
    //autoSync: true, 
    autoLoad:true, 
    autoDestroy: true, 
    url: 'update_premise.php', 
    model: 'Plant', 
    proxy: { 
     type: 'ajax', 
     // load remote data using HTTP 
     url: 'getLab.php', 
     // specify a XmlReader (coincides with the XML format of the returned data) 
     reader: { 
      type: 'json' 
     }, 
      writer: { 
      type: 'json' 

     } 
    }, 
    sorters: [{ 
     property: 'lab_name', 
     direction:'ASC' 
    }] 
}); 

var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', { 
    clicksToEdit: 1 
}); 

// create the grid and specify what field you want 
// to use for the editor at each header. 
var grid = Ext.create('Ext.grid.Panel', { 
    store: store, 
    columns: [{ 
     id: 'lab_name', 
     header: 'Common Name', 
     dataIndex: 'lab_name', 
     flex: 1, 
     editor: { 
      allowBlank: false 
     } 
    }], 
    selModel: { 
     selType: 'cellmodel' 
    }, 
    renderTo: 'editor-grid', 
    width: 600, 
    height: 300, 
    title: 'Lab?', 
    frame: true, 
    tbar: [ 
    { 
     text: 'Save', 
     handler: function() 
     { 
       for (var i = 0; i <grid.store.data.items.length; i++) { 
       var record = grid.store.data.items [i]; 
       if (record.dirty) { 
       } 
      } 
     } 
    } 
    ], 
    plugins: [cellEditing] 
}); 
+0

你能帮助我在ExtJS的 – meow 2013-02-18 06:18:54

+0

您可以使用“record.set(值)”,其中值将是新值更新单元I'M新。 – 2013-02-18 06:47:36

回答

0

基本上,它取决于你的服务器端。如果您想通过Ajax请求保存,你应该使用这样的事情:

Ext.Ajax.request({ 
    url: '/Some/Save', 
    method: 'POST', 
    params: { 
     'RecordId': record.get('Id'), 
     'RecordValue': record.get('Value') 
    } 
}) 

您应该使用上面的代码,你if语句中:

if (record.dirty) { 
    //Save code 
} 

你也可以在一个组合中的所有行大对象,并在请求中使用它。

至于单元格编辑器插件,它被设计为自动保存记录。您应该使用validateedit事件。文档在这里:http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.grid.plugin.CellEditing-event-validateedit

2

你不需要通过ajax调用保存sencha会为你做所有的检查和保存。

for (var i = 0; i <grid.store.data.items.length; i++) { 
     var record = grid.store.data.items [i]; 
     if (record.dirty) { 
      ... 
     } 
} 

这部分可以通过store.sync();

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.AbstractStore-method-sync

被替换如果您要发送的数据更新到另一个网址,然后阅读,你可以用稳重的API对象配置代理一个url参数。

proxy: { 
    type: 'ajax', 
    api: { 
     read : 'getLab.php', 
     create : 'setLab.php', 
     update : 'setLab.php', 
     destroy : 'deleteLab.php' 
    } 
    reader: { 
     type: 'json' 
    }, 
    writer: { 
     type: 'json' 

    } 
}