'Jquery datatables custom search function on JSON data
This datatable search function looks within a data field gates which is represented in JSON as
{ "completed": [], "na": [], "open": []}
The filter function is trying to look within a the "completed" key of the gates object for an for an entry matching "TestGate", which indicates whether the testing state was completed or not. We use this result for the search filter (checkbox toggle):
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
if ($("#checkboxRRComplete").is(":checked")) {
let gates = data[5] || {}
console.log(gates) \\"[obj Object]" need gate to be a JS object
return gates.completed.includes("TestGate")
}
return true
}
)
The problem here is that function above the data input is casted to a string. So in the function scope it is "[obj Object]" and the object fields are no longer accessible. Is there a way to create custom search criteria that is JSON object friendly?
NOTE: Since running into this issue I decided to just add another field to my ajax request response to be used for filtering. However, I am still wondering if there is a better way to do this by searching objects.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
