ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

No Option to load the table with selected rows initially

Open raftalks opened this issue 13 years ago • 19 comments

I think an option to specify pre-selected items after loading table data will be very useful. What do you say? now, I am doing a loop of the data and setting an extra attribute to each data item to enable the selection, if this is done via an option, it will make life easier.

raftalks avatar Dec 31 '12 17:12 raftalks

I could see a option to set the first item in the grid as selected on load. However, what would be the use case?

what kind of loop are you doing? the thing that tells the grid if the row entity is selected is entity["__ng_selected__"] being set to true the corresponding row object binding is row.selected and they are kept in sync by the grid. As the grid builds rows it sets row.selected to whatever the state of that property is.

orneryd avatar Dec 31 '12 19:12 orneryd

Hi @timothyswt , Happy New Year!!!, Yes I am looping over each row of the table data and setting manually "ng_selected" to true. The use case is a master table of workgroups, and each workgroup is setup to have employees selected from the employees table. something like this screenshot attached. screenshot

raftalks avatar Dec 31 '12 22:12 raftalks

Ok, then my question would be how would you select them via an option? Or how would you want the option to look?

On Monday, December 31, 2012, Raf wrote:

Hi @timothyswt https://github.com/timothyswt , Yes I am looping over each row of the table data and setting manually ng_selected to true. The use case is a master table of workgroups, and each workgroup is setup to have employees selected from the employees table. something like this screenshot attached. [image: screenshot]https://f.cloud.github.com/assets/788192/37013/1492b54e-539b-11e2-92b8-a7b409d5672f.png

— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/ng-grid/issues/76#issuecomment-11785277.

-Tim Sweet

orneryd avatar Dec 31 '12 23:12 orneryd

I think this can be discussed for a better way, but as of now, I can only think about the following ways to do it.

Option A: setting up primary field and passing array of values to identify each row to be selected by primary field


$scope.items = [{id:1, username:'mickymouse' },{id:2, username:'superman' }, ..data in json object array ...];

$scope.checkedItems = [1,3];

$scope.selectedItems = [];

$scope.gridOptions = {

          data: 'items',
          primaryField: 'id',
          initialSelected: $scope.checkedItems,
          columnDefs: [
                {field:'username', displayName: 'Username'},
          ],
         selectedItems: $scope.selectedItem
}

Option B: Initially putting the objects to be selected in to selectedItems option, but this has to bind like two way binding, if more items selected, the $scope.selectedItems will have them, or if $scope.selectedItems have modified by angular, the changes will trigger and be applied to the table.


$scope.items = [{id:1, username:'mickymouse' },{id:2, username:'superman' }, ..data in json object array ...];

$scope.selectedItems = [{id:2, username:'superman' }];

$scope.gridOptions = {

          data: 'items',
          columnDefs: [
                {field:'username', displayName: 'Username'},
          ],
         selectedItems: $scope.selectedItem
}

raftalks avatar Jan 01 '13 11:01 raftalks

I would lean towards Option B.

With javascript, the indexOf function for arrays that have objects only works with references to the object you want to find. So for selectedItems, instead of giving a different object that is the same of the object you want to find, you would want something like:

$scope.selectedItems = [$scope.items [1]];

Since the objects in the array could have nested objects, if we didn't want to use the reference of the object we would have to create a generic function that would check every property within every object. More can be seen here:

http://stackoverflow.com/questions/12604062/javascript-array-indexof-doesnt-search-objects

If you had a function in your controller that does the checks and then you just push them to the selectedItems, that would work. Like this:

http://plnkr.co/edit/zYkFYF96Cns0kYho7tYu?p=preview

This is what I would think is the best solution.

This is not the current ng-grid that is checked in, so let me know if this is something along the lines that you wanted and I will check it in to main.

jonricaurte avatar Jan 03 '13 20:01 jonricaurte

Hi @jonricaurte :+1:

I agree to your idea, and the Plunker seems to work but I remember trying to do the same in which some how didn't worked for me before.

But I note that you have mentioned it is not yet released in current ng-grid? Definitely this is what I think that has to be there if it is not. :)

raftalks avatar Jan 03 '13 20:01 raftalks

