Allow manually entered text in ui-select

后端 未结 5 594
北海茫月
北海茫月 2020-12-05 13:24

I am using a select box from ui-select. All is working fine, but I want to allow manually entered text and do not want to restrict the user from the values available in the

5条回答
  •  忘掉有多难
    2020-12-05 13:43

    I think I found a way to allow the user to create new entries. Use the "on-select" attribute to pass a function that takes $select as a parameter as below:

    
    
        {{$select.selected.name}}
        
        

    Then create the function that adds a new entry when the clickTriggeredSelect variable is false:

    $scope.peopleSel= function(sel) {
      if ( sel.search && ! sel.clickTriggeredSelect ) {
        if ( ! sel.selected || sel.selected.name != sel.search ) {
          //Search for an existing entry for the given name
          var newOne= personSearch( sel.search ); 
          if ( newOne === null ) {
            //Create a new entry since one does not exist
            newOne= { name: sel.search, email: 'none', country: 'unknown' };
            $scope.people.push( newOne );
          }
          //Make the found or created entry the selected one
          sel.selected= newOne;
        }
      }
      sel.search= ''; //optional clearing of search pattern
    };
    

    Note that personSearch definition is not provided here. Also this approach of testing the clickTriggeredSelect can be used to allow the user to unselect the field if a blank entry is the preference.

    PVC

提交回复
热议问题