Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't filter datatable by selected value in dropdown

I am using the jQuery plugin DataTables. I have a table of data that has HTML inputs and selects. When I use the DataTable search filter to filter the results and I search for all dropdowns that have the selected value of 'Open', nothing changes.

I believe this is happening because every dropdown in the table has the same options and the filter is searching on them and returning all results, since they all match.

How can I get the filter to search on only the selected value and not all options of the dropdown?

I have tried to find a solution, but all I can find are results like these :

  • Dropdown filter jquery datatables
  • CustomFilter

These all deal with adding custom filters for each column, I just want to use the existing DataTable filter.

Example

Live example of the problem, Search for 'Open' or 'Closed'

Code

<table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input name="name" type="text" value="Need more memory" id="name1"></td>
                <td><select name="status" id="status1">
                        <option value="2">Closed</option>
                        <option selected="selected" value="1">Open</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><input name="name" type="text" value="Can't connect" id="name2"></td>
                <td><select name="status" id="status2">
                        <option selected="selected" value="2">Closed</option>
                        <option value="1">Open</option>
                    </select>
                </td>
            </tr>
        </tbody>
</table>
like image 509
FarFigNewton Avatar asked Nov 03 '22 12:11

FarFigNewton


1 Answers

Now, you can use a data-search attribute on the <td>-element with data-tables. ref

<tr>
    <td>
       <input name="name" type="text" value="Need more memory" id="name1">
     </td>
     <td data-search="Open">
        <select name="status" id="status1">
          <option value="2">Closed</option>
          <option selected="selected" value="1">Open</option>
        </select>
     </td>
    </tr>
<tr>

fiddle

my similar question on datatables.net

like image 136
Frank Avatar answered Nov 12 '22 11:11

Frank