I'm using the angularjs-dropdown-multiselect.js to create a multi-select drop-down. I'm trying to dynamically populate the drop-down with existing data from mysql database. If I hardcode the array of objects in my scope, the drop-down is populated correctly and the glyphicon checks are highlighted, as being checked, and the correct numbers are selected. If I pass in the array of objects from the database, as a variable to the scope, the glyphicon checks are not highlighted next to the selected choices, but the correct number of selected is correct. Any ideas on how to fix this? Here is a link to the code I'm using: http://dotansimha.github.io/angularjs-dropdown-multiselect/#/.
1 Answer
In the backend, wherever you perform the query to get the array of items from the database, try making a loop that will go through the returned items, and push them into an array, one by one.
then make a get call that will receive array as a response from the database.
I hope this is what you work looking for!
6 Comments
E. Moser
When I save the initial results, I save them in a MySQL database field as a string, since the number can change depending on the user. Example: [{id: 1}, {id: 3}]. Then when I pull the data, I convert the string to an array of objects, in JavaScript. It shows the correct number of selected on the drop-down, but the checks are not, checked, by the ones that are supposed to be selected.
Maged Hennawy
Could be an issue with your model then, did you check that?
Maged Hennawy
Would be really helpful if you can post some code, or a plunkr
E. Moser
$scope.exampleData.fruits -- This is the string from the database. I pass it into this javascript to convert to an array of objects. Example: [{id: 1}, {id: 3}] var array = []; var tar = $scope.exampleData.fruits.replace(/^\{|\}$/g,'').split(','); for(var i=0,cur,pair;cur=tar[i];i++){ array[i] = {}; pair = cur.split(':'); array[i][pair[0]] = /^\d*$/.test(pair[1]) ? +pair[1] : pair[1]; }
E. Moser
(Both are exactly shown as [object Object],[object Object]) $scope.example6model = array; -- This correctly shows the number of selected, but no glyphions are checked. $scope.example6model = [{id: 1}, {id: 3}]; -- This correctly shows the number of selected and glyphions are checked. $scope.example6settings = {}; $scope.example6data = [{id:1, label: "Apple"}, {id:2, label: "Peach"}, {id:3, label: "Orange"}]; <div selected-model="example6model" options="example6data" ng-dropdown-multiselect="" extra-settings="example6settings"></div>
|