Datatables - how to pass a search parameter to a url

I would like to be able to link to a page with a datatable that will pass the value of the search parameter in the url. The goal is for the open source page to be pre-filtered with the search value parameter.

I installed jsfiddle to work with some sample data.
http://jsfiddle.net/lbriquet/9CxYT/10/

The idea would be to add a parameter to the jsfiddle url so that the page displays with the search input set to "firefox", for example, and the table is filtered to show only search matches.

Any help would be really appreciated!

+6
source share
3

, URL, . , q = Firefox

// Read a page GET URL variables and return them as an associative array.
function getUrlVars()
{
 var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;

}

$(document).ready(function() {
    // Get the query from the url
    var query = getUrlVars()['q'];
    // create the table
    var oTable = $('#example').dataTable();
    // Filter it
    oTable.fnFilter( query, 2 );
});

http://fiddle.jshell.net/9CxYT/17/show/?q=Firefox

+4

, . , .

$(document).ready( function() {
  
  // First, get the search parameter. Here I use example.com#search=yourkeyword
    var searchHash = location.hash.substr(1),
        searchString = searchHash.substr(searchHash.indexOf('search='))
		                  .split('&')[0]
		                  .split('=')[1];
  
  
  $('#example').dataTable( {
    "oSearch": { "sSearch": searchString }
  } );
} )
Hide result
+3

, :

@Ibriquet GET URL, . , , .

// Read a page GET URL variables and return them as an associative array.
function getUrlVars()
{
 var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;

}

$('#tablename').DataTable( { here, right after the column setup }

      initComplete: function() {
        this.api().search(getUrlVars()['search']).draw();        
      },

- ? Search = any & URL .

0

All Articles