2013-02-18 86 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' 

    } 
}