Jana
Jana

Reputation: 21

Bootstrap-Table Custom Search

I have a custom search that looks like this, but how can I use it in all columns and not just colName column?

https://live.bootstrap-table.com/code/janttila/5112

function customSearch(data, text) {
    return data.filter(function (row) {

        return  row.colName.toLowerCase().replace(/\s/g, "").indexOf(text.toLowerCase().replace(/\s/g, "")) > -1

    })
  }

Upvotes: 2

Views: 2390

Answers (1)

Alexander Riedel
Alexander Riedel

Reputation: 1359

you could iterate through all the columns. But maybe provide some more code to get an valid solution. I don't know how your data object exactly looks like.

//EDIT: regarding your data structure, the function will look like this:

    function customSearch(data, text) {
return data.filter(function (row) {
 
  for (let items of Object.values(row)){
    if (items.toString().toLowerCase().replace(/\s/g, "").indexOf(text.toLowerCase().replace(/\s/g, "")) > -1){
    return true
  }
  }
})
}

we iterate thrugh all the evalues in row and check if any of that suits the search text. But be aware, your dataset has the entry amount as well which you are not displaying. This one will be also included for the search

//EDIT2:

function customSearch(data, text) {
    return data.filter(function (row) {
        searchFor = ["id", "name", "price"]

        for (elem of Object.keys(row)) {
            if (searchFor.indexOf(elem) < 0) {
                delete row[elem]
            }
        }

        for (let items of Object.values(row)) {
            if (items.toString().toLowerCase().replace(/\s/g, "").indexOf(text.toLowerCase().replace(/\s/g, "")) > -1) {
                return true
            }
        }
    })
}

Upvotes: 2

Related Questions