Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

ngModel formatter function removes initial values #192

Open
mpolichette opened this issue Sep 12, 2014 · 9 comments
Open

ngModel formatter function removes initial values #192

mpolichette opened this issue Sep 12, 2014 · 9 comments

Comments

@mpolichette
Copy link

This could be a quick answer.

Is it be design that the formatter function will remove (not show in UI) values which may be on the model array, if they're not provided in the ui-select-choices?

The way I'm using ui-select, sometimes I will have some initial selected tags which are not present in the select choices. This is ok for my data, however, the formatter will remove them and never show them. It will also force them out of the model the first time it updates $modelValue.

If this is by design, ok. :(
Otherwise this is a bug report/feature request. :) and I would be happy to chip in some time and work on solving it.

Thanks for the cool project!

@mpolichette mpolichette changed the title ngModel formatter function removes current values ngModel formatter function removes initial values Sep 12, 2014
@dimirc
Copy link
Contributor

dimirc commented Sep 12, 2014

Can you setup a Plunker so I can check and try to fix if it's a bug?

@mpolichette
Copy link
Author

Yes,
http://plnkr.co/edit/Nm4dSdmIXLV2EQlA00Wx?p=preview
This plunker shows the issue. As you can see in the demo.js file, I've added a few extra strings, [..., 'Car', 'Charlotte', 'Miami'] to the initial $scope.multipleDemo.colors array. However, those values are not present in the $scope.availableColors array, and therefore are not rendered in the ui-select.

Granted, those are not colors, :P, but they are present values in the initial array.

Being that displaying values is a separate task from validation, I would think it would be fine to render values which are not part of the select choices.

This will especially be an issue when considering the possible feature of allowing select choices as well as free-form text entry.

Let me know what you think!

@wald-tq
Copy link

wald-tq commented Sep 18, 2014

I think I have a issue which is related to this.

I get a initial value from a ngResource and assign it via ng-model to the ui-select. The ui-select uses a refresh method to fetch the items from a server.

Now the items in ng-model are not in the initial array returned by the server as I am filtering server side.

The difference to the issue from @mpolichette is that I am using Objects instead of strings.

@wald-tq
Copy link

wald-tq commented Sep 18, 2014

At the moment I am using following workaround:

link-function from my select directive:

dir.link = function ($scope: tqAnsprechpartnerSelectMultipleDirectiveScope) {

    $scope.items = [];

    $scope.$watch('tqModel', function (newValue, oldValue) {
        if (newValue === oldValue) {
            return;
        }

        if ($scope.tqModel.length > 0) { 
            // we have to add the items from the tqModel to the items.
            // see: https://github.com/angular-ui/ui-select/issues/192
            $scope.items = $scope.items.concat($scope.tqModel);
        }
    });

    $scope.refresh = function (name) {
        $scope.items = tqAnsprechpartner.searchByNameAndFirma({query: name});
    };
}

template:

<ui-select style="display: block;" multiple ng-model="$parent.tqModel">
    <ui-select-match placeholder="Namen eingeben...">{{$item.Vorname}} {{$item.Nachname}}</ui-select-match>
    <ui-select-choices repeat="item in items"
                       refresh="refresh($select.search)"
                       refresh-delay="500">
        <div>{{item.Vorname}} {{item.Nachname}}</div>
        <small>
            <strong>{{item.Name_1}}</strong>
        </small>
    </ui-select-choices>
</ui-select>

call to the directive:

<tq-ansprechpartner-select-multiple style="display: block;" tq-model="tempEditValue"></tq-ansprechpartner-select-multiple>

@Yankovsky
Copy link

Same problem.

@tradiff
Copy link
Contributor

tradiff commented Nov 5, 2014

@dimirc Can you confirm that you consider this to be an issue, and not by design? This is a problem for my project in which I am wanting to initialize the control with some values that were selected in a previous session. The list of options is initially empty because I'm querying a rest api based on user input.

User @reifi has a solution to this which he mentioned in #294 (a duplicate of this issue)

@baumandm
Copy link

I ran into this issue too, using multiple and "refresh" to dynamically load choices based on a search string. I need ui-select to keep previously-selected items in the model, even when doing a different search.

The workaround of adding the items in the model to the choices array seems to be working OK.

@zukasmichael
Copy link

is there any updates for this issue ? I'm working on one project now who could have thousands of tags and I can only load them on search pattern so preloading object tags in this case becoming an issue

@dimirc dimirc added this to the 0.10.x milestone Feb 14, 2015
@arnoudhgz
Copy link

Any updates? I would really like to use the enchanchement.....

@wesleycho wesleycho removed this from the 0.10.x milestone Mar 27, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants