0

I have a requirement for a select html element that can be duplicated multiple times on a page. The options for these select elements all come from a master list. All of the select elements can only show all of the items in the master list that have not been selected in any of the other select elements unless they just were duplicated.

When you select a new item from a duplicated select element, it seems to select the option after the one you selected even though the model still has the correct one set. This always seems to happen in IE11 and it happens sometimes in Chrome.

I realize this sounds convoluted, so I created a jFiddle example.

Try these steps:

  1. Select Bender
  2. Click the duplicate link
  3. Select Fry (on the duplicated select)
  4. Notice that the one that is selected is Leela but the model still has Fry (id:2) as the one selected

Can anyone tell me how I might get around this or what I might be doing wrong?

Here is the relevant Angular code:

myapp.controller('Ctrl', function ($scope) {
    $scope.selectedIds = [{}];

    $scope.allIds = [{ name: 'Bender', value: 1},
                     {name: 'Fry', value: 2},
                     {name: 'Leela', value: 3 }];

    $scope.dupDropDown = function(currentDD) {
        var newDD = angular.copy(currentDD);
        $scope.selectedIds.push(newDD);
    }
});


angular.module('appFilters',[]).filter('ddlFilter', function () {
     return function (allIds, currentItem, selectedIds) {


         //console.log(currentItem);
         var listToReturn = allIds.filter(function (anIdFromMasterList) {
                if (currentItem.id == anIdFromMasterList.value)
                    return true;

                var areThereAny = selectedIds.some(function (aSelectedId) {
                    return  aSelectedId.id == anIdFromMasterList.value;
                });
                return !areThereAny;
            });
            return listToReturn;
        }
});

And here is the relevant HTML

<div ng-repeat="aSelection in selectedIds "> 
<a href="#" ng-click="dupDropDown(aSelection)">Duplicate</a>
<select ng-model="aSelection.id" ng-options="a.value as a.name for a in allIds | ddlFilter:aSelection:selectedIds">
<option value="">--Select--</option>
</select>
</div>
1
  • Actually looking at this some more it looks like it happens in Chrome as well. Just not all the time. I am going to update the post Commented May 30, 2014 at 11:52

1 Answer 1

0

Hi I have just made a small change in your dupDropDown function as follows

$scope.dupDropDown = function(currentDD) {

        $scope.selectedIds.push({});
    }

Please check if this works for you.

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

4 Comments

I think that would make it so the duplicated dropdown would not have the duplicated value. I sorta need it to have that value...
I am not able to fully understand your requirement.What I understand is, you want your subsequent drop downs not to have already selected values. Is that right?
Only if they were not duplicated. I actually didnt put that in the Fiddle as it works correctly. Only the duplicated ones should have the value of the original ones.
I know this is totally crazy and thanks for taking a look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.