2016-11-23 39 views
0

我用可編輯單元實現了ag-grd-ng2(Angular2的ag-grid),並且我將這些更新傳播到服務器。正確的方法來保存可編輯的Ag網格單元

我這樣做,通過連接線gridOptions.onCellValueChanged事件。

我科拉姆DEF具有以下特性:

  c.editable = true; 
      c.cellEditor = 'richSelect'; 
      c.cellEditorParams = { 
       cellRenderer: o => o.value ? o.value.name : o.value, // use name property from underlying collection to show in dropdown 
       values: list 
      } 
      c.newValueHandler = p => p.data[key] = p.newValue.name; // guessing this needs to be done so that newly selected dropdown item shows only the name in cell 

這個實現的主要問題是,從下拉列表中選擇一個項目將在網格單元,如果底層的異步調用成功立即更新不管。

是否有更好的模式用於更新這個網格,以便在調用失敗時將值恢復到網格中,或者在成功回調之前沒有設置爲新值?

另一個問題是,newValueHandler使新選定的下拉列表項在單元格中正確顯示,但onCellValueChanged不再有權訪問新選擇的項目的ID字段。

我看到的一種可能性是column.newValueHandler。也許有一種方法可以將它用於異步操作,並且只設置成功的新值?

更新,試圖以此作爲一個可能的解決方案:

newValueHandler(p, key){ 
    this.svc.updateProductField(p.data.ID, p.colDef.field, p.newValue.code) // async server update 
     .catch(err => { 
      p.data[key] = p.oldValue; // set the saved value 
      return Observable.throw(err); 
     }) 
     .subscribe(data => { $.notify(p.colDef.headerName + ' saved', 'success'); 
      p.data[key] = p.newValue.name; // set the saved value 
     }) 

} 

更新實際工作,但是UI沒有顯示新的或舊的價值,只是空白。大概沒有設計這樣的工作方式:)

+0

當您設置單元格的值,你需要調用'rowNode.setDataValue(colKey,NEWVALUE)' –

+0

感謝現在試試吧! –

+0

我認爲這整個模式是微弱的..因爲成功或錯誤任何更新將觸發相同的處理程序進入循環..我需要有一些指標,這不是一個用戶更新..整個事情是不可靠的。 –

回答

0

一個可能的解決方案:

設置單元格樣式,以成功或失敗基於服務器的呼叫。這給了用戶一些反饋。

newValueHandler(p, key) { 
    if (p.newValue && p.newValue.code) { 
     var nv = p.newValue.name; 
     this.svc.updateProductField(p.data.ID, p.colDef.field, p.newValue.code) 
      .catch(err => { 
       p.colDef.cellClass = 'ag-etp-cell-failed'; 
       $.notify(p.colDef.headerName + ' update failed', 'error'); 
       return Observable.throw(err); 
      }) 
      .map(o => { 
       p.colDef.cellClass = 'ag-etp-cell-saved'; 
       $.notify(p.colDef.headerName + ' saved', 'success'); 
      }) 
      .subscribe(); 
    } 
    p.data[key] = p.newValue.name || p.newValue; 
} 
相關問題