URL detection and search with server-side processing

URL detection and search with server-side processing

east1999east1999 Posts: 31Questions: 11Answers: 0

When migrating my current code (client-side) to server-side processing, one of the functions that broke down was my URL detector. This allows DT to tie url changes to search, so they can be linked to later on.

Mind you, search still works. table.columns(1).search(key).draw() works just fine on other parts of the code, but not here. The code is triggered by initComplete, while other searches are triggered by user input.

var url      = decodeURI(window.location.href);
var urlcheck = url.includes("=");
var urlauts = url.includes("aut");
var urlpubs = url.includes("pub");
var urltags = url.includes("tag");
var strsplit = url.split("=").pop();
var strspaced = strsplit.replace(/\+/g, " ");


var detectSearchParams = function () {

    if (urlcheck == true) {

      $('.form-control').val(strspaced);

      if (urlauts == true) { // Detect authors url
          table.columns(1).search ( strspaced ).draw();
          $('.searchcat').not($('.auths')).removeClass('active');
          $('.auths').addClass('active');
          $('#gosearch').html('<i class="fas fa-search"></i><span>Authors</span>');
        }

      else if (urlpubs == true) { // Detect publications url
          table.columns(6).search( strspaced ).draw();
          $('.searchcat').not($('.pubs')).removeClass('active');
          $('.pubs').addClass('active');
          $('#gosearch').html('<i class="fas fa-search"></i><span>Publications<span>');
          }
    else if (urltags == true) { // Detect keywords url
          table.columns(4).search( strspaced ).draw();
          $('.searchcat').not($('.keys')).removeClass('active');
          $('.keys').addClass('active');
          $('#gosearch').html('<i class="fas fa-search"></i><span>Keyword</span>');
          }
      else if (urlauts == false && urlpubs == false && urltags == false ) { // Detect general url
          $('#gosearch').html('<i class="fas fa-search"></i><span>Geral</span>');
          table.search( strspaced ).draw();
          }
      }
}

This question has accepted answers - jump to:

Answers

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

    Its difficult to say without a full test case, but I would suggest that you should use search if you want to specify an initial search value rather than using search(). My guess is that the DataTable is still initialising while you call the method, but as I say, without a test case I can't know for sure.

    Allan

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

    One other thing, you might be interested in this blog post which does something similar.

    Allan

  • east1999east1999 Posts: 31Questions: 11Answers: 0
    edited January 2019

    Thank you, I did pass by that post when I was starting, but I got the idea that it can only be used to do global search. Is that right?

    Anyway, you were right! I was actually trying to add a second call to initialisation, which was ignored by the server. In fact there was no need to have this in initComplete, so I moved it to be the first rendered thing after the table (with $(window).on.('load').

This discussion has been closed.