Angular-Formly多选框全选

4
我是一名有用的助手,可以将文本翻译成中文。

我在angular-formly中有一个多选字段,其中包含以下选项:

vm.fields = [
    {
    key: 'fruits',
    type: 'multiCheckbox',
    className: 'multi-check',
    templateOptions: {
        label: 'Fruits:',
        options: [
            {
                "name": "ALL",
                "value":"ALL"
            },

            {
                "name": "apple",
                "value":"apple"
            },
            {
                "name": "orange",
                "value":"orange"
            },
            {
                "name": "pear",
                "value":"pear"
            },
            {
                "name": "blueberry",
                "value":"blueberry"
            },
        ],
    },

  },

];

当我选择/取消选择“全部”时,希望所有选项都被选择/取消选择。 例如,如果已选中“全部”,则应该选择所有水果选项(苹果、橙子、梨子、蓝莓)。
如果取消选择“全部”,则不应选择任何水果选项。
如何在angular-formly中启用此行为?
以下是jsbin链接:https://jsbin.com/xololi/edit?html,js,output
3个回答

2
我在这里写了一个可用的示例 https://jsbin.com/dukewac/6/edit?js,output 当函数为$modelValue, fieldOptions, scope, event时,templateOptions.onClick的签名。这发生在运行ngModelAttrsTemplateManipulator时。我在我的函数中利用了这些变量。
其中一些有点hacky,但这部分与angular如何实现复选框以及angular-formly-templates-bootstrap中multiCheckbox类型使用的解决方法有关。
要注意的事项
嵌套键
此示例不适用于嵌套键,但如果更新multiCheckbox类型,则应该适用。这是因为它直接使用数组访问符号访问模型[在此处查看代码](https://github.com/formly-js/angular-formly-templates-bootstrap/blob/a69d69e5c3f6382ea6a6c028c1d8bf76a9b94db3/src/types/multiCheckbox.js)。
硬编码的“全部”选项
该代码还假定“ALL”选项是列表中的第一个选项,并且其值为“ALL”。这可以通过添加引用所有功能的索引和值的templateOption属性来解决。
模型包含“全部”
“ALL”将出现在您的模型中。解决此问题的可能方法是为其定义$parser。
不支持templateOptions.valueProp 没有考虑templateOptions.valueProp,但不应该太难添加。

运行得非常好。你的回答对我来说有很多东西需要学习。感谢你的帮助。 - quietgecko

0

这里是另一个可行的解决方案(实际上是更新了ckniffty的代码),它将ng-click绑定到formly多选框字段,然后调用控制器中的函数:

        ....
        ngModelElAttrs: {
          "ng-click": "update(this.option.value)"
        },
        controller: function ($scope) {
          $scope.update = function (val) {
            var all = 'ALL',
              // field key
              key = $scope.options.key,
              // if true - current checkbox is selected, otherwise - unselected
              selected = $scope.model[key].indexOf(val) !== -1,
              // index of 'ALL' checkbox within model array
              indexOfAll = $scope.model[key].indexOf(all);

            // 'ALL' checkbox clicked
            if (val === all) {
              // on select - select all checkboxes, on unselect - unselect all checkboxes
              $scope.options.value(selected ? $scope.to.options.map(function (option) {
                return option.value;
              }) : []);
            }
            // other then 'ALL' checkbox unselected, while 'ALL' is selected
            else if (!selected && indexOfAll !== -1) {
              // unselect 'ALL' checkbox
              $scope.model[key].splice(indexOfAll, 1);
            }
            // all checkboxes selected except of 'ALL'
      else if (selected && indexOfAll === -1 && $scope.model[key].length === $scope.to.options.length - 1) {
        // select 'ALL' checkbox
              $scope.model[key].push(all);
            }  
          };
        }
        .....

plunker: https://plnkr.co/edit/LWRZSS6HuBmrzSG5fsH9

plunker:https://plnkr.co/edit/LWRZSS6HuBmrzSG5fsH9


0

你有没有解决方案来给多选框添加defaultValue。我可以添加它并在模型中看到它,但在视图(HTML)中它没有显示。 我在jsbin上制作了一个示例,其中包含所有类型的字段以及multiCheckbox... jsbin example 谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接