axel-zarate / js-custom-select

Custom auto-complete select box for AngularJS and Bootstrap
MIT License
68 stars 59 forks source link

How to pass index value in custom-select-options #42

Closed sharathm89 closed 7 years ago

sharathm89 commented 7 years ago
<div ng-repeat="guest_detail in guest_details track by $index">
    <div custom-select="g for g in guest_names | filter: $searchTerm"
        ng-model="offlineForm.name[$index]" custom-select-options="guestNameOptions"></div>
</div>

Above is my code and i have a scenario where i need to pass the loop's index to guestNameOptions. Is it possible or any other way is there where i can capture the index and handle in onSelect, onAdd callbacks.

axel-zarate commented 7 years ago

The only solution I can think of is creating a new options object for each custom select, and passing the index to the factory function; something like:

<div ng-repeat="guest_detail in guest_details track by $index">
    <div custom-select="g for g in guest_names | filter: $searchTerm"
        ng-model="offlineForm.name[$index]" custom-select-options="getGuestNameOptions($index)"></div>
</div>

And in your controller:

$scope.getGuestNameOptions = function (index) {
    return {
        onSelect: function (item) {
            // here you have access to the index value
        },
        onAdd: function (text) {
            // here you have access to the index value
        }
    };
};
sharathm89 commented 7 years ago

thanks it worked.