jquery - On dataTable scroll data should load from server side and append to existing records -
using node js , bootstrap developing web application . have table implemented using data table. performing search , sort using server side code . fetching 50 records. these working fine.
now want load data (next 50 records ) server side when scroll reached bottom.
currently code ,
$('#table-goal').datatable({ "fninitcomplete" : function() { $('.datatables_scrollbody').mcustomscrollbar({ theme : "dark-3", callbacks : { whilescrolling : function() { //alert("scroll"); if (this.mcs.toppct == 100) { // need load data using same ajax , append data previous 50 records. } } } }); }, "scrolly" : "230px", "scrollcollapse" : true, "paging" : false, "info" : false, "processing" : true, "serverside" : true, "ajax" : { "url" : "/list/goals/suggestion", "type" : "post", "data" : function(d) { var custom = {}; custom.columnlength = d.columns.length; custom.ordercolumn = d.order[0].column; custom.orderdir = d.order[0].dir; d.type = "goal"; d.custom = custom; //d.limit = limit; //d.offset = offset; } }, "bautowidth" : true, "columns" : [{ "data" : "name" }, { "data" : "flag" }, { "data" : "goal_id" }], "columndefs" : [{ "render" : function(data, type, row) { return '<div class="goal_name" data-name="' + data + '"><span>' + row.name + '</span></div>'; }, "targets" : 0, }, { "render" : function(data, type, row) { if (data == 'yes') { return '<div class="review-editable keywords text-center" data-type="goal"><span class="top-adjust"><img class="friend-icon" src="/images/target/friends.png"></span></div>'; } else { return '<div class="review-editable keywords text-center" data-type="goal"></div>'; } }, "orderable" : false, "targets" : 1 }, { "render" : function(data, type, row) { if (data == null) { data = ''; } return '<div class="text-center add-near-data"><span class="list-view"> <a class="btn-circle-add"> <img src="/images/target/add.png"> </a></span></div>'; }, "targets" : 2 }], "order" : [[0, "asc"]] });
and backend code
router.post('/list/goals/suggestion', function(req, res) { var reqdata = req.body; var sess = req.session; var id = sess.userid; var columncount = reqdata["custom"].columnlength; var ordercolumn = reqdata["custom"].ordercolumn; var orderdir = reqdata["custom"].orderdir; var getsearchvalue = reqdata["search"].value; if (getsearchvalue == undefined) { getsearchvalue = ''; } /* * paging */ var slimit = ""; if (req.body.start != '-1' && req.body.length != '-1') { var slimit = " limit " + req.body.length + " offset " + req.body.start; } else { var slimit = " limit 50 offset 0 "; } /* * ordering */ if (columncount > 0) { console.log('---------'); var sorder = "order "; (var = 0; < columncount; i++) { if (ordercolumn == i) { sorder += reqdata["columns"][i].data + " " + orderdir + ", "; } } sorder = sorder.slice(0, -2); if (sorder == "order by") { sorder = ""; } } /* * filtering */ var swhere = ""; if (getsearchvalue != "") { getsearchvalue = getsearchvalue.touppercase(); var swhere = "where ("; (var = 0; < columncount; i++) { swhere += "upper(name) '%" + getsearchvalue + "%' or "; } swhere = swhere.slice(0, -3); swhere += ')'; } var squery = "select g.goal_id,name,flag goal g left join (select distinct on (goal_id) goal_id,flag user_goal_assoc ug left join (select 'yes' flag,(case when party_id = '" + id + "' party_friend_id else party_id end) friend_id friend (party_id = '" + id + "' or party_friend_id = '" + id + "') , status = 1) fr on (fr.friend_id =ug.creater_id , creater_id != '" + id + "' , status = 1)) ug on ( g.goal_id = ug.goal_id) " + swhere + " " + sorder + " " + slimit; client.query(squery, function(err, goallist) { //console.log(goallist.rows); var obj = { "draw" : req.body.draw, "data" : goallist.rows }; res.send(obj); });});
i tried few logics didn't worked me. code how have proceed.
i know old post, see: https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html
from can see should have add:
scroller: { loadingindicator: true }
to .datatable({
init.
Comments
Post a Comment