Pre filter data table result

Pre filter data table result

GalileoGalileo Posts: 9Questions: 2Answers: 0

I have made a data table by using PHP PDO. Due to some technical difficulty i need to query more rows as i want to show in my data table. So i am looking for a possibility to pre filter or to limit the shown data in my table even the PHP query is having more rows available. In my case i have made a "status" columns for each line. One is "open and the other is "closed": iN MY PHP query i fetch both of them but i only want to show "open" or "closed" in my table. Is there any possibility or script to limit or pre filter the data table output?

This question has an accepted answers - jump to answer

Answers

  • GalileoGalileo Posts: 9Questions: 2Answers: 0

    In the meanwhile i have found:

    $('#example').dataTable( { "search": { "search": "Fred" } } );

    How ever i am not able to implement it. Is this not correct?:

    <table class="table table-hover example " id="dataTable" name="example" width="100%" cellspacing="0">

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    id="dataTable" name="example" 
    

    "example" is the table id, not the table name.

  • GalileoGalileo Posts: 9Questions: 2Answers: 0

    OK but where i have to put dataTable then?

    <table class="table table-hover" id="example" width="100%" cellspacing="0">

  • kthorngrenkthorngren Posts: 21,262Questions: 26Answers: 4,934
    Answer ✓

    OK but where i have to put dataTable then?

    There is no need to. See this basic example. Click on the HTML table to see the table element.

    Here is an example of using search.search with ajax loaded data:
    http://live.datatables.net/xevotezi/2/edit

    Kevin

  • GalileoGalileo Posts: 9Questions: 2Answers: 0
    edited April 2021

    Dear Kevin,
    many thanks for your clear and detailed answer. It works but it is not 100 % what i am looking for.

    This solution (Ajax loaded example) shows the filter value. Is there a chance to do a filter in background and give the user the possibility still to use the filter box ? At the end i would like to have the table pre filtered but not showing the filter value in the box. Otherwise the user can easily remove the filter.

  • GalileoGalileo Posts: 9Questions: 2Answers: 0
    edited April 2021

    Solved it by my self. I have just removed the text filter value and replaced it by an php attribute of my filter value and it works. Many thanks!

    search: $filter_value

  • kthorngrenkthorngren Posts: 21,262Questions: 26Answers: 4,934

    The best approach is to filter the data you don't want displayed at the server. Maybe you can do something after your initial query.

    Or you can filter the data using Javascript in the client before the data is loaded into the table. Not sure how you are loading the table to give suggestions.

    Or you can create a search plugin to filter the rows you don't want to display. The row data is still in the Datatables cache and available to be accessed outside of the web page, ie, in the browser's console. Also, by default, the info element will indicate these rows exist but are filtered, from my example:

    Showing 1 to 1 of 1 entries (filtered from 57 total entries)

    Kevin

This discussion has been closed.