2013-02-07 45 views
3

如何配置dgrid及其存儲以定義在渲染行時是否已選擇一行?Dojo dgrid:(Pre)在渲染上選擇行

例如,如果我的行數據是像這樣:

{ 
    id: 1, 
    name: 'Item Name', 
    selected: true 
} 

我當前的代碼是遍歷集合店內的被填充後,但我敢肯定,必須有一個更有效的方法做這個。

var items = [ 
    {id: 1, name: 'Item 1', selected: true}, 
    {id: 2, name: 'Item 2', selected: false} 
]; 

require(
    [ 
    "dgrid/OnDemandGrid", 
    "dgrid/Selection", 
    "dojo/store/Memory", 
    "dojo/_base/declare", 
    "dojo/_base/array" 
    ], 

    function (OnDemandGrid, Selection, Memory, declare, array) { 
    var store = new Memory({ 
     data: items, 
     idProperty: "id" 
    }); 

    var grid = new declare([OnDemandGrid, Selection])({ 
     selectionMode: "multiple", 
     columns: { 
      id: { label: "ID" }, 
      name: { label: "Name" } 
     }, 
     store: store 
     }, "MyGrid"); 

     array.forEach(items, function (item) { 
     if (item.selected) { 
      grid.select(grid.row(item.id)); 
     } 
     }); 

     grid.startup(); 
    }); 
    } 
); 

回答

1

看來Selection.js做同樣的方式https://github.com/SitePen/dgrid/blob/master/Selection.js#L433,但我只是有一個想法,如何讓選擇渲染過程的一部分:

var grid = new declare([OnDemandGrid, Selection])({ 
    selectionMode: "multiple", 
    store: store, 
    columns: { 
     id: { 
      label: "ID", 
      get: function(item) { 
       var grid = this.grid; 
       if (item.selected === true) { 
        grid.select(grid.row(item.id)); 
       } 
       return item.id; 
      }    
     }, 
     name: { label: "Name" } 
    }, 
    "MyGrid" 
); 

看到它在行動:http://jsfiddle.net/phusick/stxZc/

+1

這樣做。值得注意的是,對於其他人,如果他們使用dgrid /選擇器,則應該將** get **方法添加到選擇器對象中,而不是常規列,因爲即使選中該行,checkbox/radio也不會被檢查。 – Buta

2

我發現這篇文章,並希望提交有關此問題。 我想獲取dgrid中的第一行數據,這是我找到這篇文章的地方。但它幫助我解決問題。

在第一列中,我添加了一個「get」函數,並能夠找到並選擇第一條記錄。 我希望這可以幫助任何人獲得或選擇dgrid中的第一條記錄。

var columns = [ 
    { label: "Name", field: '_item', x: null, 
    formatter: lang.hitch(this, this._nameFormatter), 
    get: lang.hitch(this, function(item){ 
     console.log(item) 
     if(!this.x) { 
      this.x = item.id; 
      this.grid.select(item.id); 
      this.detailsPane.setDetails(item.id); 
      return item; 
     } else { 
      return item; 
     } 
     }) 
    }, 

    { label: 'Email', field: 'email', 
    formatter: lang.hitch(this, this._emailFormatter) 
    }, 

    { label: "Phone", field: "phone" }, 
    { label: 'Address', field: 'address' }, 
    { label: 'City', field: 'city' }, 
    { label: 'State', field: 'state' }, 
    { label: 'Zip Code', field: 'zipcode'} 
];