Get Row Position
Get Row Position
Hello Everyone,
I have a table which has a column that contains a checkbox. There is a button outside of DataTables that has an onclick event which will read the values of the checkboxes and make ajax requests to delete those rows from the server side.
This is working however I have found that I need to remove the exact rows where the checkboxes are clicked when the ajax returns success for each row. I have looked at the api and found that you can use fnDeleteRow( position ); and fnGetPosition() to get the position. However in my case it is not the tr or td element that is clicked. It is a checkbox being checked and then when the button is pressed it reads those checkbox values.
Is there a way to get the position from my checkbox elements in the table rather than having to somehow hijack the td or tr elements?
I have a table which has a column that contains a checkbox. There is a button outside of DataTables that has an onclick event which will read the values of the checkboxes and make ajax requests to delete those rows from the server side.
This is working however I have found that I need to remove the exact rows where the checkboxes are clicked when the ajax returns success for each row. I have looked at the api and found that you can use fnDeleteRow( position ); and fnGetPosition() to get the position. However in my case it is not the tr or td element that is clicked. It is a checkbox being checked and then when the button is pressed it reads those checkbox values.
Is there a way to get the position from my checkbox elements in the table rather than having to somehow hijack the td or tr elements?
This discussion has been closed.
Replies
When the page first loads it generates the table in the HTML page. However when you use the add new row feature it makes an ajax call to an external site, fetches the data and uses fnAddData() to add the new rows.
I am stating this because I cannot always assume that the row will be in the HTML page. I am not sure if this makes a difference or not but I have seen this: http://www.datatables.net/forums/discussion/192/using-fngetposition-on-dynamic-tables/p1 which is why I mention this.
http://api.jquery.com/closest/
$(checkbox).closest('tr').get(0);
is that what you mean?
I mean your code gets the index properly but I then get the following error from JQuery
[code]
a.nodeName is undefined
[/code]
Here is my code that is being executed:
[code]
if(confirm('Are you sure you wish to delete the selected test users?')) {
$(".testUser_Checkbox:checked").each(function() {
var user_id = $(this).val();
$.ajax({
url: '/admin/qatooldeletetestuser/?user='+user_id,
blockUI: true,
success: function(data) {
oTable.fnDeleteRow( oTable.fnGetPosition( $(this).closest('tr').get(0) ) );
$.jGrowl('Deleted User '+user_id);
},
});
});
}
[/code]
I think the error is coming from the use of $(this) inside the success callback. I am not quite sure how I could get at the data there but that's where I need it to happen on the successful response from the ajax call. Any thoughts?
[code]
if(confirm('Are you sure you wish to delete the selected test users?')) {
$(".testUser_Checkbox:checked").each(function() {
var user_id = $(this).val();
var indexOfRow = oTable.fnGetPosition( $(this).closest('tr').get(0) );
$.ajax({
async: false,
url: '/admin/qatooldeletetestuser/?user='+user_id,
blockUI: true,
success: function(data) {
oTable.fnDeleteRow( indexOfRow );
$.jGrowl('Deleted User '+user_id);
},
});
});
}
[/code]