Load $_GET variable into filter/search box

Load $_GET variable into filter/search box

ripcurlksmripcurlksm Posts: 10Questions: 4Answers: 0
edited September 2017 in Free community support

I have a page that has a datatable on it that I'd like to be able to load a search via $_GET variable like this:

www.website.com/mydatatable.php?search=cars

Is there a way to make datatables load "cars" into the search/filter area when the page loads?

Here is my datatable code:

$(function () { 
            var loc = window.location.pathname;
            var dir = loc.substring(0, loc.lastIndexOf('/'));
            url= dir + "./DataTables-1.10.5/extensions/TableTools/swf/copy_csv_xls_pdf.swf";
            $('#example').DataTable({
                dom: 'T<"clear">lfrtip',
                tableTools: {
                    "sSwfPath": "./DataTables-1.10.5/extensions/TableTools/swf/copy_csv_xls_pdf.swf",
                    "aButtons": [ "xls" ] //"aButtons": [ "copy", "csv", "xls","pdf","print" ]
                },
                    columnDefs: [
               { type: 'formatted-num', targets: 3 }
             ],
         order: [[ 5, "desc" ]]
            } );
            var tableTools = new $.fn.dataTable.TableTools( table, {
                "buttons": [
                    "copy",
                    "csv",
                    "xls",
                    "pdf",
                    { "type": "print", "buttonText": "Print me!" }
                ]
            } );
              
            $( tableTools.fnContainer() ).insertAfter('div.info');
        });

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Yes - use the search option and pass in the variable from the query string.

    Moreover, I think you might find this blog post interesting, as it basically formalises that kind of thing into a DataTables plug-in.

    Allan

This discussion has been closed.