Server-side Delete Row with fnDeleteRow()

Server-side Delete Row with fnDeleteRow()

sitsumsitsum Posts: 9Questions: 0Answers: 0
edited November 2011 in General
Hi Guys,

I have come across yet another hurdle there.

I am using server-side processing to get my data and in each row there is a delete button with the class "delEnqBtn"

the code that i have to handle the delete is:
[code]
$("#example tbody tr td .delEnqBtn").live('click', function(){
var aPos = oTable.fnGetPosition(this.parentNode);
var aData = oTable.fnGetData(aPos[0]);
$.ajax({
type: "GET",
url: "enquiry_del.php",
data: "enq_id="+aData[1],
success: function(msg){
oTable.fnDeleteRow(aPos[0]);
}
});
});
[/code]

according to http://datatables.net/forums/discussion/6528/problems-with-delete-row/p1 and in the API http://datatables.net/api

the .fnDeleteRow() accepts both the TR element that should be deleted from the table or Index of the row to delete from the aoData object (use the fnGetPosition() API function to find the index of a row in this array).

as you can see in my code above i was able to get the index of the row (aPos[0]) by using .fnGetPosition()

then i fire the ajax to delete the particular record from the database, then if its successful it will call fnDeleteRow().

everything works fine up until fnDeleteRow() it keeps me an error: Unable to get value of the property 'nTr': object is null or undefined
jquery.dataTables.js, line 6009 character 5

can anyone please point me to the right direction as to why my code didnt work

Replies

  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    I think it's one of those annoying closure/scope issues

    what does the debugger say aPos is, when you're in the success function?
  • sitsumsitsum Posts: 9Questions: 0Answers: 0
    aPos is e.g [o,1,1] as you would expect.

    I dont know what i have done/changed its working now.

    but there is one more thing, sometime it will retain all the sortings and filters and paging but sometimes it doesnt. does any one know how i can set it so it doesnt refresh/reload the table or it will keep/remember all the sortings and filters and paging?
  • sitsumsitsum Posts: 9Questions: 0Answers: 0
    i have narrowed down the issue.

    it does actually remember the sorting and filtering but as for the paging it always jumps back 1 page.

    What i meant by that is if the current sql limit is "LIMIT 40,10" (show 10 records at a time and its on page 4) the new table will be draw from "LIMIT 30,10" it will display the 3rd page instead of the 4th page i was on previously

    is this a normal behaviour?
  • nicklognicklog Posts: 1Questions: 0Answers: 0
    Hello, I have the same problem.
    Upgraded to 1.9.1 didn't correct it.

    Have you found a solution since your last post ?
  • AnthonyVAnthonyV Posts: 37Questions: 7Answers: 0
    I am seeing the exact same problem as sitsum, where a delete will cause datatables to request the previous page. I also have 1.9.1.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Can you link me to an example showing the problem please?

    Allan
  • AnthonyVAnthonyV Posts: 37Questions: 7Answers: 0
    Hello Allan,

    Can you fire me a personal email and I will see what I can do. This seems to require server interaction, your inline editing demo had no issues. Note: this is a personal time project for me so don't expect immediate response.

    Anthony
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Done :-)
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Hi Anthony,

    Thanks for the details. I've just committed a fix to DataTables that addresses this, which you can download in the 1.9.2.dev nightly: http://datatables.net/download .

    Having said that I would urge considerable cation here - fnAddData and fnDeleteRow really should _not_ be used with server-side processing. These two methods are entirely client-side based, they have absolutely no knowledge of your server-side environment, so as soon as you do the next draw, the data will be removed and replaced with what the server knows about (its in server-side processing mode after all!).

    Allan
  • AnthonyVAnthonyV Posts: 37Questions: 7Answers: 0
    >> just delete the row on the server and then call fnDraw().

    Thanks for the fix and the suggestion. What you say makes sense, even more so for deletes because you need to retrieve another row from the server to make the row count in the page complete anyway.

    My reason for heading in the direction of fnAddData and fnDeleteRow, is because I want to minimize server traffic. For example, if I add a row to the server from the client then why not just use fnAddData to add the row locally and avoid the whole extra trip to the server to rebuild the table contents.

    Additionally I would expect that fnDeleteRow would be able to "visibly" delete the row (for example by a collapse row animation) that would be much more appealing and informative for the user rather then a simple table refresh.

    Just my two cents..

    Thanks again for an excellent tool.
This discussion has been closed.