0

Good day! I'm having trouble with searching in jquery dataTable search bar for the string word 'Active'. If I change search bar the for the word 'Inactive'. The table will filter. However it does not seem to work with the string 'Active'.

This is all the data: This is all the data

When I search Inactive When I search Inactive When I search Active When I search Inactive

Table code:

<table class="table table-hover table-bordered" id="table1">
                                <thead>
                                    <tr>
                                        <th>Faculty Code</th>
                                        <th>Last Name</th>
                                        <th>First Name</th>
                                        <th>Middle Name</th>
                                        <th>Position</th>
                                        <th></th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php  while ($row = $result -> fetch_object()): ?>
                                    <tr>
                                        <td><?php echo $row->Faculty_ID;?></td>
                                        <td><?php echo $row->Faculty_Lastname ;?></td>
                                        <td><?php echo $row->Faculty_Firstname ;?></td>
                                        <td><?php echo $row->Faculty_Middlename;?></td>
                                        <td><?php echo $row->Position; ?></td>
                                        <td class="text-center">
                                            <a  class="btn btn-sm btn-outline-primary text-muted" href="Faculty_edit.php?Faculty_ID=<?php echo $row->Faculty_ID;?>">Edit</a>

                                            <a  data-fid="<?php echo  $row->Faculty_ID;?>" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal"  class="btn btn-sm btn-outline-danger delete-faculty-btn text-muted" >Delete</a>
                                        </td>

                                        <td>
                                            <?php if($row->Status=='Active') echo '<a href="#deactive_account" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:green;">Active</a>'; ?>
                                            <?php if($row->Status=='Inactive') echo '<a href="#active_modal" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:red;">Inactive</a>'; ?>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>

jQuery Code:

<script>
    $('#table1').DataTable({
    order: [],
    columnDefs: [ { orderable: false, targets: [5] } ]
    });
</script>
jerico
  • 123
  • 2
  • 13

1 Answers1

1

$(function(){
var table1 = $('#table1').DataTable({
        order: [],
        "oSearch": {"bSmart": false}, // disable smart search
        columnDefs: [ { orderable: false, targets: [5] } ]
        });
 $('#table1_wrapper input[type=search]').on( 'keyup click', function () {
      table1
        .column('6')
        .search( "^" + this.value, true, false, true )
        .draw();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css" rel="stylesheet"/>
<table class="table table-hover table-bordered" id="table1">
                                <thead>
                                    <tr>
                                        <th>Faculty Code</th>
                                        <th>Last Name</th>
                                        <th>First Name</th>
                                        <th>Middle Name</th>
                                        <th>Position</th>
                                        <th></th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    
                                    <tr>
                                        <td>1</td>
                                        <td>lastname1</td>
                                        <td>FirstName1</td>
                                        <td>Middlename1</td>
                                        <td>Manager</td>
                                        <td class="text-center">
                                            <a  class="btn btn-sm btn-outline-primary text-muted" href="#">Edit</a>

                                            <a  data-fid="" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal"  class="btn btn-sm btn-outline-danger delete-faculty-btn text-muted" >Delete</a>
                                        </td>

                                        <td>
                                            <a href="#deactive_account" data-toggle="modal" class="activate" style="color:green;">Active</a>
                                        </td>
                                    </tr>
         <tr>
                                        <td>2</td>
                                        <td>lastname2</td>
                                        <td>FirstName3</td>
                                        <td>Middlename4</td>
                                        <td>Manager</td>
                                        <td class="text-center">
                                            <a  class="btn btn-sm btn-outline-primary text-muted" href="#">Edit</a>

                                            <a  data-fid="" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal"  class="btn btn-sm btn-outline-danger delete-faculty-btn text-muted" >Delete</a>
                                        </td>

                                        <td>
           <a href="#active_modal" data-toggle="modal" class="activate" style="color:red;">Inactive</a>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

you can try below code where search will be restricted to last column only See Datatable API documentation for more details

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57