@jonricaurte I look forward to see this feature released in current version, when can I expect to see it? :)

raftalks avatar Jan 06 '13 06:01 raftalks

The plunker-Link above doesn't really work. First of all, the grid is defined to 'multiSelect: false' (so more than one selected item shouldn't be allowed). In addition to that, the behavior is the same as in my example: http://jsfiddle.net/m48u/eC2dS/4/

By selecting any other row than the two preselected the grid selects and deselects only the "newly clicked" ones. right after selecting one of the preselected items it kind of disappears from the selected items. pretty strange

m48u avatar Jan 07 '13 12:01 m48u

@m48u I do note the issue having multiSelect set a false while selecting multiple rows, however as of having initially selected rows and selecting new rows, the preselected items does exist and it only is not working in your jsfiddle as it is slightly different.

See if this works, the selected items list is below the table: http://plnkr.co/edit/aPrqVu74pQHLuPLJEEY1

raftalks avatar Jan 07 '13 13:01 raftalks

Sorry for the late response you guys, been a little busy. I updated that version of ng-grid with preselectable rows. If you go to the same plunker, you can test it out:

http://plnkr.co/edit/zYkFYF96Cns0kYho7tYu?p=preview

Let me know what you think.

jonricaurte avatar Jan 07 '13 19:01 jonricaurte

@jonricaurte @timothyswt Thanks guys :100:

raftalks avatar Jan 08 '13 07:01 raftalks

OK, we are changing this actually. The old model was unsustainable and overly complex. Instead of appending "ng_selected" to your data model we are going to simply append the entire row object as a property "ngGridRow" to each entity. In addition, we are also creating the row objects on data load or change instead of when scrolling. That way there is only one place where the selection resides and that is dataItem["ngGridRow"].selected and programmatic selections are much more simplified.

orneryd avatar Feb 05 '13 03:02 orneryd

@timothyswt wow, I look forward to explore the changes. I hope this will also simplify the issue #123

raftalks avatar Feb 05 '13 07:02 raftalks

@timothyswt I couldn't see this plunkr working with latest version 1.7.0, http://plnkr.co/edit/zYkFYF96Cns0kYho7tYu?p=preview

I can see the count of the selected items but the rows are not highlighted.

raftalks avatar Feb 06 '13 18:02 raftalks

This is the new way for selecting items using 1.7.1:

http://plnkr.co/edit/KemYS9?p=preview

I added the same example to the examples on the landing page:

http://angular-ui.github.com/ng-grid/

On Wed, Feb 6, 2013 at 10:37 AM, Raf [email protected] wrote:

@timothyswt https://github.com/timothyswt I couldn't see this plunkr working with latest version 1.7.0, http://plnkr.co/edit/zYkFYF96Cns0kYho7tYu?p=preview

I can see the count of the selected items but the rows are not highlighted.

— Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/ng-grid/issues/76#issuecomment-13197561.

jonricaurte avatar Feb 07 '13 01:02 jonricaurte

@jonricaurte Thanks for adding the example. :+1:

raftalks avatar Feb 07 '13 07:02 raftalks

The example seems to work only if you don't sort the grid first. If you sort by name and then hit Select Enos Row, Tiancum actually gets selected. I opened a Github ticket on this.

Would it make sense for selectItem to take an item and always match up to that item instead of taking an index?

elirov avatar Apr 30 '13 14:04 elirov

I'm doing this and it works

$scope.SelectedEvent = [];

$scope.gridOptions = { data: 'Events', showGroupPanel: true, jqueryUIDraggable: true, selectedItems: $scope.SelectedEvent, multiSelect: false };

$http.post("/Events/", SearchCrieteria).success(function (response) { $scope.Events = response; if ($scope.Events.length > 0) $scope.SelectedEvent[0] = $scope.Events[0]; else $scope.SelectedEvent[0].EventId = 0; });

The trick is to set $scope.SelectedEvent[0] and not $scope.SelectedEvent.

buddishh avatar Jun 28 '13 21:06 buddishh

Btb Sorting of the columns is having a bug, the selected item changes on the grid when clicking sort repeatedly. This issue has nothing to do with selecting the first row I believe.

buddishh avatar Jun 28 '13 21:06 buddishh