Assign default value to multiselect dropdown using angularjs

↘锁芯ラ 提交于 2019-12-10 18:30:14

问题


I am using this plugin http://dotansimha.github.io/angularjs-dropdown-multiselect/#/

If you check the demo part in this page, please use the ' Smart Button Text ' Demo as i am using that.

In this demo, if you select any users, those names will be displayed in the bar.

Now, i want to give default value to it. So as soon as the page loads, there will be a default value present in this multiselect dropdown.

In normal case, i can use ng-init. What should i do in this case ?

Can someone shed some light here please.....


回答1:


Here's a fiddle that does what you want to achieve: https://jsfiddle.net/etfLssg4/

A long summary of that fiddle is as follows:

    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;
        }
    };

If the default selection were to be set in the following manner (as you probably did):

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

It wouldn't work because, for instance, the following comparison evaluates to false:

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

In answer to your question -

wat if i need to update the dropdown with values i get from a ajax call. for e.g. after ajax call, i get a response in an object stating id 3 is to be selected. how do i bind the response to the dropdown and let the user see the updated value

?

The solution to the issue in question can be resolved as follows:

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.



回答2:


I use the same library. Following works for me:

<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'}
  ]
};

EDIT:

Here is working Plunker I'm pretty sure you don't have lodash.js available. Just add <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.0/lodash.js"></script> to your html.




回答3:


Have you tried initializing the model with the default value in your controller? Something like this:

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



回答4:


I look at document.

I think ,

 $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))


来源:https://stackoverflow.com/questions/34779136/assign-default-value-to-multiselect-dropdown-using-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!