An internal function to filter (search) by type === 'sort'

An internal function to filter (search) by type === 'sort'

andrew45andrew45 Posts: 24Questions: 8Answers: 0

I have a birthday_column in my Datatable. The raw data are like "2022-05-19", but users see and filter data like "May 19, 2022":

{  targets: [number_of_birthday_column],        
        "data": "date_of_birthday",
        "render": function ( data, type, row, meta ) { 

        if (type === 'sort') 
        {
            return data;    // 2022-05-19
        }
        else
        {           
            return dateToText(data); // May 19, 2022
        }  
       }            
}

I need an internal DataTable function to filter (search) by raw data ("2022-05-19"), but the rest must work the same as now (displaying "May 19, 2022" and filtering by "May 19, 2022" for users should remain without change).

Is it possible? Is there a working code example to see?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,136Questions: 1Answers: 10,581 Site admin

    I need an internal DataTable function to filter

    What's the input for your "internal" search? You could use a custom function, in which case you are in control of the data and format and the end user can filter as a string on the rendered data.

    Allan

  • andrew45andrew45 Posts: 24Questions: 8Answers: 0

    The input for "internal" search is like 2022-05-19. I don't know how to perform such searches because 'filter' type contains data like May 19, 2022 and I don't want to change this.

  • kthorngrenkthorngren Posts: 21,786Questions: 26Answers: 5,040
    edited September 2024 Answer ✓

    One option is to use search.fixed() or column().search.fixed() with a function to compare the "internal" search term to the original data using a function. See this example with your column definition.
    https://live.datatables.net/yefusayo/1/edit

    It uses a different raw date format of 2011/04/25. To try it type oct in the global search input to see all the dates with Oct. Next type 2009/10/09 in the Internal search input to see the table futher filtered to show one record.

    The key code is this:

        table.column(4).search.fixed('internal', (cell, data) => {
          return data.date_of_birthday === internalSearch;
        }).draw();
    

    It compares the Internal Search input value, ie internalSearch, to the raw date data in each row.

    Kevin

  • allanallan Posts: 64,136Questions: 1Answers: 10,581 Site admin

    Yup - what Kevin says :)

Sign In or Register to comment.