isteven / angular-multi-select

A multi select dropdown directive for AngularJS. Allows you to use HTML tags and CSS in the data. Requires only AngularJS and nothing else.
isteven.github.io/angular-multi-select
MIT License
1.08k stars 518 forks source link

Problem with idx #449

Closed ubcent closed 7 years ago

ubcent commented 8 years ago

Hi! I have trouble with inputModelIndex. In my array index is choosing wrong. I solved this by comparasion unique fields, but this is not good way. Thanks.

P.S. Sorry for my english.

isteven commented 8 years ago

@ubcent ,

I'm not sure I understand the problem. Perhaps you can replicate it in JsFiddler or Plunker.

But if you're happy with the solution, then I guess it's OK.

Arrekusu commented 8 years ago

Hi, First of all thanks for this great directive )

I cannot create Plunker for this right now, but I think I'm having a similar problem. I guess the problem is in this snippet (when we have a grouping feature enabled):

// we refresh input model as well
    inputModelIndex = $scope.filteredModel[ j ][ $scope.indexProperty ];
    $scope.inputModel[ inputModelIndex ][ $scope.tickProperty ] = true;

We're getting entry index from filteredModel but it is not correct index for inputModel.

In my case, i'm getting the tickProperty = true added for wrong elements in inputModel (e.g. my group elements) which should not happen.

My initial inputModel (As you see, my first group is empty in this case):

[
    {
        "name": "Group1",
        "group": true
    },
    {
        "group": false
    },
    {
        "name": "Group2",
        "group": true
    },
    {
        "name": "Entry1",
        "type": "t1",
        "selected": false
    },
    {
        "name": "Entry2",
        "type": "t2",
        "selected": false
    },
    {
        "group": false
    }
]

After clicking on group element, instead of adding "selected": true for Entry1 and Entry2, the logic changes the first end of group and beginning of Group2.

I was able to brute force this like that but i guess there's got to be a better solution:

// we refresh input model as well
  inputModelIndex = $scope.filteredModel[ j ][ $scope.indexProperty ];
  $scope.inputModel.forEach(function(entry){
        if (entry[$scope.indexProperty] === inputModelIndex){
           entry[ $scope.tickProperty ] = true;
        }
  })

Hope this helps

ubcent commented 8 years ago

@Arrekusu ,

Yes, thanks, your problem description is more detailed. I found this problem when I started using large samples. Soon I will replicate problem in jsFiddle.

HyczZhu commented 8 years ago

I think the root cause is how index is calculated in $scope.prepareIndex function:

All the cases using indexProperty are treating it as the index of inputModel items, but its value is actually calculated from filteredModel.

When inputModel is updated WHILE search is not empty, the index of filteredModel items will not be the same the the ones in inputModel, which will cause this problem: when I use group selection and select some items, some other items will be selected mistakenly.