KoGrid
KoGrid copied to clipboard
With multiSelect = false, clicking on a selected item does not unselect.
I am finding KoGrid to be very useful. I am also new to github. I can't seem to find where to put this so I figured here. The js I found for this KoGrid is not necessarily from this master as I found the one from the sample code to be more complete.
http://knockout-contrib.github.io/KoGrid/#/examples
Anyway, I made a fix to what I feel is a problem. With the mutliSelect set to false, once a single item is selected, you are unable to de-select it.
Code change: // function to manage the selection action of a data item (entity) self.ChangeSelection = function (rowItem, evt) { grid.$$selectionPhase = true; if (!self.multi) { new --> if (self.lastClickedRow !== rowItem){ if (self.lastClickedRow && self.lastClickedRow.selected) { self.setSelection(self.lastClickedRow, false); } new --> } } else if (evt && evt.shiftKey) {
Now to figure out how to programmatically change the selection to be deselected as I want to use the items in the kogrid as a master list. I want to have the user select from the kogrid, push a button where my code will take the selected items and use them. When done, I want to be able to update the kogrid to de-select the kogrid items.
I experienced the same bug and I think I found a quick fix for this.
In selectionService.js
, change this function
self.setSelection = function(rowItem, isSelected) {
rowItem.selected(isSelected) ;
rowItem.entity[SELECTED_PROP] = isSelected;
if (!isSelected) {
var indx = self.selectedItems.indexOf(rowItem.entity);
self.selectedItems.splice(indx, 1);
} else {
if (self.selectedItems.indexOf(rowItem.entity) === -1) {
self.selectedItems.push(rowItem.entity);
}
}
};
to this :
self.setSelection = function(rowItem, isSelected) {
rowItem.selected(isSelected) ;
rowItem.entity[SELECTED_PROP] = isSelected;
if (!isSelected) {
var indx = self.selectedItems.indexOf(rowItem.entity);
self.selectedItems.splice(indx, 1);
} else {
if (self.selectedItems.indexOf(rowItem.entity) === -1) {
self.selectedItems.push(rowItem.entity);
}
self.lastClickedRow = rowItem;
}
};
It should work.
It looks like when the sort
function is called, rows
are rebuilt and each row calls the setSelection
with it's previous value (backed in entity[SELECTED_PROP]
). Calling setSelection
from Row
does not set the lastClickedRow
property in SelectionService
thus the error.
I'm not submitting a pull request as I did not test in multiSelect and I don't know the code enough to deduce the impact of this modification.