FuzzySearch on selected columns

FuzzySearch on selected columns

Spozz21Spozz21 Posts: 2Questions: 0Answers: 0

I want to use FuzzySearch but only on selected columns

  • I have a table with FuzzySearch.
  • I need to exclude some of the columns as they are producing unwanted results.
  • I need these columns to remain searchable so I can continue using them to produce filter buttons.

Search input function

$("#search_keywords").keyup(function (e) {
    if(e.which == 13) {
        showTable();
    }
    
    // Search and draw (below)

});

Search and draw - Option 1 and 2 work

1. DataTables search on columns 13 and 2:
    table.column([13,2]).search($(this).val()).draw();

2. FuzzySearch search on whole table:
    table.search.fuzzy($(this).val()).draw();

3. I want this option to work but it doesnt:
    table.column([13,2]).search.fuzzy($(this).val()).draw();

Error code when using option 3

DataTable_JS.js:181 Uncaught TypeError: table.column(...).search.fuzzy is not a function
    at HTMLInputElement.<anonymous> (DataTable_JS.js:181:33)
    at HTMLInputElement.dispatch (student-vendor.min.js:1:44187)
    at v.handle (student-vendor.min.js:1:40920)

Thank you in advance.

Replies

  • kthorngrenkthorngren Posts: 21,802Questions: 26Answers: 5,043
    edited 1:42AM

    I added a fuzzySearch.columns option to the Fuzzy Search plugin, for example:

    var table = new DataTable('#example', {
        fuzzySearch: {
          columns: [0, 1, 2]
        }
    });
    

    I made two changes to the code in the fuzzySearch() function. First is to get the defined columns:

            // Get columns definition
            var columns = initial.columns !== undefined ? initial.columns : null;
    

    Second is to compare only the cells in the defined columns:

            // Going to check each cell for potential matches
            for(var i = 0; i < data.length; i++) {
              
                // Compare only specified columns
                if ( columns === null || columns.includes(i) ) {
    

    The modified plugin code is in the Javascript tab. My basic testing seems to work.
    https://live.datatables.net/paraqipi/1/edit

    Note: the columns option supports providing only an array of column indexes to fuzzy search.

    Kevin

  • Spozz21Spozz21 Posts: 2Questions: 0Answers: 0

    Amazing work! Thank you for adding that in.

    All testing well from this end. B)

Sign In or Register to comment.