angular-formly multiCheckbox Select 全部
angular-formly multiCheckbox Select All
我在 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"
},
],
},
},
];
当我select/unselect"ALL"时,我希望所有的选项都是selected/unselected。
例如。如果选中 ALL,则还应选中所有水果选项(苹果、橙子、梨、蓝莓)
如果我取消选择全部,那么应检查 none 个水果选项。
如何在 angular-formly 中启用此行为?
这是 jsbin 的 link:
https://jsbin.com/xololi/edit?html,js,output
我在这里写了一个工作示例https://jsbin.com/dukewac/6/edit?js,output
函数为$modelValue, fieldOptions, scope, event
时templateOptions.onClick的签名。当 ngModelAttrsTemplateManipulator 为 运行 时会发生这种情况。我在我的函数中利用了这些变量。
其中一些有点老套,但这部分与 angular 如何实现复选框以及 multiCheckbox 在 angular-formly-templates-bootstrap 中键入的解决方法有关雇用。
陷阱
嵌套键
此示例不适用于嵌套键,但如果更新了 multiCheckbox 类型,则应该适用。这是因为它使用数组访问符号直接访问模型[请参阅此处的代码] (https://github.com/formly-js/angular-formly-templates-bootstrap/blob/a69d69e5c3f6382ea6a6c028c1d8bf76a9b94db3/src/types/multiCheckbox.js)。
硬编码“全部”选项
该代码还假定 'ALL' 选项是列表中的第一个选项,并且其值为 'ALL'。这可以通过添加一个 templateOption 属性 来解决,该选项引用所有功能的索引和值。
模型包含 'ALL'
'ALL' 将出现在您的模型中。解决这个问题的一种可能方法是为其定义一个 $parser。
不支持templateOptions.valueProp
templateOptions.valueProp
没有考虑,但应该不会太难加。
你有什么办法可以把defaultValue 添加到multiCheckbox 中吗?我可以添加它并在模型中看到它,但在视图中 (html) 它不显示。
我在 jsbin 上做了示例,其中我有所有类型的字段以及 multiCheckbox... jsbin example
谢谢
这是另一个可行的解决方案(实际上更新了 ckniffty 的代码),它将 ng-click 绑定到形成多复选框字段,然后调用控制器中的函数:
....
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);
}
};
}
.....
我在 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"
},
],
},
},
];
当我select/unselect"ALL"时,我希望所有的选项都是selected/unselected。 例如。如果选中 ALL,则还应选中所有水果选项(苹果、橙子、梨、蓝莓)
如果我取消选择全部,那么应检查 none 个水果选项。
如何在 angular-formly 中启用此行为?
这是 jsbin 的 link: https://jsbin.com/xololi/edit?html,js,output
我在这里写了一个工作示例https://jsbin.com/dukewac/6/edit?js,output
函数为$modelValue, fieldOptions, scope, event
时templateOptions.onClick的签名。当 ngModelAttrsTemplateManipulator 为 运行 时会发生这种情况。我在我的函数中利用了这些变量。
其中一些有点老套,但这部分与 angular 如何实现复选框以及 multiCheckbox 在 angular-formly-templates-bootstrap 中键入的解决方法有关雇用。
陷阱
嵌套键
此示例不适用于嵌套键,但如果更新了 multiCheckbox 类型,则应该适用。这是因为它使用数组访问符号直接访问模型[请参阅此处的代码] (https://github.com/formly-js/angular-formly-templates-bootstrap/blob/a69d69e5c3f6382ea6a6c028c1d8bf76a9b94db3/src/types/multiCheckbox.js)。
硬编码“全部”选项
该代码还假定 'ALL' 选项是列表中的第一个选项,并且其值为 'ALL'。这可以通过添加一个 templateOption 属性 来解决,该选项引用所有功能的索引和值。
模型包含 'ALL'
'ALL' 将出现在您的模型中。解决这个问题的一种可能方法是为其定义一个 $parser。
不支持templateOptions.valueProp
templateOptions.valueProp
没有考虑,但应该不会太难加。
你有什么办法可以把defaultValue 添加到multiCheckbox 中吗?我可以添加它并在模型中看到它,但在视图中 (html) 它不显示。 我在 jsbin 上做了示例,其中我有所有类型的字段以及 multiCheckbox... jsbin example 谢谢
这是另一个可行的解决方案(实际上更新了 ckniffty 的代码),它将 ng-click 绑定到形成多复选框字段,然后调用控制器中的函数:
....
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);
}
};
}
.....