Dynamic checkbox list in modal editor
Dynamic checkbox list in modal editor
I am dynamically creating a list of checkbox items that correspond to roles in ASP.NET Core. Everything seems to be working when displaying the statuses in the checkboxes, but after updating the roles with the submit button, the JSON result is wrong.
https://live.datatables.net/razujato/5/edit
Description of problem:
When pressing the EDIT RECORD button, the JSON result is not displayed correctly in the CONSOLE. False values are not displayed and the boolean states do not match what is selected from the modal.
Replies
Hi,
That the checkbox is not included in the value of the field when not checked, is correct and expected. That is how HTML checkboxes work.
What you want to do is have the
value
property for each checkbox identify that checkbox, so you end up with an array of the values selected. For example you might have:As the selected values, rather than an array of booleans such as:
This example might help - note how each checkbox has its own value. In that case it is a foreign key reference to the linked table, so we can build up an array of ids for the field value.
Once important point here is that position in the array of checkbox values is effectively meaningless. If
3
appears in the value array somewhere, it will check the box which has that value.In your example, you would want to use the
RoleName
as the value for the option. I'm not sure thatSelected
in thedatarole
property actually has any use here, since the value of the checkboxes is set by therolesname
array indataSet
.I've made a few changes to get your example working: https://live.datatables.net/maxomuza/1/edit .
Specifically:
rolesname[]
since that is the property name in the data objectopen
event since it is a static listRoleName
.I think it would actually be more usual to have the
id
as the value (the UUID) and that is what would be in therolesname
array, rather than the label / name. But this works as well (although it doesn't allow for two entries with the same name).Regards,
Allan