How to Set/Filter a View Via JSON Data
How to Set/Filter a View Via JSON Data

Hi All,
I have a basic table, pulling data from a text file. For each person's name, I would like to assign one of three types to that person, but not show the types in the table. I am trying to build a dropdown/filter that would show these three types. The dropdown would look like this:
All
Filter1
Filter2
Filter3
And flipping those dropdown values would update the view of the table on the fly, showing either all the names or a subset of them. I'm not sure how to implement this both in the data file, and within the HTML itself.
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th> </th>
<th>Name</th>
<th>Total</th>
<th>Prologue</th>
<th>Stage 1</th>
<th>Stage 2</th>
<th>Stage 3</th>
</tr>
</thead>
<tfoot>
<tr>
<th> </th>
<th>Name</th>
<th>Total</th>
<th>Prologue</th>
<th>Stage 1</th>
<th>Stage 2</th>
<th>Stage 3</th>
</tr>
</tfoot>
</table>
{
"data": [
[
"img",
"Tiger Nixon2",
"50",
"25",
"25",
"25",
"25"
],
[
"img",
"Tiger Nixon2",
"50",
"25",
"25",
"25",
"25"
]
]
}
Any help is appreciated. Thank you.
This question has an accepted answers - jump to answer
Answers
Here is a select filter example you can start from.
You can use
columns.visible
to hide the column. Usecolumns.render
in the hidden column to render thefilter1
, filter2, etc values as per your requirements. Set the select filter event to use
-api column().search()for the hidden column. Use
column().search( "" )for the
All` option.If you need further help please build a simple test case showing what you hae so we can provide more specific help.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thanks Kevin. This got me started in the right direction. Here's my sample code:
http://live.datatables.net/yohuluco/1/edit
I've hidden the one column and now have a dropdown/filter for it, but a couple things I'm trying to still do:
Have the dropdown/filter be up next to the search input and display "All" by default in the dropdown.
Only have one filter for this specifically, not every column of data
As a bonus, be able to set a CSS class to each of the three dropdown values, so I can apply a different color to each name (relative to the dropdown type), when displaying "All" results.
Thanks a bunch for your help.
Ed
You can place the input anywhere you like. This example will show how to place elements with the Datatables elements.
The
columns()
API supports manycolumn-selector
options. You can do something like this for column 1:Kevin