I have a requirement where I need to work with two drop-down lists that contain the same array elements. When an element is selected from list1 (part1), it should be removed from list2 (part1). Conversely, if the element is unselected in list1, it should appear back in list2.
To address this issue, I attempted the following JavaScript code:
Controller.js
$scope.list1 = [{"columnName": "part1", value: "obj1"},
{"columnName": "part2", value: "obj2"},
{"columnName": "part3", value: "obj3"},
{"columnName": "part4", value: "obj4"},
{"columnName": "part5", value: "obj5"}]
$scope.list2 = [{"columnName": "part1", value: "obj1"},
{"columnName": "part2", value: "obj2"},
{"columnName": "part3", value: "obj3"},
{"columnName": "part4", value: "obj4"},
{"columnName": "part5", value: "obj5"}]
$scope.getColumn = function(column) {
$scope.indexOfColumn = $scope.list2.indexOf(column);
$scope.removedColumn = $scope.list2.splice($scope.indexOfColumn, 1)
console.log($scope.indexOfColumn, 'column index', $scope.removedColumn)
}
And here is the corresponding HTML markup:
<form>
X-axis <select ng-model="xcolumn" ng-options="l1.columnName for l1 in list1" ng-change="getColumn(xcolumn)">
<option value=""></option>
</select>
Y-axis
<select ng-model="ycolumn" ng-options="l2.columnName for l2 in list2" ng-change="getColumn()">
<option value=""></option>
</select>
</form>
For further reference, you can view my implementation on this fiddle link: http://jsfiddle.net/soumyagangamwar/9ra59ptb/
In summary, the functionality involves ensuring that selecting or deselecting an element updates the contents of both lists appropriately. If any element is deselected, it should reappear in both lists. Any guidance on refining this solution would be greatly appreciated.