【发布时间】:2020-02-15 09:30:00
【问题描述】:
我正在使用数据表来显示数据库中的大量行。我使用下面的代码来显示从数据库中获取的数据,但随着数据开始增加,加载需要大量时间,因为选择查询中有多个连接。
以下是加载视图需要大量时间的代码:
<script>
$(document).ready(function() {
$('#fileData').dataTable({
"aLengthMenu": [[5,10, 25, 50, 100, -1], [5,10, 25, 50, 100, "All"]],
//"aaSorting": [[ 4, "desc" ]],
"iDisplayLength": <?php echo 5; ?>,
'bProcessing' : true,
'bServerSide' : false,
"oTableTools": {
"sSwfPath": "assets/media/swf/copy_csv_xls_pdf.swf",
"aButtons": []
},
"oLanguage": {
"sSearch": "Filter: "
},
"aoColumns": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
],
"aoColumns": [
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true},
{"bVisible": true}
]
}).columnFilter({ aoColumns: [
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true },
{ type: "text", bRegex:true }
]});
});
</script>
<table id="fileData" class="table table-striped table-bordered table-hover table-full-width">
<thead>
<tr>
<th>Sl.No</th>
<th>Type</th>
<th>No </th>
<th>0-15 yrs M</th>
<th>0-15 yrs F</th>
<th>15-45 yrs M</th>
<th>15-45 yrs F</th>
<th>Above 45 yrs M</th>
<th>Above 45 yrs F</th>
<th>Cumulative Since April</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php //if(is_object($proj_workers_report)) {
?>
<?php foreach ($nreports->result() as $index=>$row) { ?>
<tr>
<td> <?php echo $index + 1; ?> </td>
<td><?php echo $row->test1; ?></td>
<td><?php echo $row->test2; ?></td>
<td><?php echo $row->test3; ?></td>
<td><?php echo $row->test4; ?></td>
<td><?php echo $row->test5; ?></td>
<td><?php echo $row->test; ?></td>
</td>
<td></td>
</tr>
<?php
} ?>
</tbody>
</table>
After searching for solution to reduce loading time i found solution as enabling "serverSide":true.so i changed the code as below
$(document).ready(function () {
var year="<?php echo base_url() . 'reportc/new_disease_morbidity_report'; ?>";
alert(year);
var dataTable = $('#example2').DataTable({
"processing":true,
"serverSide":true,
"order":[[ 0, "desc" ]],
"ajax":{
url:"<?php echo base_url() . 'test/nreport'; ?>",
type:"POST",
data:"{'id':year}",
success: function (data) {
alert("success");
},
error: function () {
alert('error');
}
},
'language': {
"emptyTable":"No patient available"
},
"columnDefs":[
{
//"targets":[0, 3],
//"orderable":false,
},
],
});
controller:
function new_disease_morbidity_report()
{
$year = $this->input->post('id');
echo $year;
}
但是新代码的问题是它无法调用控制器中的函数。谁能帮我解决这个问题。我有什么遗漏吗?
【问题讨论】:
-
table datatable 有多少行一次可以轻松渲染50000条记录
-
首先检查使用您的 api 返回的行数
-
“无法调用函数”以什么方式?您观察到的错误或具体故障是什么?您在一个相当脱节的帖子中显示了很多代码。第二个代码 sn-p 的描述暗示它正在工作?您要描述的确切问题是什么?
标签: php jquery mysql codeigniter datatable