My checkbox does not correctly return TRUE/FALSE status

My checkbox does not correctly return TRUE/FALSE status

ingezoneingezone Posts: 17Questions: 7Answers: 0

https://live.datatables.net/razujato/1/edit

Debugger code (debug.datatables.net):
editor.on('preSubmit', function (e, json, data, action) {
let opt = this.field('options');
console.log(opt.val()[0]);
});

Error messages shown: undefined value.

Description of problem:
Once in the editor modal, when the checkbox is checked and then I press the Refresh button, the code returns "true". BUT when the checkbox is NOT checked and I press the Refresh button, the code returns 'undefined'

How do I get the code to return the value 'false' instead of 'undefined'

Regards! :)

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    Hi,

    You need to use the unselectedValue option of checkbox if you want it to have a value when no checkbox is selected. You are using it more as a radio box there, which is why this is required.

    Allan

  • ingezoneingezone Posts: 17Questions: 7Answers: 0

    Hi

    Thanks for your response, I got it working by returning the TRUE/FALSE value, using the following code snippet.

    { label: "Mostrar opciones:", name: "options", type: "checkbox", //separator: '|', options: [{ label: '', value: true }], unselectedValue: false, def: false }

    How do I make the delivered value be of type boolean? Currently the value is of type Array and the result is [false] or [true], otherwise if I enable the { separator: '|' }, the result is type string "false" or "true".

    You can see my test code here: https://live.datatables.net/razujato/2/edit

    Regards!

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    How do I make the delivered value be of type boolean

    Client-side use fields.getFormatter to cast the value. The reason for this is that the value is read from the input element which is always a string.

    At the server-side you might need to cast it there if you are expecting a true Boolean there. Http parameters are type-less unfortunately.

    Allan

  • ingezoneingezone Posts: 17Questions: 7Answers: 0

    Hi Allan

    I thank you very much for your answers, they were very accurate and I was able to solve it.

    Regards!

Sign In or Register to comment.