首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向ui添加项目-选择Angular

向ui添加项目-选择Angular
EN

Stack Overflow用户
提问于 2014-10-15 18:38:45
回答 5查看 10.2K关注 0票数 2

我正在使用angular-ui-select,我有一个问题。如何修改ui-select,以便可以手动添加项目-不仅从ng-model中的现有项目中选择,还可以添加新元素。

提前谢谢你

EN

回答 5

Stack Overflow用户

发布于 2015-07-20 02:10:57

最近,我想出了一个解决方案。您可以使用ui-select-choices中的刷新选项来添加一些逻辑并实现您想要的功能。

代码语言:javascript
复制
<ui-select-choices ...
  refresh=”main.refreshResults($select)” 
  refresh-delay=”0"
>
  <span>{{table.description}}</span>
</ui-select-choices>

然后在控制器上

代码语言:javascript
复制
function refreshResults($select){
 var search = $select.search,
   list = angular.copy($select.items), 
   FLAG = -1;

 //remove last user input
 list = list.filter(function(item) { 
   return item.id !== FLAG; 
 });

 if (!search) {
   //use the predefined list
   $select.items = list;
 }
 else {
   //manually add user input and set selection
   var userInputItem = {
     id: FLAG,
     description: search
   };
   $select.items = [userInputItem].concat(list);
   $select.selected = userInputItem;
 }
}

我使用搜索字段($select.search)和一个基本的对象结构id和description来实现它。希望能有所帮助。

Plunker

票数 9
EN

Stack Overflow用户

发布于 2015-06-03 23:22:21

只需将tagging="true“添加到ui-select指令中即可,请参阅演示:

http://plnkr.co/edit/hFxGahJEFIggsL2Wdsli?p=preview

即:

代码语言:javascript
复制
 <ui-select multiple ng-model="multipleDemo.colors" tagging="true" theme="select2"  style="width: 300px;">
    <ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>
    <ui-select-choices repeat="color in peopleAsync | filter:$select.search">
      {{color}}
    </ui-select-choices>
  </ui-select>
票数 4
EN

Stack Overflow用户

发布于 2016-08-02 18:33:42

当你使用一个字符串时,只需添加tagging,但是当你使用一个对象时,你可能会得到一些错误。

在这种情况下,您需要将转换函数指定为tagging。此函数应返回新对象。请参见以下代码:

代码语言:javascript
复制
<ui-select multiple tagging="transformFunction" tagging-label="(custom 'new' label)" ng-model="multipleDemo.colors">
<ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>
<ui-select-choices repeat="color in availableColors | filter:$select.search">
    {{color}}
</ui-select-choices>
</ui-select>

在你的控制器中

代码语言:javascript
复制
transformFunction = function(new_value){ //new value is a string
   //now construct the object and return them.
   var new_object = {};
   new_object.name = new_value;
   new_object.... = ...
   //some http request if you need them.
   //some treatement

   return new_object;
}

请随意参考此示例:

http://plnkr.co/edit/m1SQXUxftBLQtitng1f0?p=preview

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26380468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档