2

I'm using the jQuery DataTables plug-in. I am wondering if there is a way to add empty rows to the table so that the table always displays say 50 rows, weather there is data in those rows or not?

For example, if I make an AJAX call and it returns 5 entries, I want to be able to display 45 empty rows. Or if the call returns 49 entries, I want to still display 1 empty row at the bottom of the table.

1
  • As @Jakub's edit said, this will totally mess up sorting and filtering - you can't guarantee that the row will stay on the bottom. If you disable both of those, then maybe, but you're likely to see odd effects still. If the reason you're doing this is to ensure panel dimensions, I'd go with some CSS instead to keep the size. Commented May 18, 2018 at 7:53

4 Answers 4

1

After you populate your data source with results from the ajax call, you could count amount of elements in the source, and if it's less than 50, you could add to it some empty records.

Edit: on second thought it might not be the best idea, because it'll screw up the sorting.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking the same thing with the sorting. I have thought of that solution, I just wasn't sure if there was a more elegant way of doing it. Thanks!
0

Hard to know without code snippets and what you're doing, but you could either modify it on the server or client side.

You could force the call to return 50 rows on the server, regardless of how many are populated or you could receive the response and then modify it on the client side.

Comments

0

Someone may have a better solution, but this should "work" assuming you have at least 1 entry.

for (var i=numEntries; i < 50; i++) {
    $("#tableID tr:last").after('<tr><td>Empty Row</td></tr>');
}

JSFiddle: http://jsfiddle.net/yXvmX/2/

2 Comments

It definitely works, but I believe you are correct. Every time that the data is changed/selected then it needs to be called. I made a Fiddle and it for sure works.
The problem with this is that the rows aren't empty, nor are they being controlled by a DataTables...
0

I found a callback function named "drawCallback" can be used in this kind of case. https://datatables.net/reference/option/drawCallback every time dataTable redraw one table page content like you do sorting, it will be called.

$("#yourTable").DataTable({
                    "columns":columns,
                    "ajax": {
                        "url": url
                        "dataSrc" : function (json) {   return json;}
                    },
                    "searching": false ,
                    "pageLength": rowNumPerPage,
                    "lengthChange": false,
                    "autoWidth": false,
                    "order": [[ 0, "asc" ]],

                    "drawCallback": function( settings ) {
                        //for maintaining the same height every page, 
                        //add empty row to table 
                        var api = this.api();

                        var currentPageDataSet = api.rows( {page:'current'} ).data() ;
                        if(currentPageDataSet.length < rowNumPerPage){     
                            var $tbody = $('#yourTable tbody');
                            for(var i = 0; i< rowNumPerPage-currentPageDataSet.length; i++){
                                var isEven = (currentPageDataSet.length+(i+1))%2 === 0;
                                var $tr = (isEven)? $('<tr role="row" class="even"></tr>') : $('<tr role="row"  class="odd"></tr>');
                                for(var j=0; j< columns.length; j++){
                                    $tr.append('<td style="color:white;" >_</td>');
                                }
                                $tbody.append($tr);
                            }
                        }

                    }
                });

In this callback function, what I do is to count the row number of current page data, if the row number is smaller than the max page row number, then we append empty row elements to tbody.

Comments

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.