Datatables Server Side Implementation with Nodejs and MongoDB

No comments
How to implement server side Datatables processing using Node-js and MongoDB (ExpressJs) 

Something you should kept in mind is that -
1- Add Datatables CDN or offline link.
2- Try to implement client side Datatables first.(This is server side Implementation and to be used if your have more than 1000 record.)  

• Now this is working Code.....
• this code to be used on client side (HTML) page

$('#example').dataTable({
processing: true,
destroy:true,
autoWidth:false,
sAjaxDataProp: 'data',
ajax:{
url: "/getData",
type: 'POST',
data: {"id":id,"product":prdMain},
dataSrc: ""
},
columns: [
{ data : "job_name" },
{ data : "product" },
{ data : "test_plan" }

] ,"fnRowCallback":function( nRow, aData, iDisplayIndex, iDisplayIndexFull ){

var dat1 = '<div >'+aData['job_name']+'</div>';
var dat2 = '<div >'+aData['product']+'</div>';
var dat3 = '<div >'+aData['test_plan']+'</div>';
$('td:eq(0)', nRow).html(dat1);
$('td:eq(1)', nRow).html(dat2);
$('td:eq(2)', nRow).html(dat3);
}
});


• Nodejs Controller code 


router.post('/getData', function(req, res){    
     obj.getData(function (err,data) {        
        if (err) throw err;
        else {
         res.send(data); //send as it is to client page//
         };
    }) 
});

Models Code 

• Fetching the data from MongoDB.

module.exports.getData= function (callback) {
     Data.find({},callback);  //.Find is Used to get all data.
}


Note - This is not for beginners so I have skipped the basic things like exports and injections as this blog is dedicated to programmers so if you wish to continue with this post you must know basic things and in case you still face any issue kindly comment bellow.



No comments :

Post a Comment