Datatable not filtering initially

Datatable not filtering initially

neo2810neo2810 Posts: 4Questions: 1Answers: 0
edited September 2019 in Free community support

I'm using Datatables in 2 areas on my site. The first area the table is within a single page and works perfectly.
On the second area, I have a page which, when a button is clicked, opens a new page into a Div section on the main page. Within this "results" page, I have my table and script to call the function. I also had to add a <head> section to call the .js script even though that is in the main page <head> section. I assume the "results" page acts like a separate page to the main.
Anyway, when my table loads, it gives me the dropdown and search bar but loads all the table rows, regardless of whether I set "iDisplayLength" and/or a HTML data-page-length of 10.

For now, I've set the data-page-length and iDisplayLength to 5 so that the dropdown is empty on load. When I click and select an option (say 10) in the dropdown, then it filters correctly and gives me pagenation. It just wont filter on initial load.

It's driving me nuts and I really don't get why its doing that?

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Anyway, when my table loads, it gives me the dropdown and search bar but loads all the table rows, regardless of whether I set "iDisplayLength" and/or a HTML data-page-length of 10.

    Hard to say without seeing what you are doing. Best case is to provide a link to your page or a test case replicating the issue. IF you can't do that then please post the Datatables code so we can see what you have. Maybe a screenshot of the initial page.

    I would check the browser's console for errors. That could cause the table to not populate or format as you expect.

    Kevin

  • neo2810neo2810 Posts: 4Questions: 1Answers: 0

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Please post your Datatables initialization code. And let us know if you get any errors in your browser's console.

    Kevin

  • neo2810neo2810 Posts: 4Questions: 1Answers: 0

    List schedules buttons calls this script:

    public function findHubFlight()
    {
    $params = array();
    
    if ($this->post->code != '') {
    $params['s.code'] = $this->post->code;
    }
    
    if ($this->post->depicao != '') {
    $params['s.depicao'] = $this->post->depicao;
    }
    
    $params['airline.enabled'] = 1;
    $params['s.enabled'] = 1;
    $this->set('allroutes', SchedulesData::findSchedules($params));
    $this->render('airline_results.tpl');
    }
    

    Results page which should only list the first 10 results with pagenation has this relevant code:

    <head>
        <script type="text/javascript" src="<?php echo fileurl('lib/js/jquery.dataTables.js');?>"></script>
    </head>
    
    <script type="text/javascript">
        $(document).ready(function() {
            $('#tableschedule').dataTable( {
                paging: true,
                "sPaginationType": "full_numbers",
                "iDisplayLength": 5
            } );
        } );
    </script>
    

    ```

    <

    table style="text-align: left" cellspacing="0" id="tableschedule"
    class="tablesorter styled" data-page-length="5">
    <thead>
    <tr>
    <th colspan="2">Flight</th>
    <th>Aircraft</th>
    <th>Distance</th>
    <th class="last"></th>
    </tr>
    </thead>
    <tbody>
    <?php
    foreach($allroutes as $route)
    {

    <?php > <?php echo $route->code.$route->flightnum?>
    <?php echo $route->depicao.' » '.$route->arricao.'

    '. str_ireplace(array("airport", "international", "airfield"), '', $route->depname).' » '. str_ireplace(array("airport", "international", "airfield"), '', $route->arrname); ?> imagecode); ?>.png" width="80px"> <?php echo $route->aircrafticao; ?> <?php echo round($route->distance);?> id);?>"> Go <?php } > ``` ?>
  • neo2810neo2810 Posts: 4Questions: 1Answers: 0

    No console errors

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @neo2810 ,

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.