Defining the search value for a column

Defining the search value for a column

midetrimidetri Posts: 1Questions: 1Answers: 0

I can't believe I spent the last hour for searching without success an in my opinion simple and common problem: All I want is to define the value that shall be used for DT to search in a specific column. In other words, if you set up a table in static HTML you can define via https://datatables.net/examples/advanced_init/html5-data-attributes.html HTML data-attributes. But how do you do it if you do NOT use static HTML, when DT gets it's rowset data from a JavaScript variable?

columns: [{ title: 'Date', data:{"_": 'hrDate', 'sort': 'timestamp'} }]

works fine for setting sorting value. But how do I set the search value (equivalent to HTML attribute data-search)?

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    Use columns().search()
    https://datatables.net/reference/api/columns().search()

    Example:
    I have a couple of buttons that search for a certain value in only one column that is identified by an id in the HTML. I turned off "smart search" because in my use case it doesn't make sense.

    This is what it looks like:

    ...
    buttons: [
        {   text: lang === "de" ? "Alles" : "All",
            action: function ( e, dt, node, config ) {
                dt.columns("#qaStatus").search("").draw();
            }
        },
        {   text: lang === "de" ? "Nur Test" : "Test only",
            action: function ( e, dt, node, config ) {
                dt.columns("#qaStatus").search("test version", false, false).draw();
            }
        },
        {   text: lang === "de" ? "Nur Live" : "Live only",
            action: function ( e, dt, node, config ) {
                dt.columns("#qaStatus").search("live version  ", false, false).draw();
            }
        },
        {   text: lang === "de" ? "Nur vorherige Live" : "Previous Live only",
            action: function ( e, dt, node, config ) {
                dt.columns("#qaStatus").search(" live version", false, false).draw();
            }
        }
    ] ...
    
  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited October 2022

    You don't have your syntax correct for the Orthogonal data. Instead of using columns.data you need to use columns.render. See the Predefined values section.

    Kevin

Sign In or Register to comment.