Matching DataTable/Mysql-Table indexes for a proper delete.

Matching DataTable/Mysql-Table indexes for a proper delete.

atupacatupac Posts: 8Questions: 0Answers: 4
edited July 2015 in Free community support

Hi, i'm using the DT nice stuff to display the content of a MySql table (localhost DB).

Now, i want to delete the selected raw(s) on dataTable, but here is my problem:

1- i use the 'fnGetSelected' DT-Tools extension to get all selected rows (i absolutely need to get an array with indexes as i use the Eloquent ORM, so can't use the ajax implemented server-side...)

2- i then use the $.post trick to "submit" the indexes to my php server page 'suppress_id.php'...

code below:

"fnClick": function(){            
    var tt = TableTools.fnGetInstance( 'dt_contacts' );
    var indexes = tt.fnGetSelectedIndexes();
    if(indexes.length>0)
       $.post(window.location.href='suppress_id', indexes);
}

==> Results:
:) -> routing (redirect to php page) for submit works.

:( -> i have problem with indexes value: always '1' (even selecting different rows and having tested it with alert() before submit..)

:( -> i have problem of matching indexes value order from DT with the mysql table after multiple deletings of rows (because index values are no more in order: 1/6/7/9/..).

So thanks for helping me solving those 2 bad points.

Replies

  • atupacatupac Posts: 8Questions: 0Answers: 4

    Well found so far, here my solution:

    on JS side:

    "fnClick": function(){
    
    +            // indexs des lignes selectionnées
    
    +            var indexs = table.cells('.selected', 0).data();
    
    +            // lecture valeur colonne cachée 'id_contact' pour chaque ligne sélectionnée
    
    +            if(indexs.length>0){              
    
    +              var indexsData = [];
    
    +              for (var i = 0; i < indexs.length; i++) {   
    
    +                indexsData[i] = indexs[i];
    
    +              }
    
    +              // Envoi indexs contacts à supprimer de la BDD
    
    +              $("#ids_contacts").val(indexsData);
    
    +              $("#contacts").submit();
    
                 }
    
                 else
    
                   alert( 'Aucune ligne sélectionnée!' );
    
               }
    

    On server side:

    $indexs = $this->app->request->post('ids_contacts');
    
    +       $indexs = explode(",", $indexs);
    
    +       foreach ($indexs as $index) {
    
    +           //print $index;
    
    +           Contact::supprimeContact($index);
    
    +       }   
    
    

    This worked for me to delete correct Matching selected rows with DB-Table lines :)

    PS: Just a thing, why can't we directly convert at "DT-Application" structure to an Array with toArray() ?

This discussion has been closed.