简体   繁体   English

带有Node.js的jQuery DataTables

[英]jQuery DataTables with Node.js

So i am trying to implement a pagination table with the datatables plugin, this is my first time using this plugin. 因此,我尝试使用datatables插件实现分页表,这是我第一次使用此插件。 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. 按照插件文档中的介绍,我按照插件上的文档进行操作,并尝试通过使用Ajax从服务器获取值。

I seem to be getting the following error once i make the get request and i am unsure of why? 发出get请求后,我似乎会收到以下错误,但我不确定为什么?

Error: Uncaught TypeError: Cannot read property 'length' of undefined 错误: 未被捕获的TypeError:无法读取未定义的属性“长度”

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: 返回的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: 'raaj@paypal.com' } ], searchStatus: 'Success', eBayUserID: '', errorCode: '0', rowCount: '6', pageNumber: '1', paginationValue: '1', paypalLoginID: 'raaj@paypal.com' } {reportList:[{reportID:'EIBBP-448',eBayUserID:'',reportStatus:'New',摘要:'Raj创建的BugBounty报告,lastUpdatedDate:'2015-06-15 01:05',createdDate:' 2015-06-15 01:05',paypalLoginID:'raaj@paypal.com'}],searchStatus:“成功”,eBayUserID:“”,错误代码:“ 0”,rowCount:“ 6”,pageNumber:“ 1” ,paginationValue:'1',paypalLoginID:'raaj@paypal.com'}

It would be great to know if there is anyone who has worked with node.js server side processing for datatables 知道是否有人在node.js服务器端处理数据表方面工作非常高兴

You need to define dataSrc and columns.data - the following should work : 您需要定义dataSrccolumns.data以下内容应该可以工作:

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 ") dataSrc用于指定保存行项目的数组的名称(由于“ 无法读取未定义的属性'length'的原因 ”)
  • columns.data to map item properties to columns columns.data将项目属性映射到列

You simply don't have to bother with the server side processing. 您根本不必费心服务器端处理。 I used a simple way to trick dataTables initialization. 我使用了一种简单的方法来诱骗dataTables初始化。

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. 首先,在确认数据显示良好之后,您需要通过最优选的方式获取要在表中显示的数据,现在转到初始化dataTables并进行设置,以使其在初始化之前延迟。

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. 例如,在我的情况下,我给了它100ms的延迟,它就像一个咒语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM