Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign default value to multiselect dropdown using angularjs

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.....

like image 318
Ayesha Avatar asked Sep 27 '26 00:09

Ayesha


2 Answers

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.
like image 174
Igwe Kalu Avatar answered Sep 28 '26 16:09

Igwe Kalu


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.

like image 24
oKonyk Avatar answered Sep 28 '26 14:09

oKonyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!