DataTables Link to Filter/Search


/ Published in: jQuery
Save to your folder(s)

Uses the dataTables filter/search to provide a link to the term being filtered and copies a link to clipboard via ZeroClipboard. Requires the getURLParam, ZeroClipboard and dataTables jQuery plugins.

http://www.mathias-bank.de/jQuery/jquery.getParams.js

http://www.datatables.net/

http://code.google.com/p/zeroclipboard/


Copy this code and paste it in your HTML
  1. <script type="text/javascript" src="path/to/jquery.dataTables.min.js" ></script>
  2. <script type="text/javascript" src="path/to/jquery.getURLParam.js" ></script>
  3. <script type="text/javascript" src="path/to/ZeroClipboard/ZeroClipboard.js"></script>
  4. <script>
  5. jQuery(function() {
  6. jQuery(".dataTables_filter :input").after("<button id='clipLink'>Link to search</button>"); // set button for copy
  7. var oSearch = $.getURLParam("search"); //utilize the getURLParam plugin to return the ?search= param(s)
  8. e = jQuery.Event("keyup"); //set enter event
  9. e.which = 13 //enter key
  10. if (oSearch != null) { //must be null and not blank for a url
  11. jQuery(".dataTables_filter :input").val(oSearch.replace(/%20/g, ' ')).trigger(e)// find all url encoded spaces and replace them, fabricate enter press
  12. }
  13.  
  14. jQuery("button#clipLink").zclip({
  15. path: "path/to/ZeroClipboard/ZeroClipboard.swf",
  16. copy: function(){
  17. if(jQuery(".dataTables_filter :input").val() == "") {
  18. alert("please enter a search term");
  19. return false;
  20. } else {
  21. var pathname = window.location.href;
  22. searchTerm = jQuery(".dataTables_filter :input").val();
  23. result = pathname.substring(5, pathname.indexOf('/')) + "path/to/datatables/page/?search=" + searchTerm;
  24. jQuery("#copiedLink").val(result);
  25. return jQuery("#copiedLink").val();
  26. }
  27. }
  28. });
  29. });
  30. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.