简体   繁体   English

AngularJS设置多选下拉列表的值

[英]AngularJS set values of multi-select dropdown

I have a multi select dropdown that functions as appropriate when setting values, but once set, I need to display what has been selected in an update form. 我有一个多选下拉列表,在设置值时可以正常工作,但是一旦设置,我需要显示在更新表单中选择的内容。 My values are stored in a DB (SharePoint) accessible over REST. 我的值存储在可通过REST访问的DB(SharePoint)中。 Here is an example REST output with multiple IDs of my array: 这是一个示例REST输出,其中包含多个我的数组ID:

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

Here is my select function, including retrieving the variable from REST: 这是我的select函数,包括从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: 这是我的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>

EDIT: Using id (inspired by yvesmancera) in ng-model would greatly reduce the complexity - You don't need to pre-process your options and input array anymore, just plug it in and done! 编辑:在ng-model中使用id(灵感来自yvesmancera)将大大降低复杂性 - 您不需要预先处理您的选项和输入数组,只需将其插入并完成即可!

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

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

Note: normally we would pre-populate ng-options into an array to preserve the order of the options, if the original data is an object. 注意:如果原始数据是对象,通常我们会将ng-options预先填充到数组中以保留选项的顺序。 But since you use orderBy, you can use the object directly as ng-options. 但是,由于您使用orderBy,因此可以将对象直接用作ng-options。

fiddle 小提琴


Outdated: 已过期:

You need to point to the same object in ng-options for them to get selected on load. 您需要在ng-options中指向相同的对象,以便在加载时选择它们。

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

Try changing your ng-options to something like: 尝试将ng-options更改为:

<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 make this line change in your controller: 并在您的控制器中进行此行更改:

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

Edit for multiple selection: 编辑多个选择:

<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 selected to $scope.selectedCategoriesIds eg: 在您的控制器中,将您想要选择的项目添加到$ scope.selectedCategoriesIds,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM