Filter information from the datatable

Filter information from the datatable

juansowjuansow Posts: 6Questions: 3Answers: 0

How can i filter information in a datatable, for example, show all data except null valor information, i tried but i dont understand the filter example in database.net.

this is my html code.

<!DOCTYPE html>
<html>
<head>
    

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css"/>
  <link href="https://cdn.datatables.net/buttons/1.3.1/css/buttons.dataTables.min.css " rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
  <script src="https://cdn.datatables.net/buttons/1.3.1/js/dataTables.buttons.min.js"></script>
  <script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js"></script>

  <title>Prueba datatable</title>
  
  <script>
  $(document).ready(function(){
    $.getJSON( "http://jsonplaceholder.typicode.com/posts", function( data ) {

    var t = $('#data-table').DataTable({

       "data" : data,
       columns : [
       {"data" : "userId"},
       {"data" : "id"},
       {"data" : "title"},
       {"data" : "body"}
       ],
       "lengthMenu": [[10, 15, 25, 50, -1], [10, 15, 25, 50, "All"]],
       "pageLength": 25,

       $filteredData = t
       .column(0)
       .data()
       .filter( function(value, index){
        return value = null false : true;

       
       }) ;    
      });
    });
  });

  </script>
</head>
<body>

 <table id="data-table" class="table table-bordered" width="100%">
  <thead>
    <tr>
      <th>userId</th>
      <th>id</th>
      <th>title</th>
      <th>body</th>
    </tr>
  </thead>


</table>

</body>

</html>

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    The filter() API does not affect the display of the table. It is used to get a filtered set of data that can be used within your Javascript script.

    You are trying to use the API within the Datatables initialization code which is not the right place to put it. You are probably getting a syntax error in your browser's console.

    If you don't want null data in your table the best place to filter the data is in your server script (http://jsonplaceholder.typicode.com/posts).

    Are you trying to remove the null data from the table or do you want this data in the table with the option to filter it using search?

    Kevin

  • juansowjuansow Posts: 6Questions: 3Answers: 0

    I'm triying to show for example, data information with less valor than 10 in the selected column and hide that information for search, so if i'm puting the filter() in the bad place, you can give me an example, where i need to put it on or how i can use it in my code.

    thanks for your answer.

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
    edited August 2018 Answer ✓

    Not sure I'm totally clear on what you want but you can use searchCols to initially filter the table. Or you can create a search plugin that can be used to filter the table based on your criteria no matter what additional searches someone uses.

    Here is an example of using a search plugin for filtering null data in the second column:
    http://live.datatables.net/fosidapu/1/edit

    As mentioned in the documentation using filter() does not actually filter the table data. It just provides a set of data that you assign to a variable to use within your Javascript.

    Hopefully this will get you started. Please post any questions.

    Kevin

  • safaviasafavia Posts: 36Questions: 14Answers: 0
    edited August 2018

    Search plugin seems to be a good solution. Thanks!

    Cheers,
    Alan

  • juansowjuansow Posts: 6Questions: 3Answers: 0

    @kthorngren thanks for your answer, i'm sure what to do now, if i have more question, can i ask you for answers in direct message?.

    Thanks a lot =D.

This discussion has been closed.