Dynamic checkbox list in modal editor

Dynamic checkbox list in modal editor

ingezoneingezone Posts: 20Questions: 8Answers: 0

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

  • allanallan Posts: 63,602Questions: 1Answers: 10,486 Site admin

    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:

    [ 0, 3 ]
    

    As the selected values, rather than an array of booleans such as:

    [ true, false, false, true ]
    

    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 that Selected in the datarole property actually has any use here, since the value of the checkboxes is set by the rolesname array in dataSet.

    I've made a few changes to get your example working: https://live.datatables.net/maxomuza/1/edit .

    Specifically:

    1. The field name should be rolesname[] since that is the property name in the data object
    2. Removed the use of the open event since it is a static list
    3. Changed the value to RoleName.

    I think it would actually be more usual to have the id as the value (the UUID) and that is what would be in the rolesname 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

Sign In or Register to comment.