【问题标题】:AngularJS ui-select2 multi select not workingAngularJS ui-select2 多选不起作用
【发布时间】:2014-08-03 13:41:59
【问题描述】:

我尝试使用 AngularJS ui-select 演示应用程序:https://github.com/angular-ui/ui-select2/tree/master/demo 进行多选。我尝试的具体示例是具有预定义值的多选。最初,它加载两个预选状态。单击条目时,下拉菜单显示未定义。并且模型具有两个字符的状态名称。我错过了什么吗?

plunker 链接:http://plnkr.co/edit/IeWSZX2MDq1GfXbm3hQB

html:

<input type="text" style="width:300px" ui-select2="multi" ng-model="multi2Value" />

片段:

var states = [
  { text: 'Alaskan/Hawaiian Time Zone', children: [
    { id: 'AK', text: 'Alaska' },
    { id: 'HI', text: 'Hawaii' }
  ]},
  { text: 'Pacific Time Zone', children: [
    { id: 'CA', text: 'California' },
    { id: 'NV', text: 'Nevada' },
    { id: 'OR', text: 'Oregon' },
    { id: 'WA', text: 'Washington' }
  ]}, ...}

$scope.multi2Value = [
    { id: 'CT', text: 'Connecticut' },
    { id: 'DE', text: 'Delaware' }];

$scope.multi = {
    multiple: true,
    query: function (query) {
        query.callback({ results: states });
    },
    initSelection: function (element, callback) {
        var val = $(element).select2('val'),
          results = [];
        for (var i=0; i<val.length; i++) {
            results.push(findState(val[i]));
        }
        callback(results);
    }
};

【问题讨论】:

  • 你能提供一个plunker吗?

标签: angularjs ui-select2


【解决方案1】:

将您的 &lt;input&gt; 字段更改为 &lt;div&gt; 并重试。

html 标记应如下所示:

 <div  style="width:300px" ui-select2="multi" ng-model="multi2Value" />

【讨论】:

  • 嗨,艾伦,感谢您的解决方案,这真的很有帮助。能否请您也说说原因?
【解决方案2】:

您甚至可以使用选择标签。我为此创建了一个 plunker:

HTML:

<select multiple class="full-width" ui-select2="groupSetup" ng-model="activity.act_group_id" ng-init="activity.act_group_id=split_custom(activity.act_group_id,',',1)" data-placeholder="Select Group">
  <option ng-repeat="group in groups | orderBy:'text'" value="{{group.id}}">{{group.text}}</option>
</select>

JS:

$scope.groups = [{"text": "Group1", "id": 2}, {"text": "Group2", "id": 62}, {"text": "Group3", "id": 68}, {"text": "Group4", "id": 74}, {"text": "Group5", "id": 98}, {"text": "Group6", "id": 104}, {"text": "Group7", "id": 107}, {"text": "Group8", "id": 139}, {"text": "Group9", "id": 149}, {"text": "Group10", "id": 154}];
$scope.groupSetup = {
    multiple: true,
    formatSearching: 'Searching the group...',
    formatNoMatches: 'No group found'
};

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

【讨论】:

  • 如何将某些选项设置为默认选中?
  • @PeterPan:去 plunker 看看默认选择了一些组。这是通过在 ng-init 上调用 split_custom js 函数来完成的,该函数选择您要选择的选项。请深入研究代码,如果我能帮助您更多地调试它,请告诉我。
  • 嗨,你能帮我吗,我怎样才能刷新像activity.act_group_id这样的模型
猜你喜欢
  • 1970-01-01
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 2012-12-05
相关资源
最近更新 更多