[英]angular-formly multiCheckbox Select All
I have a multiselect field in angular-formly, with the following options: 我有一个角度形式的多选字段,有以下选项:
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"
},
],
},
},
];
When I select/unselect "ALL", I want all the options to be selected/unselected. 当我选择/取消选择“全部”时,我希望选择/取消选择所有选项。 For Example. 例如。 If ALL is checked, then all the fruits options( apple, orange, pear, blueberry) should be checked as well 如果选中ALL,则还应检查所有水果选项(苹果,橙子,梨,蓝莓)
If I unselect ALL, then none of the fruit options should be checked. 如果我取消选择ALL,则不应检查任何水果选项。
How do I enable this behavior in angular-formly? 如何以角度形式启用此行为?
Here is the link to jsbin: https://jsbin.com/xololi/edit?html,js,output 这是jsbin的链接: https ://jsbin.com/xololi/edit ? html,js,output
I wrote a working example here https://jsbin.com/dukewac/6/edit?js,output 我在这里写了一个工作示例https://jsbin.com/dukewac/6/edit?js,output
The signature of templateOptions.onClick when a function is $modelValue, fieldOptions, scope, event
. 当一个函数是$modelValue, fieldOptions, scope, event
时,templateOptions.onClick的签名。 This occurs when the ngModelAttrsTemplateManipulator is run. 运行ngModelAttrsTemplateManipulator时会发生这种情况。 I leveraged these variables in my function. 我在我的函数中利用了这些变量。
Some of it is a bit hacky but that partially has to do with how angular implements checkboxes and the workarounds that the multiCheckbox type in angular-formly-templates-bootstrap employs. 其中一些有点hacky但部分与角度实现复选框和angular-formly-templates-bootstrap中的multiCheckbox类型所采用的变通方法有关。
This example will not work with nested keys but should if the multiCheckbox type was updated. 此示例不适用于嵌套键,但应该更新multiCheckbox类型。 This is because it is directly accessing the model using array access notation [see the code here] ( https://github.com/formly-js/angular-formly-templates-bootstrap/blob/a69d69e5c3f6382ea6a6c028c1d8bf76a9b94db3/src/types/multiCheckbox.js ). 这是因为它使用数组访问符号直接访问模型[请参阅此处的代码]( https://github.com/formly-js/angular-formly-templates-bootstrap/blob/a69d69e5c3f6382ea6a6c028c1d8bf76a9b94db3/src/types/multiCheckbox。 js )。
The code also assumes that the 'ALL' option is the first in the list and that its value is 'ALL'. 该代码还假定'ALL'选项是列表中的第一个选项,其值为'ALL'。 This could be fixed by adding a templateOption property that references what the index and value the all functionality is for. 这可以通过添加templateOption属性来修复,该属性引用所有功能的索引和值。
'ALL' will appear in your model. 'ALL'将出现在您的模型中。 A possible way to get around this would be to define a $parser for it. 解决这个问题的一种可能方法是为它定义一个$解析器。
templateOptions.valueProp
不支持templateOptions.valueProp
templateOptions.valueProp
is not taken into account but shouldn;t be too difficult to add. templateOptions.valueProp
没有被考虑在内,但是不应该太难添加。
Do you have any solution how to add defaultValue to multiCheckbox. 您是否有任何解决方案如何将defaultValue添加到multiCheckbox。 I can add it and see it in model but on view (html) it is not displayed. 我可以添加它并在模型中看到它但在视图(html)上它不会显示。 I'we made example on jsbin where I have all types of fields and also multiCheckbox... jsbin example Thanks. 我在jsbin上做了示例,我有各种类型的字段,还有multiCheckbox ... jsbin示例谢谢。
here is another working solution (actually updated ckniffty's code), which binds ng-click to formly multi checkbox field and then calls function in controller: 这是另一个有效的解决方案(实际上更新了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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.