m3t4l
m3t4l

Reputation: 61

DataTables: get the row number of a paginated result from a php script

I have an index.html page showing a table where the results are obtained by DataTables with server-side processing.

The results are paginated, and each row has 8 columns: the first column shows an ID number, named idNumber (which is the primary key of a mysql table named messages) the second column shows an integer number, named reportNumber which is a foreign key in messages; this number is formatted as a link that opens a modal window with id="modalreports". Whatever reportNumber you click, always the same modal window is opened.

The modal window displays a table with id="tableReports" and the results are obtained and paginated by DataTables Ajax call with server-side processing, where a PHP script getReports.php is called.

The following is the code:

        var tabreps = $('#tableReports').DataTable({
            'processing': true,
            'serverSide': true,
            'serverMethod': 'post',
            'cache': false,
            'ajax': {
                'url':'getReports.php'
            },
            'columnDefs': [
                { className: 'dt-center', targets: [0] }
            ],
            "columns" : [
                {data: 'reportNumber',
                 render: function(data, type, row) {                            
                            var numRep = row.reportNumber;
                            
                            return '<input type="radio" name="radiobutton_report" value="'+numRep+'" id="id_radiobutton_report_' + numRep + '">&nbsp;&nbsp;' + numRep;
                         }
                },
                {data: 'reportDetail'} 
                ],
                "order": [[0, "asc"]]                    
        });

Inside getReports.php, the following mysql select is executed:

SELECT reportNumber,reportDetail FROM reports;

the SELECT gives 25000 rows; the result is formatted according to the SSP::simple method in the POST data Server-side example. The rows inside the modal window are paginated, and each row has two columns (according to the script above):

From the main page, if I click on the link having i.e. 14816 as a report number, I would like that the modal window opens at the page showing the radiobutton with 14816 as a value, and checked.

I managed to pass the report number from the main page to the modal window (which is assigned to a variable reportNumberFromMainPage) on client-side, but didn't manage to have the page including the radiobutton with its report number displayed.

I put the following code

var totalRowsFromMysql = tabreps.page.info().recordsTotal; 

to obtain all of the rows from the mysql table (25000), but using the page.info() properties I only get information on the first page.

I also tried the following code to obtain the page number

var indexReport = tabreps.column(0, {order: 'current'}).data().indexOf(reportNumberFromMainPage);
if (indexReport >= 0) {
  var pageLength = tabreps.page.info().length;
  var pageNumber = Math.floor(indexReport / pageLength) + 1;
  console.log("clicked on report number " +  reportNumberFromMainPage + '; found at page ' + pageNumber);
}

but always get indexReport = -1. I know that server-side processing only sends to the client a number of data filtered according to the page, number of rows for page (default: 10), but I wonder if there is a DataTables parameter, as well as page.info().recordsTotal, which sends back an absolute row index and not the current page row index.

Thanks to everybody could give me some helpful hints or suggestions.

Upvotes: 0

Views: 88

Answers (0)

Related Questions