I have an observable array with available items that was bound to a select dropdown box in a grid row. Users can add more rows with drop downs and select items in each of them.
The issue is once an item is selected in a dropdown row, it should not be available in any other dropdowns except the one that selected it. Removing the selected item from the array removes it from all other dropdowns but also it from the selecting dropdown.
Ex:if initially the available array contained [a, b, c, d, e, f]
dropdown 1: selected = a; available [a, c, d, f]
dropdown 2: selected = b; available [b, c, d, f]
dropdown 3: selected = e; available [c, d, e, f]
I'm trying a ko.computed approach that returns an array based on the availableItems PLUS the current selected value. But I'm having some issues getting the computed to update every dropdown.
Rough code snips:
<table>
<tbody data-bind='foreach: item'>
<tr>
<td>
<select data-bind="
options: $root.availableItemsWithFilter,
event: { onchange: changedItem }">
</select>
</td>
</tr>
</tbody>
</table>
self.availableItemsFiltered = ko.computed(function () {
var temp = self.availableItems();
if (currentRowNumber == self.selectedRow()) {
temp.push(self.selectedItem);
}
return temp;
});
self.changedItem = function(item) {
if (self.selectedItem != '')
{
self.availableItems.remove(item);
}
}