Checkboxes on an Editor Form - checkbox state is flipped

Checkboxes on an Editor Form - checkbox state is flipped

rsreaganrsreagan Posts: 7Questions: 0Answers: 0
edited December 2012 in General
Hi folks, I'm using the DataTables.net Editor. One of my fields is a checkbox, and I define it as such:

[code]
{
"dataProp": "IsAssociated",
"label":"Is assigned",
"name":"IsAssociated",
"type": "checkbox",
"ipOpts": [
{
"label": "",
"value": ""
}
],
"separator": ""
},
....
[/code]

I'm loading my DataTable from an AJAX source. The JSON data coming across for this checkbox field is transmitted as "True" or "False" (not "1" or "0"). This is what my WCF service serializes a boolean as.

My problem is that in my Editor form, False values are giving the checkbox a checkmark, and True values do NOT have a checkmark. Can anyone tell me what might be wrong with my field declaration above? Or where else I might investigate?

From the docs, it looks like there can be multiple checkboxes that are mutually exclusive and used like radio buttons, but that's not what I'm trying to do. I just need to represent a boolean value. Thanks in advance!

Edit: Allan, we're doing a lot with DataTables.net, the Editor, and .NET (particularly with WCF services). Would you be interested in posting any of our basic examples for the .NET world? If so, let me know at rob _at_ ironhorsesoftware.com.

Replies

  • rsreaganrsreagan Posts: 7Questions: 0Answers: 0
    edited December 2012
    Ok, I think I've got it. To represent a boolean value in the form, the aforementioned code above should be changed to:

    [code]
    {
    "dataProp": "IsAssociated",
    "label":"Is assigned",
    "name":"IsAssociated",
    "type": "checkbox",
    "ipOpts": [
    {
    "label": "", //leave this blank because I'm already using 'Is assigned' as my label.
    "value": "1" //1 will check the checkbox if the value is True.
    }
    ],
    "separator": ""
    },
    ....
    [/code]

    This does lead to one potential problem. On the JSON postback, the value of IsAssociated will be in an array with one element. If you don't want to bend your web service to accommodate DataTables.net, you can reassign the value before the Editor calls back to the server. In the Editor initialization code, I changed the following:

    [code]
    var myEditor = new $.fn.dataTable.Editor({
    "ajax": function (method, url, data, successCallback, errorCallback) {
    data.data.IsAssociated = data.data.IsAssociated[0];
    ... more code ...
    },
    ...
    [/code]

    Allan or anyone else, can you confirm that this is the correct way to accomplish this?
  • allanallan Posts: 63,394Questions: 1Answers: 10,450 Site admin
    If you set the separator to something that isn't false on a loose type check, you'll get a string back rather than an array. The array format is useful for Joins, but less useful in cases like this where you just want a value. For that, use a separator such as `","` . Saves messing around with the `ajax` method!

    Regards,
    Allan
  • rsreaganrsreagan Posts: 7Questions: 0Answers: 0
    That worked like a charm. Thanks!
This discussion has been closed.