3

How do I configure dgrid and it's store to define whether a row is already selected when the row is rendered?

For example, if my row data is like so:

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

My current code is to loop through the collection after the store's been populated, but I'm sure there must be a more efficient way to do this.

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();
    });
  }
);
Buta
  • 73
  • 1
  • 4

2 Answers2

2

I found this post and wanted to commit about this issue. I wanted to get the first row of data in the dgrid, and this is where I found this post. But it help me in my solution.

In the first column I added a "get" function and was able to find and select the first record. I hope this helps anyone trying to get or select the first record in a 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'}
];
j0k
  • 22,600
  • 28
  • 79
  • 90
1

It seems Selection.js does it the same way https://github.com/SitePen/dgrid/blob/master/Selection.js#L433, but I just got an idea, how to make selection a part of the rendering process:

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"
);

See it in action: http://jsfiddle.net/phusick/stxZc/

phusick
  • 7,342
  • 1
  • 21
  • 26
  • 1
    That did the trick. One note, for others if they're using the dgrid/selector, the **get** method should be added to the selector object and not a regular column as the checkbox/radio will not be checked even though the row is selected. – Buta Feb 07 '13 at 22:29