Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use datatables (jquery datagrid plugin ) to implement an check all and delete function?

I am using jquery plugin Datatables to manipulate my rows Actually it has a tabletools plugin that allow checkall function, however, I can check all the items/ check multi, but how can add a delete button and return the row selected?

I already have the sql query for deletion and the function for a deletion warning popup box .

Thank you

Select all in datatable

Document on how to retrieve row, but i still don't understand how to do this, thank you

        $(document).ready( function () {
            $('#viewSub').dataTable( {
                "sDom": 'T<"clear">lfrtip',                 
                "oTableTools": {
                    "sSwfPath": "../plugin/DataTables-1.9.0/extras/TableTools/media/swf/copy_cvs_xls_pdf.swf",
                    "sRowSelect": "multi",
                    "aButtons": [
                        "select_all",
                        "select_none",
                        "copy",
                        "print",
                        "delete", <===********can not add here, seems only allow pre-defined button.
                        {
                            "sExtends":    "collection",
                            "sButtonText": "Save",
                            "aButtons":    [ "csv", "xls", "pdf" ]
                        }
                    ]

                }
            } );
        } );
like image 833
Leo Chan Avatar asked Mar 30 '12 16:03

Leo Chan


1 Answers

You can add a function to catch selected row:

$(document).ready( function () {
    $('#example').dataTable( {
        "sDom": 'T<"clear">lfrtip',
        "oTableTools": {
            "fnRowSelected": function ( node ) {
                alert( 'The row with ID '+node.id'+ was selected' );
            }
        }
    } );
} );

In this function, you can, for example, add set value of a hidden field to node.id, a a simple <button>Delete</button> and onClick handler to this button to delete row with selected id.

Check also answers to this question.

like image 78
maialithar Avatar answered Oct 20 '22 17:10

maialithar