• We just launched and are currently in beta. Join us as we build and grow the community.

Assign a New Search Box for Table Outside of DataTable

jusit

Wit Warrior
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
193
Likes
47
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Creating our Database

First, we create the database that contains our sample data.

I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database.

You should be able to create a database named mydatabase.

Adding our Dependencies

Next, we need to add bootstrap and the datatable styles and scripts. These files are included in the downloadable of this tutorial.

Add the styles and scripts in the header section of your page or the scripts below the end body tag.

  1. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "bootstrap/css/bootstrap.min.css"

    >
  2. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "datatable/dataTable.bootstrap.min.css"

    >

  3. <script

    src

    =

    "jquery/jquery.min.js"

    ></

    script

    >
  4. <script

    src

    =

    "bootstrap/js/bootstrap.min.js"

    ></

    script

    >
  5. <script

    src

    =

    "datatable/jquery.dataTables.min.js"

    ></

    script

    >
  6. <script

    src

    =

    "datatable/dataTable.bootstrap.min.js"

    ></

    script

    >

Creating our Markup

Next, we create our table and the new search box for our table.

  1. <div class="container">
  2. <h1 class="page-header text-center">Assign a New Search Box for Table</h1>
  3. <div class="row">
  4. <div class="col-sm-8 col-sm-offset-2">
  5. <input type="text" class="form-control input-lg" id="searchBox">
  6. <table id="myTable" class="table table-bordered table-striped">
  7. <thead>
  8. <th>ID</th>
  9. <th>Firstname</th>
  10. <th>Lastname</th>
  11. <th>Address</th>
  12. </thead>
  13. <tbody>
  14. <?php
  15. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'mydatabase'

    )

    ;
  16. $sql

    =

    "SELECT * FROM members"

    ;

  17. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  18. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  19. echo
  20. "<tr>
  21. <td>"

    .

    $row

    [

    'id'

    ]

    .

    "</td>
  22. <td>"

    .

    $row

    [

    'firstname'

    ]

    .

    "</td>
  23. <td>"

    .

    $row

    [

    'lastname'

    ]

    .

    "</td>
  24. <td>"

    .

    $row

    [

    'address'

    ]

    .

    "</td>
  25. </tr>"

    ;

  26. }

  27. ?>
  28. </tbody>
  29. </table>
  30. </div>
  31. </div>
  32. </div>

Initialize DataTable

Next, we initialize our datatable and assign our new searchbox to our data table.

  1. $(

    function

    (

    )

    {
  2. //inialize datatable
  3. var

    myTable =

    $(

    '#myTable'

    )

    .DataTable

    (

    {
  4. 'paging'

    :

    true

    ,
  5. 'lengthChange'

    :

    false

    ,
  6. 'searching'

    :

    true

    ,
  7. 'ordering'

    :

    true

    ,
  8. 'info'

    :

    false

    ,
  9. 'autoWidth'

    :

    false
  10. }

    )

  11. //assign a new searchbox for our table
  12. $(

    '#searchBox'

    )

    .on

    (

    'keyup'

    ,

    function

    (

    )

    {
  13. myTable.search

    (

    this

    .value

    )

    .draw

    (

    )

    ;
  14. }

    )

    ;
  15. }

    )

    ;

Hiding the Default Search

Lastly, we hide the default search box of our data table.

  1. .dataTables_filter

    {
  2. display

    :

    none

    ;
  3. }

Here's the full HTML code.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Assign a New Search Box for Table Outside of DataTable</title>
  6. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  7. <link rel="stylesheet" type="text/css" href="datatable/dataTable.bootstrap.min.css">
  8. <style>
  9. .dataTables_filter {
  10. display: none;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <h1 class="page-header text-center">Assign a New Search Box for Table</h1>
  17. <div class="row">
  18. <div class="col-sm-8 col-sm-offset-2">
  19. <input type="text" class="form-control input-lg" id="searchBox">
  20. <table id="myTable" class="table table-bordered table-striped">
  21. <thead>
  22. <th>ID</th>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Address</th>
  26. </thead>
  27. <tbody>
  28. <?php
  29. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'mydatabase'

    )

    ;
  30. $sql

    =

    "SELECT * FROM members"

    ;

  31. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  32. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  33. echo
  34. "<tr>
  35. <td>"

    .

    $row

    [

    'id'

    ]

    .

    "</td>
  36. <td>"

    .

    $row

    [

    'firstname'

    ]

    .

    "</td>
  37. <td>"

    .

    $row

    [

    'lastname'

    ]

    .

    "</td>
  38. <td>"

    .

    $row

    [

    'address'

    ]

    .

    "</td>
  39. </tr>"

    ;

  40. }

  41. ?>
  42. </tbody>
  43. </table>
  44. </div>
  45. </div>
  46. </div>


  47. <script src="jquery/jquery.min.js"></script>
  48. <script src="bootstrap/js/bootstrap.min.js"></script>
  49. <script src="datatable/jquery.dataTables.min.js"></script>
  50. <script src="datatable/dataTable.bootstrap.min.js"></script>
  51. <!-- generate datatable on our table -->
  52. <script>
  53. $(function(){
  54. //inialize datatable
  55. var myTable = $('#myTable').DataTable({
  56. 'paging' : true,
  57. 'lengthChange': false,
  58. 'searching' : true,
  59. 'ordering' : true,
  60. 'info' : false,
  61. 'autoWidth' : false
  62. })

  63. //assign a new searchbox for out table
  64. $('#searchBox').on('keyup', function(){
  65. myTable.search(this.value).draw();
  66. });
  67. });
  68. </script>
  69. </body>
  70. </html>

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,053

322,753

322,762

Top