Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * JQUERY AJAX FILTER TABLE [UPDATED]
- */
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <link rel="stylesheet" href="assets/css/custom.css">
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Ajax Filter table</title>
- <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- <link rel="stylesheet" href="//cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css">
- <script src="//cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
- <script>
- $(document).ready(function(){
- //When the select box changes we get the value from it. Then we do AJAX to get the staffs with this value.
- //On success we parse data into variable and append it to table.
- var filter = $("#filter").val();
- $("#table").DataTable({
- "ajax":{
- "url":"fetch.php?filter="+filter,
- "dataSrc":"",
- "type":"GET"
- },
- "columns":[
- {data: 'id'},
- {data: 'first_name'},
- {data: 'last_name'},
- {data: 'type'}
- ]
- });
- $( "#filter" ).change(function() {
- //javascript:location.reload(true);
- window.location.reload(false);
- });
- });
- </script>
- <style>
- .checkbox {
- margin-right : 5px;
- font-size:16px;
- }
- input[type="checkbox"]{
- margin-right : 5px;
- }
- table {
- margin-top:10px;
- }
- table tr {
- border-bottom: 1px solid #ddd;
- }
- table tr th {
- text-transform:uppercase;
- padding:5px 10px;
- }
- table tr td {
- padding:5px 10px;
- }
- table tr:nth-child(odd){
- background:#eee;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="jumbotron">
- <h1> Jquery Ajax Filter</h1>
- </div>
- </div>
- <div class="container">
- <div class="col-md-8 col-md-offset-2">
- <select id="filter">
- <option value="all" name="" >All </option>
- <option value="intern" >Intern</option>
- <option value="employee" >employee</option>
- <option value="temp" >temp</option>
- </select>
- </div>
- <div class="col-md-8 col-md-offset-2" >
- <?php //Empty table filled by JS above ?>
- <table class="table table-striped" id="table">
- <thead id="thead">
- <tr>
- <th>ID</th>
- <th>First name</th>
- <th>Last name</th>
- <th>Type</th>
- </tr>
- </thead>
- <tbody id="tbody"></tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
Add Comment
Please, Sign In to add comment