Define data type of a column, auto-select conditions for SearchBuilder
Define data type of a column, auto-select conditions for SearchBuilder
Is there a way to tell datatables about the type of data a column can contain? For example, 'year' is an integer, publishDate is a date, hasSomething is a boolean, title is a string. When those are defined as searchBuilder fields, is there a way that the conditions are automatically displayed? Duck-typing won't work, because the data comes from the API.
I thought maybe 'type': https://datatables.net/reference/option/columns.type but according to the docs this has no effect for server-side scripts.
And of course, I keep thinking there must be a way to define these within the column itself, rather than as a def that points to the column indexes.
Psuedo-code:
columns: [
{ data: 'year', type: 'int', searchBuilder: { conditions: ['<=', '=','>=','between'} }
This is what I'm getting now, I keep thinking that if datatables knew that year was an it, it would at least pick appropriate defaults for searchBuilder:
Answers
You can predefine the type using :
columns.searchBuilderType
.I never used server-side before, but it seems like you need to more things than client-side, see: https://datatables.net/extensions/searchbuilder/serverside
Thanks. I don't think this is the right spot (it's not having any effect).
new DataTable('#myTable', {
columns: [{ searchBuilderType: 'num', searchable: false }, null, null, null, null]
});
Are there any examples that don't use columnDefs, which are powerful when defining properties over multiple columns, but since I'm creating each column from data that's passed in, it's clearer if everything can just be created the column itself.
new DataTable('#myTable', {
columns: [
className: 'year',
sortable: true,
searchBuilder: true,
searchBuilderType: 'num',
searchPanes: ...
}
]
Is there a way to get all the column options? I could likely figure it out then. I'm not sure where searchBuilderType: 'num' should go.
Technically there is no difference between using
columns
andcolumnDefs
. Typically, as you mentioned, is used to define the same config options for multiple coluimns. There are other cases where its useful as well. See the conflict resolution section to understand how conflicts betweencolumns
andcolumnDefs
is worked out.Take any example using
columnDefs
and simply place that code in the appropriate column. Here is a simple example:https://live.datatables.net/yaroyala/74/edit
Start date has two columns. One using
columns.searchBuilderType
to set the date column as a string and the other remains the default. The default uses the datetime extension to allow searching for dates. TheStart date string
column allows for searching by string. This seems to work.If
year
is being detected as a string then there must be something in the JSON response, for that column, that contains something other than a number. You can not force a type tonum
for example if there is data in that column that is not a number. Please post an example JSON response, using the browser's network inspector, so we can see the values returned foryear
.Kevin
See: https://datatables.net/extensions/searchbuilder/examples/customisation/renderArrays.html