2013-02-07 42 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'} 
];