1

I have two multi select lists.

On click/select of any item from 1st list, I want to remove the selected item from 1st list and to move the same to 2nd list and vice versa.

I have tried ng-change and ng-click but not getting idea.

Below is my code

1st List

<select style="min-height:200px"
        multiple class="form-control"
        ng-model="listOneItem"
        ng-options="listOneItem.Name for listOneItem in listOneItems track by listOneItem.Name"
        ng-change="moveItemsToSecondList();"></select>

2nd List

 <select style="min-height:200px"
            multiple class="form-control"
            ng-model="listTwoItem"
            ng-options="listTwoItem.Name for listTwoItem in listTwoItems track by listTwoItem.Name"
            ng-change="moveItemsToFirstList();"></select>

Angularjs

  $scope.moveItemsToSecondList= function () {
    alert(listOneItem[0].Name);
  };

  $scope.listOneItems= [{
    Name: 'Independence Day'
  }, {
    Name: 'Labor Day'
  }, {
    Name: 'Thanksgiving Day'
  }, {
    Name: 'Chrismas Day'
  }];

I didn't write functionality to move yet because I am not able to call my methods. i.e. I just put an alert in the method as shown.

7
  • Is your alert shows anything? Commented Sep 22, 2015 at 9:22
  • no method didn't call yet :( Commented Sep 22, 2015 at 9:23
  • guys any idea please... Commented Sep 22, 2015 at 9:54
  • ng-change should fire. Please show more code, especially your controller function and that you specify this correctly in the template. Is your select even populated with your array? Commented Sep 22, 2015 at 9:57
  • 1
    ngChange works fine with ´multiple´. I checked it. Commented Sep 22, 2015 at 10:59

1 Answer 1

3

Hi I have created jsfiddle demo here I have added some logic in it that my help you http://jsfiddle.net/qjcqwhsw/1/

$scope.moveItemsToSecondList= function (items) {
    for(var i=0; i<items.length; i++){
        var index = $scope.listOneItems.indexOf(items[i]);
        $scope.listTwoItems.push(items[i]);
        $scope.listOneItems.splice(index, 1);            
    }

};
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting -1 index always by using your code. Can you please check if its fine
can you provide details plesae

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.