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

Selecting an item not in the list #1409

Open
tharrington opened this issue Jan 27, 2016 · 13 comments
Open

Selecting an item not in the list #1409

tharrington opened this issue Jan 27, 2016 · 13 comments

Comments

@tharrington
Copy link

how can i select (or add) an item that is not in the list? I.e. when a user starts typing and filters to the point where there isnt a match, an option to create a new entry appears.

 <ui-select ng-model="order.email" theme="bootstrap">
      <ui-select-match placeholder="Search by email...">{{order.email}}</ui-select-match>
      <ui-select-choices repeat="item in customers | filter: $select.search">
        <div ng-bind-html="item.email | highlight: $select.search"></div>
      </ui-select-choices>
    </ui-select>
@user378230
Copy link
Contributor

See wiki: https://github.com/angular-ui/ui-select/wiki/ui-select

option description value default
tagging Enable tagging mode (add new items on the fly). Accepts a string which is a scope function. If your model is an array of objects, this string is required. The function will be passed the new item as a string and should return an object which is the transformed value to be pushed to the items array. string ($scope function) undefined
tagging-label Set a label for tags that appear in the dropdown. Expects a string or false. If false, then tagging happens without new items appearing in the dropdown. If empty or undeclared, tagging-label defaults to (new) string, boolean undefined

Example: https://github.com/angular-ui/ui-select/blob/master/examples/demo-tagging.html

@tharrington
Copy link
Author

I'm getting a bunch of errors with these examples... One is if I don't even type anything and maybe a selection from the dropdown then switch the selection I get an error:

TypeError: g.selected.filter is not a function

code:

<ui-select tagging="tagTransform" ng-model="order.customer" theme="bootstrap">
                  <ui-select-match placeholder="Enter email...">{{$select.selected.email}}</ui-select-match>
                  <ui-select-choices repeat="customer in customers | filter: $select.search">
                    <div ng-if="customer.isTag" ng-bind-html="customer.email +' <small>(new)</small>'| highlight: $select.search"></div>
                    <div ng-if="!customer.isTag" ng-bind-html="customer.email | highlight: $select.search"></div>
        </ui-select-choices>
    </ui-select>
 $scope.tagTransform = function(tag) {
        var newCustomer = {
        first_name: '',
        last_name: '',
        email: tag.toLowerCase()
        };
        $scope.order.email = newCustomer.email;
        return newCustomer;

    }

When I start typing, another error appears:

Uncaught TypeError: Cannot read property 'length' of undefinedd @ select.min.js:7(anonymous function) @ select.min.js:7jQuery.event.dispatch @ jquery.js:4435elemData.handle @ jquery.js:4121

@darioml
Copy link

darioml commented Feb 24, 2016

Did you end up getting this to work?

Looking at a non-multiple select object that lets me dynamically add values to the list. Seems similar here?

@user378230
Copy link
Contributor

There were some fixes that recently went in for single select tagging (#1439)

@darioml are you using the latest version of ui-select with this fix?

@andrecbr
Copy link

andrecbr commented Mar 3, 2016

@user378230 I used the latest version in this plnkr but without success, do you have any idea what's wrong?

@darioml
Copy link

darioml commented Mar 5, 2016

Using the latest (1.4.9 angular, 1.14.9 ui-select) with no luck. My tagging function is not even being called.

@user378230
Copy link
Contributor

Guess tagging with single select isn't currently supported.

PRs would be welcome I'm sure...

@sqwk
Copy link

sqwk commented Apr 5, 2016

Using v0.16.1 and v1.5.3 I do not get any error messages but new elements are not added to the array; they are simply ignored and the input is reset to the value before.

@user378230
Copy link
Contributor

As a workaround you can use multiple select mode with limit="1" #1110

@darioml
Copy link

darioml commented Apr 7, 2016

Have to say that's not a workaround, rather a different feature set. "multiple" changes the UI elements & UX.

I implemented this by using a custom filter. My list of options has an element with ID: new. The value / text for it is changed by my filter.

.filter('allowNew', ['$filter', $filter =>
    (input, optional1) => {
        let filtered = $filter('filter')(input, { name: optional1 });
        if (filtered) { filtered = $filter('filter')(filtered, { id: '!new' }); }

        if (optional1 && optional1.length > 1 && filtered) {
            const newone = $filter('filter')(input, { id: 'new' }, true);
            if (newone) {
                newone[0].name = optional1;
                filtered.push(newone[0]);
            }
        }
        return filtered;
    },
])

Not the best code, but worked.

@solerman
Copy link

solerman commented May 5, 2016

Seemed to works for me setting tagging-label="false" but causes other problems

@nielk
Copy link

nielk commented Jun 3, 2016

I encountered the same issue, my tagging function is not called, PR would be welcome…

aaronroberson pushed a commit that referenced this issue Jul 19, 2016
#1727)

* When in tagging mode, not multiple (taggingLabel === false), selecting
an item by click was instead calling taggingFunc(). This fix checks
for this manual selection, whether ctrl.search is filled or not and
acts accordingly.

Same changes mad in #1439 that were mysteriously lost.
Fixes #1357, #1496 and #1409

* Simplification to combine ctrl.clickTriggeredSelect in one assignment
karthicksundararajan added a commit to karthicksundararajan/ui-select that referenced this issue Jul 20, 2016
…orks

Tagging in single select is not working which has been testing in latest version of angular. Here, I've just copied working version of code from uiSelectMultiple directive and removed some code which causes the problem.

All the test has been passed. Examples are working fine

Closes angular-ui#1409, angular-ui#890
kboga pushed a commit to kboga/ui-select that referenced this issue Nov 29, 2016
…orks

Tagging in single select is not working which has been testing in latest version of angular. Here, I've just copied working version of code from uiSelectMultiple directive and removed some code which causes the problem.

All the test has been passed. Examples are working fine

Closes angular-ui#1409, angular-ui#890
kboga pushed a commit to kboga/ui-select that referenced this issue Nov 29, 2016
…orks

Tagging in single select is not working which has been testing in latest version of angular. Here, I've just copied working version of code from uiSelectMultiple directive and removed some code which causes the problem.

All the test has been passed. Examples are working fine

Closes angular-ui#1409, angular-ui#890
@gine
Copy link

gine commented Sep 27, 2017

@darioml i don't understand how i can use you code. where i must call the filter? can you make me an example?

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