2010-09-14 28 views
9

我在學習如何在管理應用程序中的表上使用EXTJS網格進行一些簡單的CRUD操作。EXTJS +在保存網格後使用數據庫ID更新商店

我有一個簡單的網格,允許某人編輯用戶,商店被定義爲:

 var userDataStore = new Ext.data.Store({ 
      id: 'userDataStore', 
      autoSave: false, 
      batch: true, 
      proxy: new Ext.data.HttpProxy({ 
       api: { 
        read: '/Admin/Users/All', 
        create: '/Admin/Users/Save', 
        update: '/Admin/Users/Save' 
       } 
      }), 
      reader: new Ext.data.JsonReader(
      { 
       root: 'Data',      
       idProperty: 'ID', 
       totalProperty: 'total', 
       successProperty: 'success', 
       messageProperty: 'message' 
      }, [ 
       { name: 'ID', type: 'string', allowBlanks: false }, 
       { name: 'NT_ID', type: 'string', allowBlank: false }, 
       { name: 'EMail', type: 'string', allowBlank: false }, 
       { name: 'Name', type: 'string', allowBlank: false }, 
       { name: 'Enabled', type: 'bool', allowBlank: false }, 
       { name: 'CurrentRoleCode', type: 'string', allowBlank: false}] 
      ), 
      writer: new Ext.data.JsonWriter(
      { 
       encode: false, 
       writeAllFields: true, 
       listful: true 
      }) 
     }); 

這是綁定到一個網格,和我能夠載入和保存用戶沒有問題。保存按鈕看起來是這樣的:

var saveButton = new Ext.Button({ 
      text: 'Save', 
      disabled: true, 
      handler: function() { 
       userDataStore.save();      
       pageState.ClearDirty(); 
       saveButton.disable(); 
      } 
     }); 

但是,創建新用戶時,JSON的POST用戶發佈到同一個REST服務端點爲「更新」,唯一的區別是沒有ID值被張貼(因爲只有在從服務器加載時纔在商店中設置一個)。

這工作,我能夠創建用戶。

保存REST服務使用新的數據庫ID發回已創建的行,並且我假定EXTJS會自動將新生成的數據庫ID綁定到該行。這允許用戶進一步編輯該行,並導致更新而不是插入。

取而代之,該行繼續有一個空白的用戶標識,因此額外的保存會創建另一個新用戶。

因此,要麼:

  • EXTJS應該自動解決生成的行ID的,我只是做了什麼。
  • 我應該手動重新加載網格後,每次保存一個額外的REST調用。

我一直在尋找EXTJS文檔和論壇,但我不清楚正確的做法。

有人可以澄清嗎?

編輯:我試着在JSON中返回Success = True來匹配SuccessProperty,但是這仍然沒有奏效。

編輯#2:到目前爲止,我發現的唯一工作是在保存後執行「userDataStore.reload()」,但是因爲我在保存後返回商店的內容,所以希望EXTJS會理解並更新行值。

回答

-1

您需要使用savebtn處理程序中的新參數重新加載存儲庫 ,比如 store.reload();

當然,你可以添加更多的PARAMS加載行動

2
 
    I've got an idea that may help you. Let't suppose that user added a new 
record in grid, in that moment add a new property newRecOrderNo to the record to 
identify the record after response. When user will post data to server after 
inserting you must get a new ID and associate it to newRecOrderNo 
(like Map<Integer,Integer>). Then return json object like that : 

{ 
    success : true, 
    newIdes : { 
      1 : 23, 
      2 : 34 
    } 
} 
 

Then when you get response do set proper IDs to records: 

    userDataStore.each(function(rec){ 
     if(rec.data.newRecOrderNo){ 
      rec.data.ID = response.newIdes[rec.data.newRecOrderNo]; 
      delete rec.data.newRedOrderNo; 
     } 
    }) 
 
}) 

1

是的,它集ID(以及其他領域,如果服務器返回修改它們的值),如果創建 AJAX後端返回紀錄設置ID,至少在extjs 4.1。您應該返回帶有id設置的插入記錄作爲json字典,在此示例中,root爲'Data',即:

{ 
    "Data": { 
    "ID": 8932, 
    "NT_ID": 28738273, 
    ... 
    "CurrentRoleCode": "aaa", 
    }, 
    "success": true 
}