Cannot read current checkbox value. fnGetNodes()/fnGetData() do not work.
Cannot read current checkbox value. fnGetNodes()/fnGetData() do not work.
dogcatgoat
Posts: 38Questions: 0Answers: 0
Hi,
I am trying to submit a JSON object ONLY composed of checked rows in my table.
I have looked at the following example:
http://www.datatables.net/examples/api/form.html
In this case, only the names of the checkboxes that are selected are submitted.
I actually want to submit all the other columns of data as a JSON object since the user is allowed to edit them.
If I take the fnGetNodes approach:
[code]
vTable.fnGetNodes().each(function(i){....
var isChecked=vTable.fnGetData(i)[checkbox_row].attr('checked');
...
});
[/code]
the attr() selector always returns the initial form value, not its current selected value.
If I try to use $("vTable tbody tr") approach as described in:
http://www.datatables.net/examples/advanced_init/events_post_init.html
I am lost on how I can query the checkbox column to see if it is on or off and then construct the JSON object with the rest of the columns. For example:
[code]
$("#vTable tbody tr :checked").each(function(i)...
[/code]
Only selects/returns the checkbox object out of context of its neighboring columns which I need to construct the JSON object with.
Can someone please advise?
Thank you.
I am trying to submit a JSON object ONLY composed of checked rows in my table.
I have looked at the following example:
http://www.datatables.net/examples/api/form.html
In this case, only the names of the checkboxes that are selected are submitted.
I actually want to submit all the other columns of data as a JSON object since the user is allowed to edit them.
If I take the fnGetNodes approach:
[code]
vTable.fnGetNodes().each(function(i){....
var isChecked=vTable.fnGetData(i)[checkbox_row].attr('checked');
...
});
[/code]
the attr() selector always returns the initial form value, not its current selected value.
If I try to use $("vTable tbody tr") approach as described in:
http://www.datatables.net/examples/advanced_init/events_post_init.html
I am lost on how I can query the checkbox column to see if it is on or off and then construct the JSON object with the rest of the columns. For example:
[code]
$("#vTable tbody tr :checked").each(function(i)...
[/code]
Only selects/returns the checkbox object out of context of its neighboring columns which I need to construct the JSON object with.
Can someone please advise?
Thank you.
This discussion has been closed.
Replies
http://www.datatables.net/examples/advanced_init/events_post_init.html
I query:
[code]
$('#vtable tbody tr').each(function(i) {
var nTds=$('td', this);
var c_val=$(nTds[1]).attr('checked');
[/code]
but c_val returns null. Why can't I get the value of the checkbox this way?
Thanks again.
I'd suggest something like:
[code]
$('input:checked', oTables.fnGetNodes() )
[/code]
Allan
Yes, the second column contains the checkbox (others are also hidden).
I use that to try to build my JSON object as follows:
[code]
$('input:checked', vTable.fnGetNodes()).each(function(i){
appendMyJSON(myJSON, vTable.fnGetData(i));
});
[/code]
where I have a function appendMyJSON which takes as input a JSON object and a row of data to add to it.
I am still having problems now, though, since the reference i is in a different order than how it is displayed and so the incorrect row is added to it.
Is this where you use things like vTable.fnGetPosition(this)/fnGetData(aPos[0]) to correlate the rows correctly? I copied the example from the API but get an error message:
Uncaught TypeError: Cannot read property '0' of null
Thanks for any other advice. I'm still on the learning curve for jquery and datatables!
Tried to name the checkbox the string "myString" which is the value of an adjacent cell.
From here, I found fnFindCellRowNodes() in the API:
[code]
var aaa = vTable.fnFindCellRowNodes( myString );
[/code]
Which should give me the entire row that the checkbox is in. Then tried to parse aaa which is an HTMLTableRowElement object to get all the values of the other columns in the row:
[code]
var nTds=$('td', aaa);
[/code]
but its not returning anything that makes sense to me.
Do you have any further suggestions? Thanks,
[code]
$('input:checked', vTable.fnGetNodes()).each(function(i){
appendMyJSON(myJSON, this.value);
});
[/code]
Allan
Thanks for your response.
I actually want to submit the 30 other columns as the JSON object. The user will be able to change them after they are posted in the table. Some of the columns I would like to submit require a little processing ideally before they get to the server (meaning I would like to have 20-30 lines of code to individually process each of the other elements in the row into JSON objects-- I don't need for them to all be submitted as is).
Thanks again.