AngularJS setting dropdown menu values ​​with multiple choices

I have a popup menu with multiple selections that works when needed, when needed, but after installation I need to display what was selected in the update form. My values ​​are stored in a database (SharePoint) accessible via REST. Here is an example REST output with multiple IDs of my array:

"CatId": [
    18,
    80,
    84
],

      

Here is my select function including retrieving a variable from REST:

var currentCatValue = results.CatId;

 $scope.categoryValues = [];

    appCatList.query(function (categorydata) {
        var categoryValues = categorydata.value; // Data is within an object of "value", so this pushes the server side array into the $scope array

        // Foreach type, push values into types array
        angular.forEach(categoryValues, function (categoryvalue, categorykey) {

            $scope.categoryValues.push({
                label: categoryvalue.Title,
                value: categoryvalue.ID,
            });
        })
        var currentDetailIndex = $scope.categoryValues.map(function (e) { return e.value; }).indexOf(currentCatValue);
        $scope.vm.selectedCategory = $scope.categoryValues[currentDetailIndex];
    });

      

Here is my HTML:

<select class="form-control" id="Event_Cat" data-ng-model="vm.selectedCategory"
                                data-ng-options="opt as opt.label for opt in categoryValues | orderBy:'label'" required>
                            <option style="display:none" value="">Select a Category</option>
                        </select>

      

+3


source to share


2 answers


EDIT: Using id (inspired by yvesmancera) in ng-model will greatly reduce the complexity - you no longer need to preprocess your parameters and input array, just plug it in and you're done!

<select multiple ng-model="currentCatValue" ng-options="opt.ID as opt.Title for opt in categoryValues">

$scope.currentCatValue = currentCatValue;
$scope.categoryValues = categoryValues;

      

Note: we usually pre-fill ng-options into an array to preserve the order of the options if the original data is objects. But since you are using orderBy, you can use the object directly as ng-options.

violin




Deprecated:

You need to specify the same object in ng variants so that they can be selected on load.

$scope.categoryValues = [];
$scope.vm.selectedCategory = [];

angular.forEach(categoryValues, function (categoryvalue, categorykey) {
    var category = {
        label: categoryvalue.Title,
        value: categoryvalue.ID,
    }      

    $scope.categoryValues.push(category);

    if (currentCatValue.indexOf(parseInt(category.value)) != -1) {
        $scope.vm.selectedCategory.push(category);
    }
});

      

+5


source


Try changing your ng options to something like:

<select class="form-control" id="Event_Cat" data-ng-model="vm.selectedCategory" data-ng-options="opt.id as opt.label for opt in categoryValues | orderBy:'label'" required>
      <option style="display:none" value="">Select a Category</option>
</select>

      

And change this line in your controller:

$scope.vm.selectedCategory = $scope.categoryValues[currentDetailIndex].id;

      



Edit for multiple choice:

<select class="form-control" id="Event_Cat" data-ng-model="selectedCategoriesIds" data-ng-options="opt.id as opt.label for opt in categoryValues | orderBy:'label'" required multiple>
      <option style="display:none" value="">Select a Category</option>
</select>

      

In your controller, add the items you want to select to the $ scope.selectedCategoriesIds value, like this:

$scope.selectedCategoriesIds = [];
$scope.selectedCategoriesIds.push('18');
$scope.selectedCategoriesIds.push('80');

      

+1


source







All Articles