admin管理员组文章数量:1389903
I am trying to create a similar Group Checkbox Control like the WordPress categories listing but have not been able to save my values. I am not convinced I am taking the best direction, but seem to be close to the answer.
I'd like these checkbox values to save to a single attribute as an array, but I am fine with saving the selections as a string (value1,value3,value4,etc).
Below is an example of the edit()
method I have created and I've taken notes from a similar question here, but am not able to get my selections to save:
attributes: {
myCheckbox: {
type: 'array',
default: []
}
},
edit: function ( props ) {
var checkboxes = [];
var data = [
{
label: 'Checkbox 1',
value: 'checkbox1'
},
{
label: 'Checkbox 2',
value: 'checkbox2'
}
];
$.each( data, function ( c, fields ) {
checkboxes.push(
el( CheckboxControl, {
key: fields.value,
label: fields.label,
name: 'myCheckbox[]',
onChange: function( val ) {
props.setAttributes({ myCheckbox : myCheckbox[fields.value] });
}
})
)
});
return [
el(
InspectorControls, {
key: 'inspector'
},
el(
PanelBody, {
title: __( 'Settings' )
},
checkboxes
)
),
el( ServerSideRender, {
block: 'block/name',
attributes: props.attributes
} )
]
},
I am trying to create a similar Group Checkbox Control like the WordPress categories listing but have not been able to save my values. I am not convinced I am taking the best direction, but seem to be close to the answer.
I'd like these checkbox values to save to a single attribute as an array, but I am fine with saving the selections as a string (value1,value3,value4,etc).
Below is an example of the edit()
method I have created and I've taken notes from a similar question here, but am not able to get my selections to save:
attributes: {
myCheckbox: {
type: 'array',
default: []
}
},
edit: function ( props ) {
var checkboxes = [];
var data = [
{
label: 'Checkbox 1',
value: 'checkbox1'
},
{
label: 'Checkbox 2',
value: 'checkbox2'
}
];
$.each( data, function ( c, fields ) {
checkboxes.push(
el( CheckboxControl, {
key: fields.value,
label: fields.label,
name: 'myCheckbox[]',
onChange: function( val ) {
props.setAttributes({ myCheckbox : myCheckbox[fields.value] });
}
})
)
});
return [
el(
InspectorControls, {
key: 'inspector'
},
el(
PanelBody, {
title: __( 'Settings' )
},
checkboxes
)
),
el( ServerSideRender, {
block: 'block/name',
attributes: props.attributes
} )
]
},
Share
Improve this question
edited Feb 15, 2020 at 7:47
Alex Mangini
asked May 21, 2019 at 19:46
Alex ManginiAlex Mangini
252 silver badges9 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default -1Use this code
$.each(data, function (c, fields) {
checkboxes.push(
el( CheckboxControl, {
key: fields.value,
label: fields.label,
name: 'myCheckbox[]',
checked: props.attributes.myCheckbox.indexOf(fields.value) > -1,
onChange: function( val ) {
let data = props.attributes.myCheckbox;
if (val) {
if (data.indexOf(fields.value) === -1) {
data.push(fields.value);
}
} else {
data = props.attributes.myCheckbox.filter((v) => v !== fields.value);
}
props.setAttributes({ myCheckbox: data });
}
})
)
})
本文标签: block editorMultiple checkboxes Gutenberg control
版权声明:本文标题:block editor - Multiple checkboxes Gutenberg control 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652834a2617788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
myCheckbox
. You need to have a separate attribute for each checkbox. If the user can only choose one of the two, use radio buttons instead. – WebElaine Commented May 21, 2019 at 20:11checked
in the Checkbox control declaration, or are you talking about actually checkingmyCheckbox.checked ? true : false
and if so, in thesetAttributes
function? I ask because I am still a little unclear, though your comment has given me a better understanding. By the way, I am not trying to create a radio, I only limited the choices to keep the example code shorter. – Alex Mangini Commented May 21, 2019 at 22:10true : false
and using that inprops.setAttributes
. And, you need to have a separate attribute for each checkbox. – WebElaine Commented May 22, 2019 at 13:40myCheckbox[fields.value]
? I am not sure how to referencemyCheckbox
as an array without getting errors. – Alex Mangini Commented May 22, 2019 at 18:00mycheckbox1
attribute and amycheckbox2
attribute, or else if you are going to use a singlemycheckbox
attribute that is an array of values, you'll have to clone that array and update only the value that changed. Much simpler to use separate attributes IMHO. – WebElaine Commented May 22, 2019 at 18:40