【问题标题】:Assign default value to multiselect dropdown using angularjs使用angularjs为多选下拉列表分配默认值
【发布时间】:2016-04-19 03:53:21
【问题描述】:

我正在使用这个插件http://dotansimha.github.io/angularjs-dropdown-multiselect/#/

如果您查看此页面中的演示部分,请使用我正在使用的“智能按钮文本”演示。

在此演示中,如果您选择任何用户,这些名称将显示在栏中。

现在,我想给它默认值。因此,一旦页面加载,这个多选下拉菜单中就会出现一个默认值。

在正常情况下,我可以使用 ng-init。在这种情况下我该怎么办?

有人可以在这里阐明一下吗.....

【问题讨论】:

  • @DeblatonJean-Philippe- 对不起,琼。我没明白你的意思
  • @DeblatonJean-Philippe- 我可以使用 $scope.value="34" 来分配初始状态。但是这种类型的分配不适用于这个插件......
  • 提供demo供大家测试

标签: javascript jquery angularjs multi-select


【解决方案1】:

这是一个你想要实现的小提琴:https://jsfiddle.net/etfLssg4/

该小提琴的详细摘要如下:

    var items = [{
        id: 1,
        label: "David"
    }, {
        id: 2,
        label: "Jhon"
    }, {
        id: 3,
        label: "Lisa"
    }, {
        id: 4,
        label: "Nicole"
    }, {
        id: 5,
        label: "Danny"
    }];

    $scope.example13data = items;

    // here we set the default selections as 'Lisa' and 'Danny'.
    // The point you had missed is that both selection array and options array
    // should have elements with matching references.
    $scope.example13model = [items[2], items[4]];

    $scope.example13settings = {
        smartButtonMaxItems: 3,
        smartButtonTextConverter: function(itemText, originalItem) {
            if (itemText === 'Jhon') {
                return 'Jhonny!';
            }

            return itemText;
        }
    };

如果要按以下方式设置默认选择(您可能已经这样做了):

$scope.example13model = [{
    id: 3,
    label: "Lisa"
}, {
    id: 5,
    label: "Danny"
}];

这是行不通的,因为例如,下面的比较结果为假:

items[2] === { id: 3, label: "Lisa" }; // false!

回答你的问题 -

如果我需要使用从 ajax 获得的值更新下拉列表 称呼。例如在ajax调用之后,我在一个对象中得到一个响应 id 3 将被选中。我如何将响应绑定到下拉列表和 让用户看到更新后的值

?

该问题的解决方法如下:

var items = [/* as before, this is the list of base options */];
...
...
var dataFromAjax = [/* data here */];
var selection = items.filter(function(item){
    // check if the item matches any one in the ajax data
    return dataFromAjax.some(function(dataItem){
        // assuming the `id` property is unique
        return item.id === dataItem.id;
    });
});
// at this point `selection` is an array with elements that are references to selected option items.

【讨论】:

  • 好吧,当我检查 Git hub 项目时,我发现该插件对它有依赖。
  • 如果你使用 node 或 bower 作为你的包管理器,它一定已经自动为你解决了这种依赖关系(可能为什么之前没有注意到)
  • 那么您应该在选项中找到与您从 ajax 调用获得的日期等效(或匹配)的参考
  • 有了对angularjs的理解,你应该知道示例代码的每一位应该放在哪里了。
  • 根据您之前的问题 - 在这种情况下,您必须将结构传播到这样的标记:options="obj.example13model",就是这样。
【解决方案2】:

我使用相同的库。 以下对我有用:

<div ng-dropdown-multiselect options="data.options" selected-model="data.preselected"></div>

$scope.data = {
  options: [
    {id: 1, label: 'one'},
    {id: 2, label: 'two'},
    {id: 3, label: 'three'},
    {id: 4, label: 'four'}
  ],
  preselected: [
    {id: 2, label: 'two'},
    {id: 3, label: 'three'}
  ]
};

编辑

这里正在工作Plunker 我很确定您没有可用的lodash.js。 只需将&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.0/lodash.js"&gt;&lt;/script&gt; 添加到您的html。

【讨论】:

  • @oKonyk- 不,它给出了一个错误,指出“无法读取未定义的属性'长度'。并且下拉栏中显示的数据是 {{getButtonText()}}
  • 请检查我的编辑,这是可行的解决方案:plnkr.co/edit/eIK7p6kqunptUTcDQ0G4?p=preview。再次不要忘记添加 lodash.js
【解决方案3】:

您是否尝试过使用控制器中的默认值初始化模型?像这样的:

$scope.example13model = [{"id": 1}]

【讨论】:

  • 它没有用。在下拉选项卡中给出一个空白字段。
【解决方案4】:

我在看文档。

我认为,

 $scope.example1data = new Array({id: 1, label: "David"}, {id: 2, label: "Jhon"}, {id: 3, label: "Danny"});



$scope.example1model = new Array();
 //elements that we want to checked
 $scope.example1model.push( $scope.example1data[0])
 $scope.example1model.push( $scope.example1data[1))

【讨论】:

  • 对不起,我没明白你在最后一段中的意思。
  • 我的意思是您应该将 example1data 中的元素之一设置为其模型。否则下拉列表填充空白
  • 试过但没用。仍然显示空白。 $scope.example1model = $scope.example1data[0];它不起作用
  • :( yunus 没用。你可以在你的本地机器上尝试一下,看看你是否可以设置一个默认值
  • @Ayesha,我做了一个工作小提琴。检查我的答案以查看解释。如果您需要更多说明,请询问。
猜你喜欢
  • 2013-11-23
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多