4

So i am trying to implement a pagination table with the datatables plugin, this is my first time using this plugin. I followed the documentation on the plugin and tried to get the values from the server through the use of Ajax, as per presented in the plugins documentation.

I seem to be getting the following error once i make the get request and i am unsure of why?

Error: Uncaught TypeError: Cannot read property 'length' of undefined

On client side i have the following code

viewReports = {
    init: function(){
        $('#paginatedData').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": '/viewreports'
        });

    }
};

$(document).ready(viewReports.init);

In my server side i have the following

router.get('/viewreports', function(res, req){

    async.parallel({
        viewReports: function(callback){
            restCall('/rest/bugbounty/latest/message/searchReport', 'POST', parameters, function(data){
                callback(null, data);
            }); 
        }
    }, function(err, result){
        if(!err){
            res.send(result.viewReports);
            res.render('viewreports');
        }
    });
});

Returned JSON:

{ reportList: [ { reportID: 'EIBBP-448', eBayUserID: ' ', reportStatus: 'New', summary: 'BugBounty Report created by Raj', lastUpdatedDate: '2015-06-15 01:05', createdDate: '2015-06-15 01:05', paypalLoginID: '[email protected]' } ], searchStatus: 'Success', eBayUserID: '', errorCode: '0', rowCount: '6', pageNumber: '1', paginationValue: '1', paypalLoginID: '[email protected]' }

It would be great to know if there is anyone who has worked with node.js server side processing for datatables

5
  • In server-side processing mode your server-side code should return data in the JSON format as described here. Please post the JSON file that your server returns. Commented Jun 15, 2015 at 2:26
  • so there is no specific file where i am saving my json data to, but upon rendering the route '/viewreports' i am able to see the following value on the console { reportList: [ { reportID: 'EIBBP-448', eBayUserID: ' ', reportStatus: 'New', summary: 'BugBounty Report created by Raj', lastUpdatedDate: '2015-06-15 01:05', createdDate: '2015-06-15 01:05', paypalLoginID: '[email protected]' } ], searchStatus: 'Success', eBayUserID: '', errorCode: '0', rowCount: '6', pageNumber: '1', paginationValue: '1', paypalLoginID: '[email protected]' } Commented Jun 15, 2015 at 2:30
  • Do i need to stringify the above json value? Commented Jun 15, 2015 at 2:32
  • Your response is not correct for server-side processing mode, please read Server-side processing for more details and data format. And yes, you need to return string in JSON format. Commented Jun 15, 2015 at 2:36
  • Please Check answer of pankaj kumar https://stackoverflow.com/questions/50731153/jquery-datatable-with-nodejs-mysql/78165849#78165849 Commented Mar 15, 2024 at 10:44

2 Answers 2

6

You need to define dataSrc and columns.data - the following should work :

var table = $('#example').DataTable({
    processing: true,
    serverSide: true,
    ajax: {
        url: "/viewreports",
        dataSrc: "reportList"
    },    
    columns: [ 
        { data : "reportID" },
        { data : "eBayUserID" },
        { data : "reportStatus" },
        { data : "summary" },
        { data : "lastUpdatedDate" },        
        { data : "createdDate" },        
        { data : "paypalLoginID" }
   ]     
}); 

on an empty table :

<table id="example"></table>  
  • dataSrc to specify what the array holding row items is named (cause of "Cannot read property 'length' of undefined")
  • columns.data to map item properties to columns
Sign up to request clarification or add additional context in comments.

Comments

0

You simply don't have to bother with the server side processing. I used a simple way to trick dataTables initialization.

First, you will need to fetch the data you want to display in the table through your most preferred way, after you have confirm that data displays well, now head to where you initialize dataTables and make it so that it delays before initialization.

setTimeout(() => {
    $('#yourtable').dataTable({
        // datatables customization options
    });
}, 100)

For example, in my case I gave it a delay of 100ms, and it works like a charm.

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.