Custom column search logic

Custom column search logic

FeraudFeraud Posts: 20Questions: 6Answers: 0

Hi,

I want to search a column with stock symbols (like MSFT for Microsoft). On exakt match, the search shall return just this single stock. E.g. the user types "O" and a symbol "O" exists. Only this stock is shown. The user continues typing "OF" and now two stocks are shown "OFA" and "OFB". The user continues typing "OFA" and only this stocks is shown.

Using colum.search(...), I guess I cannot handle this custom logic, because only search strings can be defined. I also want to avoid the $.fn.dataTable.ext.search-array because I have multiple tables and the search logic of the web site becomes hard to maintain adding all search logic to the global search array.

Regards,

Torsten

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi Feraud,

    Yep, you can create a custom search which when triggered calls one of the search methods - this can be on the entire table, a single column, or a set of columns. If you specify a regex search (true for the second parameter), and wrap the search text between a '^' (start of text) and a '$' (end of text), you'll get the behaviour you want!

    So, something like

    $('#example').DataTable().column(0).search('^' + searchString + '$',true).draw()
    

    Hope that helps,

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Not sure I understand the question but maybe this example will help you:
    https://datatables.net/examples/api/regex.html

    Kevin

  • FeraudFeraud Posts: 20Questions: 6Answers: 0

    @colin, @kthorngren: thanks for answering.

    Defining a RegEx executes always the same logic. I need an "if".

    If the search encounters a complete hit (as in @colin's regex) only this row is shown.
    If there is no complete hit, all rows containing search expression are shown.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Ah, sorry @Feraud, yep, I missed that bit about the "OF" showing the two.

    Take a look at this live example, this should do what you're looking for.

    C

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    @Feraud - please reload if you've looked at it already, I've made a slight tweak. This is the final code here, for prosperity!

    $(document).ready( function () {
      var table = $('#example').DataTable({
        "dom": '<"top"i>rt<"bottom"><"clear">'
      });
      
      $('#mySearch').on( 'keyup click', function () {
            table.column(0).search('^' + $(this).val() + '$', true);
            if (table.page.info().recordsDisplay != 1) {
              table.column(0).search('^' + $(this).val(), true);
            }
        
            table.draw();
        } );
    } );
    
  • FeraudFeraud Posts: 20Questions: 6Answers: 0
    edited February 2018

    @colin: That looks very, very promising. I will check it out during the week-end.

    I wasn't aware about recordsDisplay. Nice 2 step procedure!

    Will inform you about the final outcome :smile:

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Excellent, don't work too hard over the weekend! Shout if you have any more questions... :)

    C

  • FeraudFeraud Posts: 20Questions: 6Answers: 0

    It's not work - it's fun using datatables!

  • FeraudFeraud Posts: 20Questions: 6Answers: 0
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi Torsten,

    Nice, that looks good! Glad to see you got it up and running! :)

    Cheers,

    Colin

This discussion has been closed.