duanbozhong9689 2019-08-20 04:27
浏览 371
已采纳

通过查询大量数据来减少html表的加载时间 PHP,HTML,SQLSERVER 2012

I'm experiencing a problem when loading the data from my database, the data is somewhat large enough to cause a delay.

I tried to add this in my code, but still not enough.

.fixed-table {
    table-layout: fixed;
}

I stumbled upon this guide. https://patdavid.net/2019/02/displaying-a-big-html-table/. So i tried to implement it, unfortunately my loading time is still the same.

With this line, how will I approach this

I set the table to display: none; initially and once the document was ready I set it back to display: table; (relying on JavaScript to do this).

This is my table script

/* FOR CORPORATE DATA TABLE START */
function format ( dataSource ) {
    var html = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;" class="fixed-table table table-bordered">';
    for (var key in dataSource){
        html += '<tr>'+
                   '<td>' + key             +'</td>'+
                   '<td>' + dataSource[key] +'</td>'+
                '</tr>';
    } return html += '</table>';  }
var earnings_amendment_table = $('#earnings_amendment').DataTable({});

      // Add event listener for opening and closing details
      $('#earnings_amendment').on('click', 'td.details-control', function () {
          var tr = $(this).closest('tr');
          var row = earnings_amendment_table.row(tr);

          if (row.child.isShown()) {
              // This row is already open - close it
              row.child.hide();
              tr.removeClass('shown');
          } else {
              // Open this row
              row.child(format({
                  'Particulars : ' : tr.data('key-1'),
                  'Account Type : ' : tr.data('key-2'),
                  'Date Due : ' :  tr.data('key-3')
              })).show();
              tr.addClass('shown');
          } });
/* FOR CORPORATE DATA TABLE END */

This is the table

    <div class="box-body">
              <table id="earnings_amendment" class="fixed-table table table-bordered">
                <thead>
                  <th></th>
                  <th>Reference ID.</th>
                  <th>Reference No.</th>
                  <th>Employee Name</th>
                  <th>Account Title</th>
                  <th>Amount</th>
                  <th>Activity</th>
                  <th>Posted By</th>
                  <th>Validated By</th>
                  <th>Noted By</th>
                  <th>Tools</th>
                </thead>
                <tbody>
                <?php
$sql = "
select earningsamendment.particulars,earningsamendment.accounttype,earningsamendment.datedue,
employeemasterfile.lastname,employeemasterfile.firstname,employeemasterfile.middlename,
referenceno, accounttitle, max(credit) as credit, max(debit) as debit, max(referenceid) as referenceid, earningsamendment.employeeidno,
earningsamendment.postedby, approvedby, notedby
from earningsamendment
left join employeemasterfile on earningsamendment.employeeidno= employeemasterfile.employeeidno
WHERE earningsamendment.accounttitle='$accounttitle' AND 
YEAR(earningsamendment.dateposted)='$year'
group by referenceno, earningsamendment.employeeidno, accounttitle, earningsamendment.postedby, approvedby,
notedby,employeemasterfile.lastname,employeemasterfile.firstname,employeemasterfile.middlename,
earningsamendment.particulars,earningsamendment.accounttype,earningsamendment.datedue
";
                    $query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
                    while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
                      echo "
                      <tr data-key-1='".$row['particulars']."' data-key-2='".$row['accounttype']."' data-key-3='".$row['datedue']."'>
                        <td class='details-control'></td>
                          <td>".$row['referenceid']."</td>
                          <td>".$row['referenceno']."</td>
                          <td>".$row['lastname']." ".$row['middlename']." ".$row['firstname']."</td>
                          <td>".$row['accounttitle']."</td>
                          <td>".$row['credit']."</td>
                          <td>".(($row['debit']==$row['credit']) ? 'PAID' : 'FOR TAKE UP' )."</td>
                          <td>".$row['postedby']."</td>
                          <td>".$row['approvedby']."</td>
                          <td>".$row['notedby']."</td>
                          <td>
                            <button class='btn btn-success btn-sm edit btn-flat' data-id='".$row['referenceid']."'><i class='fa fa-edit'></i> Preview</button>

                            <button class='btn btn-danger btn-sm delete btn-flat' data-id='".$row['referenceid']."'><i class='fa fa-trash'></i> Delete</button>

                        " ?>
                            <?php if (empty($row['approvedby'])) { echo " <button class='btn btn-warning btn-sm approve btn-flat' data-id='".$row['referenceid']."'><i class='fa fa-check-square-o'></i> Approve</button> "; } ?>

                            <?php if (empty($row['notedby'])) { echo " <button class='btn btn-primary btn-sm note btn-flat' data-id='".$row['referenceid']."'><i class='fa fa-arrow-circle-right'></i> Note</button> "; } ?>

                            <?php "</td>
                        </tr>
                      ";
                    }
                  ?>
                </tbody>
              </table>
            </div>

The total data being loaded is around 300,000-500,000. It's taking so long for it to load. up to 60-70 seconds. Is there a way for us to reduce this time? Is the structure of my table/query wrong, is there any possible solution to reduce this delay?

I tried to use clusterize.js, but I can't seem to be able to implement it.

ADDENDUM: Is there a way for this to be implemented into SQL SERVER 2012?https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/server_processing.php

I'm planning to do this via Server-Side, but I'm worried that I may change all the tables that I did.

  • 写回答

2条回答 默认 最新

  • dsz90288 2019-08-20 05:07
    关注

    It would be best to use pagination and filtering to make the table usable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题