1

I am new to jQuery and javascript. So I am not able to find solution. I am using gentelella alela template's default example data table. I want to add column filter to each column except action column. I got this link. So I tried to add js given on my page like below.

<script>
    $(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#datatable thead tr').clone(true).appendTo( '#datatable thead' );
    $('#datatable thead tr:eq(1) th').each( function (i) {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );

        $( 'input', this ).on( 'keyup change', function () {
            if ( table.column(i).search() !== this.value ) {
                table
                    .column(i)
                    .search( this.value )
                    .draw();
            }
        } );
    } );

    // var table = $('#datatable').DataTable( {
    //     orderCellsTop: true,
    //     fixedHeader: true
    // } );
} );

    </script>

In above code I commented some code because it was giving me this error alert.

Now with above code I am getting result as below image: enter image description here

with above code its not able to search from the text inputs(its hitting sorting). its showing sorting on search row also. Also I don't want search on Action column.

My problems are:

  1. Column searching is not working. When I hit on input field of column search, its sorting things.

  2. I am not expecting sorting on column search row. need only search boxes for each column except Action column.

  3. There should no search box for action column.

How do I achieve column filtering except Action column in datatable?

Please help. Thanks in advance.

Table structure:

<table id="datatable" class="table table-striped table-bordered" style="width:100%;">
                    <thead>
                      <tr>
                        <th>Category Name</th>
                        <th>Status</th>
                        <th>Action</th>

                      </tr>
                    </thead>


                    <tbody>
                      @if(count($category) >= 1)
                      @foreach ($category as $cat)
                      <tr>
                        <td>{{$cat->category_name}}</td>
                        <td>
                          @if($cat->is_active == 1)
                          <span class="tag-success">Active</span>
                          @elseif($cat->is_active == 0)
                          <span class="tag-danger">Inactive</span>
                          @endif
                        </td>
                        <td>
                          @if($cat->is_active == 1)
                          <a href="{{route('category.edit', $cat->category_id)}}" class="tableIcons-edit"
                            data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit"><i
                              class="fa fa-pencil-square-o"></i></a>&nbsp;
                          <a href="{{route('categoryInactivate', $cat->category_id)}}" class="tableIcons-inactive"
                            data-toggle="tooltip" data-placement="top" title="" data-original-title="Inactivate"
                            ><i class="fa fa-thumbs-down"></i></a> 
                          @elseif($cat->is_active == 0)
                          <a href="{{route('categoryActivate', $cat->category_id)}}" class="tableIcons-active"
                            data-toggle="tooltip" data-placement="top" title="" data-original-title="Activate"
                            ><i class="fa fa-thumbs-up"></i></a>
                          @endif
                        </td>

                      </tr>
                      @endforeach
                      @else
                      <p>No records found!</p>
                      @endif
                    </tbody>
                  </table>
ganesh
  • 416
  • 1
  • 11
  • 32
  • you need to uncomment this one because table variable is used above in your script var table = $('#datatable').DataTable( { orderCellsTop: true, fixedHeader: true } ); – Amit Sharma Dec 13 '19 at 08:02

3 Answers3

0

You need to change your code a little bit and add a condition to it.

$(document).ready(function() {
// Setup - add a text input to each footer cell
$('#example1 thead tr').clone(true).appendTo( '#example1 thead' );
$('#example1 thead tr:eq(1) th').each( function (i) {

    var title = $(this).text();
    if(title != 'Action'){
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );

    $( 'input', this ).on( 'keyup change', function () {
        if ( table.column(i).search() !== this.value ) {
            table
                .column(i)
                .search( this.value )
                .draw();
        }
    } );
}
}
);

 var table = $('#example1').DataTable( {
     orderCellsTop: true,
    fixedHeader: true
 } );
});
Amit Sharma
  • 1,775
  • 3
  • 11
  • 20
  • 1
    by adding that condition, its removing search box only. My issues like search not working on input box, its hitting filtering. Also its adding sorting to search row. – ganesh Dec 13 '19 at 09:08
  • you mean the top search stop working after adding the column search ? – Amit Sharma Dec 13 '19 at 09:32
  • No. my column search not working with the code mentioned. Its hitting sorting. Please check my edit in the question – ganesh Dec 13 '19 at 09:38
  • I have added the same code and it is working fine https://prnt.sc/qa9ffz – Amit Sharma Dec 13 '19 at 09:41
  • send your table structure please – Amit Sharma Dec 13 '19 at 09:42
  • how your image and my image looking different in case of column search row. in your image for search row sorting is not gets applied. Why in my case it gets applied. Did you change in datatable jquery? – ganesh Dec 13 '19 at 09:44
  • No, Could you please send me your table structure please – Amit Sharma Dec 13 '19 at 09:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204181/discussion-between-ganesh-and-amit-sharma). – ganesh Dec 13 '19 at 09:46
  • The problem is that it is cloning the row of the header, so actions column is also being fixed above. You can remove the search input but the word "Action" will appear anyway because of the line: $('#example1 thead tr').clone(true).appendTo( '#example1 thead' ); – Laynier Piedra Jul 29 '21 at 16:26
0

If you code the first accepted answer to this question, you will only remove search input but the word 'Action' will still appear in the row, so you have to add an else clause to the if block. The final code could look like this:

    $(document).ready(function() {
// Setup - add a text input to each footer cell
$('#example1 thead tr').clone(true).appendTo( '#example1 thead' );
$('#example1 thead tr:eq(1) th').each( function (i) {

    var title = $(this).text();
    if(title != 'Action'){
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );

    $( 'input', this ).on( 'keyup change', function () {
        if ( table.column(i).search() !== this.value ) {
            table
                .column(i)
                .search( this.value )
                .draw();
        }
    } );
} else $(this).text('');
}
);

 var table = $('#example1').DataTable( {
     orderCellsTop: true,
    fixedHeader: true
 } );
});

The else clause will remove the undesired word 'Action' because of the cloned header row. Note: this is not the best solution, it's just one.

Laynier Piedra
  • 425
  • 6
  • 16
-1

You could insert this on Datatable function:

columnDefs: [ { targets: [ 2 ], orderable: false, }, ],

refer to this link: https://datatables.net/forums/discussion/21164/disable-sorting-of-one-column

also you could remove search filtering on action:

$('#datatable thead tr:eq(1) th').each( function (i) {
    var title = $(this).text();
    if (i != 2) {
       $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
       $( 'input', this ).on( 'keyup change', function () {
           if ( table.column(i).search() !== this.value ) {
               table
                   .column(i)
                   .search( this.value )
                   .draw();
           }
       } );
   }} );
mreawn
  • 1
  • 2