Datatbase table is not refresh after deleting the Row from the table

Datatbase table is not refresh after deleting the Row from the table

Omar TweetOmar Tweet Posts: 22Questions: 0Answers: 0

Link : https://justjammu.com/testrec/record.php

Steps:

1) On the page load of record.php, from AJAX page (record-extra.php) it will call DB and create Dynamic HTML then bind this HTML with html(worksHTML) with DIV element.

2) After the bind HTML, it will call the Database tables for the Pagination and Search columns.

3) Upto here, it is working fine.

4) There is a DELETE button with every row, on call of DELETE button it will AJAX page(record-del.php) it will call DB and DELETE from the table for that record. Then in JS it will remove that <tr> element.

5) Upto, here it is working fine. Then I am destroying the Database table and then call the ajax.refresh but it not showing NEW pagination and number of records.

Replies

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited February 23

    After destroying the Datatable you are calling a function called loadtable(). All it is doing is initializing the Datatable but you are not fetching the updated data via Ajax. Possibly you need to call getWorks() instead.

    After destroying the Datatable you might also nee to use jQuery empty() to clear the table contents before reloading the table via ajax.

    Kevin

  • Omar TweetOmar Tweet Posts: 22Questions: 0Answers: 0

    Thank @kthorngren for your reply:

    Yes, if I call getWorks() then it is working fine but I don't want to call ajax DB again for the select statement.

    I want to delete the row from DB from PHP and then remove.<tr row> from JS level. No need to call DB PHP for the SELECT statement. Is it not possible to avoid SELECT statement?

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    It is possible but your step 5 indicated that you wanted to destroy the Datatable and reload the data via ajax.

    Instead of using destroy() you could use row().remove(). I think the easiest way, based on your call to del() with the id, is to use rowId with the setting of id. This will allow the use of row-selector as the row id to get the proper row for row().remove().

    Kevin

  • Omar TweetOmar Tweet Posts: 22Questions: 0Answers: 0

    Thank you very much, @kthorngren. Your solution is working fine.

    datatable.row($(doc).parents('tr')).remove().draw(); -- This is working where doc=this object

    Now, another case.. If I am at 3rd page of the pagination and deleted the record from that page and after the delete using above command it is coming back to the first page instead of remaining at the same page. Is it possible to remain on the same page itself using above solution.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The draw() docs show options to control the paging. Likely you will want to pass false to stay on the same page but update the sorting and searching.

    Kevin

  • Omar TweetOmar Tweet Posts: 22Questions: 0Answers: 0
    edited February 23

    datatable.row($(doc).parents('tr')).remove().draw(false); -- This is working where doc=this object and I remained at the same page of the pagination. Also it also keep the column filtered as it, so that is good.

    For instance, If I chose color "Re" it will fetch 4 records and If I delete one record then 3 will be shown and the value "Re" will remain in the text box of color filter and that is good.

    I need to close this discussion and you have made my day. Thank you very much.

  • Omar TweetOmar Tweet Posts: 22Questions: 0Answers: 0

    Hello @kthorngren , In the below link:

    https://justjammu.com/testrec/record.php

    The Excel button is not working and rest of buttons are working fine.

Sign In or Register to comment.