dgrid icon indicating copy to clipboard operation
dgrid copied to clipboard

Add getSelectedItems function to Selection

Open schallm opened this issue 11 years ago • 5 comments

We need the ability to get the currently selected items out of a grid. I know I can capture the dgrid-select function, but I usually don't care at the time of the selection. I need the information at a later point. It would be nice to have something like the following function as part of Selection:

getSelectedItems: function () {
    var items = [];
    // Iterate through all currently-selected items
    for (var id in this.selection) {
        if (this.selection[id]) {
            items.push(this.row(id))
        }
    }
    return items;
};

schallm avatar Aug 08 '13 17:08 schallm

I think it would be nice, but it is really not necessary, if you need it offen, you can declare your own grid widget by extending original dgrid funcionality

declare([Grid], { 
 getSelectedItems: function () {
    var items = [];
    // Iterate through all currently-selected items
    for (var id in this.selection) {
        if (this.selection[id]) {
            items.push(this.row(id))
        }
    }
    return items;
  }
}

martinerko avatar Aug 08 '13 18:08 martinerko

Sure it is easy to add, which is what I have done, but why have a Selection extension that doesn't give me a way get what is currently selected? I realize that code size is always a factor, but at some point shouldn't certain basic features should be built in?

Also, for future readers, your example would require the Selection extension.

schallm avatar Aug 08 '13 18:08 schallm

There are a couple of reasons why it's not quite as simple as you mention (and thus why it's not implemented), mainly centering around items that aren't loaded.

Firstly, this.row(id) will not be able to retrieve data for any rows that were previously rendered but are no longer rendered (but are still selected), which is possible when using OnDemandList, since it removes rows from the list/grid as the user scrolls far away from them.

Secondly, if you use OnDemandList and the select-all feature of the Selection mixin, if the user has selected all items, then that may include items that are not currently loaded (and thus are either not in the selection object at all yet, or will fail to obtain row data through this.row for the previous reason).

kfranqueiro avatar Aug 09 '13 14:08 kfranqueiro

So is the issue that the grid can't return the Item objects because there may not be an element object for each item? Could that could be null if there is no element connected or maybe always null for the OnDemand*? I really only care about the data anyway. I need to be able to collect all the selected data rows and pass them up to the server.

Or could there be a solution here something like getSelectedData() That returns an array of selected bound data?

schallm avatar Aug 14 '13 02:08 schallm

I was also surprised that there was no function for getting a list of currently selected rows, or at least a list of IDs of currently selected rows. I am doing it myself via the loop mentioned above, but it's very strange to me that a "selection" feature would not have such a method built-in.

I see the point about some of the data not being in memory any more, and I may be missing something about how grids and/or stores work, but it seems like if it's possible for such items to still be selected, then at least their IDs would have to be available. Right?

chrislong avatar Feb 21 '15 04:02 chrislong