【发布时间】:2019-05-02 12:07:15
【问题描述】:
因为我必须渲染大数据,并且当我进行客户端分页时,这需要时间,我知道服务器端分页最适合
- 大型数据集
- 更快的初始页面加载
但我不知道如何使用 NODE.JS EJS 和 MYSQL 进行服务器端分页,这是我的 Routes 和.EJS
路线
app.get('/',function(req,res,next){
req.getConnection(function(error, conn) {
let sql = `SELECT *FROM studiestable
WHERE ReceivingDate >= ( CURDATE() - INTERVAL 35 DAY )
ORDER BY Bckup DESC,
ReceivingDate DESC`;
conn.query(sql,function(err,rows,fields){
if (err) {
req.flash('error', err)
res.render('patient/dashboard', {
title: 'Dashboard',
data: ''
})
} else {
res.render('patient/dashboard', {
title: 'Dashboard',
data: rows
})
}
})
})
})
EJS
<table id="pattTab" class="table small">
<tr style="background-color: rgb(122, 135, 160);color: white">
<th>ID</th>
<th>Patient Name</th>
<th>Age</th>
<th>Modality</th>
<th>Images</th>
</tr>
<% if (data) { %>
<% data.forEach(function(Patient){ %>
<tr>
<td><%= Patient.PatientID %></td>
<td><%= Patient.PatientName %></td>
<td><%= Patient.Age %></td>
<td><%= Patient.Modality %></td>
<td><%= Patient.Images %></td>
</tr>
<% }) %>
<% } %>
</table>
【问题讨论】:
-
Datatable 是服务器端分页的最佳选择。可能这个链接对你有用datatables.net/examples/data_sources/server_side
-
嗨,sadil,但我必须在没有数据表插件的情况下这样做,因为该插件不能很好地处理连续变化的数据
-
我没有得到
continuous changing data的意思 -
sadil 我在渲染我的表格后有多个事件,为此我必须在事件发生后一次又一次地渲染我的表格,当你有大量数据时,该数据表的性能不佳,请参见此处在stackoverflow.com/questions/50405711/…之前使用过数据表
-
你考虑过使用SQL的
OFFSET和LIMIT吗?
标签: javascript mysql node.js express ejs