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,840Questions: 26Answers: 5,049
    edited March 17

    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)

  • allanallan Posts: 64,230Questions: 1Answers: 10,599 Site admin

    Great addition I reckon :). I've added it into the plug-in. Nice one Kevin.

    Allan

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049
    edited March 18

    @allan I was looking at creating a PR but was unsure what all needed changing and just hadn't got around to asking yet. I figured features/fuzzySearch/src/dataTables.fuzzySearch.ts is the only place to change but wasn't sure how to handle this part:

    import DataTable, {ColumnSelector} from 'datatables.net';
     
     declare module 'datatables.net' {
        interface Config {
            fuzzySearch?:
                | boolean
                | {
                        columns?: ColumnSelector;
    

    Does ColumnSelector include all the options at column-selector with initial.columns being an array of indexes representing the column-selector ?

    Kevin

  • allanallan Posts: 64,230Questions: 1Answers: 10,599 Site admin

    Does ColumnSelector include all the options at column-selector

    Yes.

    with a return of an array of indexes when using initial.columns?

    No return type as it isn't a function. It is a TypeScript type. In this case it means that columns can be an array of indexes, a string, etc. Resolving it into an array of indexes is done later by columns().indexes().

    So... with that said, I realise the error in making it a column selector, rather than just an array of indexes, which I guess is what you were thinking? There isn't a column index resolver from the options at the moment.

    I've just been tinkering around with the plug-in, and it needs some work. It doesn't register for layout and I don't like how a few things are done in it. It also needs to be updated to how I build plugins, and the CSS pulled into a file.

    Hah - I thought this was going to be a quick commit! Its going to turn into a rewrite. Doh :)

    Allan

  • kthorngrenkthorngren Posts: 21,840Questions: 26Answers: 5,049
    edited March 18

    rather than just an array of indexes, which I guess is what you were thinking?

    Yes, I thought the definition would be columns?: array;. But I don't know TS so not sure if that is valid.

    I thought this was going to be a quick commit! Its going to turn into a rewrite. Doh

    LOL, and don't forget the Fuzzy Search blog - just kidding.

    Kevin

  • allanallan Posts: 64,230Questions: 1Answers: 10,599 Site admin

    columns?: array; would do, although columns?: number[]; would probably be more appropriate.

    I was wondering what I should blog about this month - this update might be it...

    Allan

Sign In or Register to comment.