Im stumped - I just can't get this default filter to work..... :-)

Im stumped - I just can't get this default filter to work..... :-)

Gaz96Gaz96 Posts: 20Questions: 6Answers: 1
edited March 2016 in Free community support
$(document).ready( function () {
    $('#naps').DataTable({
            "columnDefs": [
                { "type": "date-uk", targets: 0 }]
    });
   
} );
</script>
<script>
$(document).ready(function() {
  var oTable = $('#naps').dataTable();
  var myFilter = $_SESSION['username']
  
  oTable.fnFilter( myFilter, 1 );
} );
</script>

Answers

  • allanallan Posts: 63,768Questions: 1Answers: 10,510 Site admin

    Looks like it should work to me (assuming you output the PHP variable correctly to Javascript - the above code wouldn't work since it would be invalid Javascript - I'm not sure if that is a typo?). Can you link to a test case please.

    Allan

  • Gaz96Gaz96 Posts: 20Questions: 6Answers: 1

    Hi

    There is a typo - there should be a ; at the end of line 12 but even after adding it it doesn't work. I tried putting apostrophes around $_SESSION['username'] but no joy.

    [sigh]

  • Gaz96Gaz96 Posts: 20Questions: 6Answers: 1

    Just had a thought - using .fnfilter doesn't require any extra prior referencing does it??

  • allanallan Posts: 63,768Questions: 1Answers: 10,510 Site admin

    Your session variable isn't going to be interpreted by PHP at all. If you look at the console in your browser, do you not see a syntax error? We'd really need a link to a test case showing the issue, per the forum rules, to be sure, but I would imagine you'll need to do something like:

      var myFilter = <?php echo json_encode( $_SESSION['username'] ); ?>;
    

    Allan

  • Gaz96Gaz96 Posts: 20Questions: 6Answers: 1

    Morning. I just tried the syntax you suggested but no luck. The problem is it's not throwing an error - the page loads just fine and so does the table, but displaying all records without the filter applied. Exactly what can of a link do you need.....?

  • allanallan Posts: 63,768Questions: 1Answers: 10,510 Site admin

    A link to the page so I can take a look and see what is going wrong.

  • Gaz96Gaz96 Posts: 20Questions: 6Answers: 1
    edited March 2016

    It's in a kind of sandpit. You'll need to go here:
    http://www.spyringcentral.co.uk/garywhite_glw/login.php
    Login with the username {removed}, password {removed}
    Then go to the competitions page where the naps table loads and the filter is supposed to restrict it to showing only records for him......

  • Gaz96Gaz96 Posts: 20Questions: 6Answers: 1

    I know whats wrong - can I send you a screenshot....?? The variable myFilter is not populating......... The code is correctly reading the session variable but nothing..........

  • allanallan Posts: 63,768Questions: 1Answers: 10,510 Site admin

    In this rendered code:

       var oTable = $('#naps').DataTable();
      var myFilter = "Gaz96";
      var myFilter = Gaz96;
      oTable.fnfilter("Gaz96");
    

    there are two issues:

    1. Remove line 3 - it is not valid Javascript
    2. Use oTable.search( myFilter );

    the legacy fnFilter method is not available if you use the $().DataTable() form.

    Allan

This discussion has been closed.