Checkbox selection

Checkbox selection

matthewjmatthewj Posts: 39Questions: 2Answers: 0
edited June 2013 in Editor
The checkbox field created with add API method doesn't check the selected values got from the server[when clicked on
Edit button of editor]. Interestingly the checkbox field created using fields initialization option checks the selected values got from the server.

This works:
[code]
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": ".....",
"domTable": "....",
"idSrc": "id",
"fields": [
....
....
{
"label": "User type:",
"name": "usertype",
"type": "checkbox",
"separator": "|",
"ipOpts": [
{ "label": "Student", "value": "1" },
{ "label": "Proctor", "value": "2" }
]
}
], ....
...
});
[/code]

Below doesn't show check to selected values:
[code]
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": ".....",
"domTable": "....",
"idSrc": "id",
....
...
});

editor.on('onInitCreate', function () {
fnUserScreenFields();
}
editor.on('onInitEdit', function () {
fnUserScreenFields();
}

function fnUserScreenFields() {
editor.add({
"label": "First name:",
"name": "fname"
});
editor.add({
"label": "Middle name:",
"name": "mname"
});
editor.add({
"label": "Last name:",
"name": "lname"
});
editor.add({
"label": "User type:",
"name": "usertype",
"type": "checkbox",
"separator": "|",
"ipOpts": [
{ "label": "Student", "value": "1" },
{ "label": "Proctor", "value": "2" }
]
});
....
....
}
var table = $('#userlist').dataTable( {
....
});

[/code]

below is my trimmed json string:
[code]
{"sEcho":1,"iTotalRecords":"13","iTotalDisplayRecords":"13","aaData":[{"id":"102674","fname":"xyz",...,"usertype":"1|2"}, ...
[/code]

Please note in the json string "usertype":"1|2" is the separated list of values to check. The checkboxs are checked [in Edit form of editor] when field is initialized using fields initialization option but when I use the second approach using add method of api the checkboxs are not shown checked in edit form.
http://editor.datatables.net/fields/#checkbox

Any pointers why the second approach doesn't work?

Regards,
Mathew

Replies

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    This doesn't work as `onInitCreate` and `onInitEdit` actually fire after the values (default or current) have been set. The intention for these two events was that you could use them to manipulate the form once they have been fully set up - including values etc.

    One option is to call the `set` function immediately after the add with the value you want set.

    I'll have a look into adding more events for this. Thanks for flagging it up.

    Regards,
    Allan
This discussion has been closed.