2010-09-14 87 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 
}