rows().remove not working

rows().remove not working

wandalinwandalin Posts: 3Questions: 1Answers: 0

Link to test case: none
Debugger code (debug.datatables.net): uqoban
Error messages shown: none
Description of problem:

I'm trying to remove a row from the table to keep the page and item count consistent and true, I use server-side processing with the ssp.class.php file.

The idea:

I have a table where a user can click on a button "receive" to receive a document (what I'm trying to make is a document tracking system). Why I need to hide the document though, is because if a document is intended for a specific person, it would hide that row so no-one else can "receive" the document, unless you are logged in as that person.

here's the code I've tried so far:

$(document).ready(function (){
    var receiveTable = $('#receiveTable').DataTable({
        processing: true,
        language: {
            processing: '<div class="spinner-border text-warning" role="status"><span class="sr-only"></span></div>'
        },
        serverSide: true,
        ajax: requestsFolder + 'getToReceive.php',
        "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ],
        "searching": true,
        "ordering": false,
        "info": true,
        "autoWidth": false,
        "columnDefs": [
            { "targets": [2], "className": "d-none" },
            { "targets": [0,1,4], "className": "align-middle text-center"},
            { "targets": "_all", "className": "align-middle"},
            { "targets": 0, "checkboxes": { "selectRow": true } }
        ],
        "select": { "style": "multi" },
        "responsive": true,
        "drawCallback": function( settings ) {
            $('[data-toggle="tooltip"]').tooltip();
            $('[data-toggle="popover"]').popover();
        },
        "rowCallback": function( row, data, index ) {
            if (data[2] === "no") {
                // $(row).hide(); // <------ this works, but item count of the table is not true, and it messes with the pagelength
                $(row).addClass('remove'); // <----- upon dom inspection, the classes are indeed added to the <tr>
            }
        },
      });

      receiveTable.rows('.remove').remove().draw(); // <----------- this is my main issue, it does not work
    
      $(document).on('submit','#receive', function(e){
        var form = this;

        var rows_selected = receiveTable.column(0).checkboxes.selected();
        $.each(rows_selected, function(index, rowId){
            $(form).append(
                $('<input>')
                   .attr('type', 'hidden')
                   .attr('name', 'rec-check[]')
                   .val(rowId)
            );
         });
      });
});

any help would be greatly appreciated! If there are other needed files, please let me know.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 16,828Questions: 25Answers: 3,989
    Answer ✓

    rows().remove() works with client side processing only. With server side processing all table functions are expected to take place at the server script.

    // $(row).hide(); // <------ this works, but item count of the table is not true, and it messes with the pagelength

    Yes, because Datatables doesn't know this happened and still has the rows has displayed in it data cache.

    One option is to turn off server side rpocessing, unless you need it for performance reasons. This will allow you to use the APIs that are client side only.

    However more secure option might be to use ajax.data to send to the server the user information so the server can filter the result based on the user. See this example. This way you don't have data at the client that you don't want the user to have access to. This option will work with client side or server side processing.

    Kevin

  • wandalinwandalin Posts: 3Questions: 1Answers: 0

    Hello! Thanks for the fast reply! I do have the table on client-side now, but upon the first week of beta-testing, data gathered was around ~5.4k rows of data so having it sever-side seems much more feasible in the long run, I was able to do it like you said and took the process on the server script. I did try to use ajax.data but since the data is already saved in a php session variable, I directly accessed that instead and performed my queries. I'm not sure if this approach is the most efficient though, would love some thoughts! and thank you again!

  • allanallan Posts: 56,888Questions: 1Answers: 9,042 Site admin
    Answer ✓

    Locking can always be a bit difficult since you run into issues with users not releasing a lock - etc. However, what I think would typically be done in this case is to have a column in the database table that indicates the user (or session) that has a lock on a file. Your SQL query to display all documents could then take that into account (e.g. have a condition that shows documents which aren't locked, or are locked but are locked to this session/user).

    There would be an Ajax request to request a lock which needs to complete before the user gets to see the document.

    Hope that helps a bit. What I suggest isn't the only way! There are many ways to do locking and if your own works, roll with it!

    Allan

  • wandalinwandalin Posts: 3Questions: 1Answers: 0

    This is exactly what I did! I do have a column on my database named "for" which contains the user id for who the file receiver is, and 0 if it's accessible to anyone, I just worked that into my queries and the table is much cleaner now, thanks again to the both of you! :)

Sign In or Register to comment.