0

Giving a simple table like this

<table id="exampleTable" class="table hover nowrap">
   <thead>
      <tr>
         <th>Id</th>
         <th>Name</th>
      </tr>
   </thead></table>

Also, using the jQuery Datatable plugin and making the rows selectable, like in this example. The Javascript goes like this:

var exampleTable = $("#exampleTable").DataTable(
                    {
                        "bDestroy": true,
                        "lengthChange": false,
                        "bPaginate": false
                    });

$('#exampleTable tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            $('#exampleTable tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    } );

So, when the table is empty, it shows a selectable row which says something like:

No data available in table

How to detect the table is empty and not allow selection on that kind of "message rows"?

Here is a fiddle with the code.

2
  • do you want hide table when there is no data? Commented Jan 27, 2017 at 9:56
  • But what happen then when I have only one row? @Murali no, I want to disable the selection on the "No data" row. Commented Jan 27, 2017 at 10:09

3 Answers 3

3

you can check if there is td with the class dataTables_empty like this:

$('#exampleTable tbody').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected')) {
            $(this).removeClass('selected');
        }
        else if(! $(this).find('td.dataTables_empty').length){
            $('#exampleTable').DataTable().rows().nodes().each(function(){
               $(this).removeClass('selected');
            });
            $(this).addClass('selected');
        }
    } );
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I was thinking in this possibility, but I was hoping to find a "cleaner" way to do it.
looks clean enough for me :), I don't know how else you would check if there are entries? Maybe Datatable provides some callback functions where you can check it
That was exacly what I was hoping for :)
I have updated your answer, cause it is necessary to remove the selected class from the hidden rows too.
0

You can use the - .row().data() method to see if there is data. if this returns undefined, you can disable selection of the row.

i also think this would be an ideal solution instead of dom traversal which is more expensive. what do you guys think ?

below is the updated fiddle. you can comment the tbody from the html to test it.

Let me know if this helps.

Modified Fiddle

4 Comments

It doesn't work. Just make a search with some random letters and you will see that the "No matching records found" row is still selectable.
sorry about that. had not considered that scenario. from the api documentation what i found was adding the two resources for select there is not additional logic required. just an option to enable select. i.e 'select:true'. here is the updated fiddle. jsfiddle.net/vje9e2sL . please check resources in the fiddle for required css and js files.
the on 'select' is not mandatory. its just if you want to do additional processing. so please ignore if not required.
Yes, I have seen that, but I need to do more things in the select event, so I need to handle it.
-1

inside your click method, add before everything

if($('#exampleTable tbody tr td').length == 1){
      return;
    }

2 Comments

This will disable the selection always there is only one row, even when the row is a valid record.
so check with td (I've updated) because your 'no message' row just have one td (assuming that your datatable data has more than one td) should work as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.