Datatable Scroller (Server Side) Part:3
假設您有數百萬條記錄,你不需要分頁,然後您可以使用資料表滾動條。 我認為滾動條比分頁更好的使用者體驗,因為幾乎你可以一次檢視整個表。 今天我將展示如何在datatable.js v1.10
中實現scroller
並通過ajax
顯示伺服器端資料。 要在datatable
中實現scroller
,您必須新增js
庫或擴充套件dataTables.scroller.js
。 您可以直接從這裡下載。
<script type="text/javascript" language="javascript" src="js/dataTables.scroller.js"></script>
Datatable將根據您的檢視(如表格)獲取一些額外的記錄,以實現平滑渲染。 它將根據滾動位置向伺服器請求新記錄。
讓我初始化資料表:
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
serverSide: true,
ajax:{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get
error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");
}
},
dom: "frtiS",
scrollY: 200,
deferRender: true,
scroller: {
loadingIndicator: true
}
} );
} );
這裡有三個選項很重要::
dom
: “frtiS
”表格的Dom
元素,這裡“f
”表示過濾,“r”用於處理,“t”用於表,“i”用於表的資訊,“S”用於表滾動scrollY
: 200這是網格高度。deferRender
:true
僅當您滾動時,它才會從伺服器渲染大量資料(錶行)中的塊資料。 它將顯著提高效能。
根據“scrollY
”中提到的表高度,滾動條將觸發帶有起始行號和限制條數的ajax
。假設您的資料庫中有200條記錄。在只顯示5行的表檢視中,預設情況下,scroller
將獲取10
條記錄以進行平滑渲染。 在下一個滾動觸發器中,ajax
將請求更多10條記錄。此長度按資料表滾動條本身的“scrollY
”計算。
在我的第一篇文章中,我討論了伺服器端處理。 根據您的搜尋引數,順序和限制條數,這是伺服器端程式碼:
//Database connections
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: ".mysqli_connect_error());
// storing request (ie, get/post) global array to a variable
$requestData = $_REQUEST;
$columns = array(
// datatable column index => database column name
0 => 'employee_name',
1 => 'employee_salary',
2 => 'employee_age'
);
// getting total number records without any search
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql. = " FROM employee";
$query = mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql. = " FROM employee WHERE 1=1";
if (!empty($requestData['search']['value'])) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql. = " AND ( employee_name LIKE '".$requestData['search']['value'].
"%' ";
$sql. = " OR employee_salary LIKE '".$requestData['search']['value'].
"%' ";
$sql. = " OR employee_age LIKE '".$requestData['search']['value'].
"%' )";
}
$query = mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql. = " ORDER BY ".$columns[$requestData['order'][0]['column']].
" ".$requestData['order'][0]['dir'].
" LIMIT ".$requestData['start'].
" ,".$requestData['length'].
" ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length. */
$query = mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while ($row = mysqli_fetch_array($query)) { // preparing an array
$nestedData = array();
$nestedData[] = $row["employee_name"];
$nestedData[] = $row["employee_salary"];
$nestedData[] = $row["employee_age"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval($requestData['draw']), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval($totalData), // total number of records
"recordsFiltered" => intval($totalFiltered), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
Datatable實現滾動載入
一般來說datatable
都是預設使用“paging”:true
,這一屬性來控制全域性的表格翻頁。效果大致如下圖:
但是有時候你也會用到滾動條式的翻頁方式,類似下面這種:
這時,你只需要加上以下兩個屬性即可。
scrollY: 1000,
scroller: {
loadingIndicator: true
},
其中scrollY
表示:定義一個高度,當列表內容超過這個高度時,顯示垂直滾動條。
scroller
表示開啟滾動翻頁。
下面是官網論壇上給出的一個demo
:
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
serverSide: true,
ajax:{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get
error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");
}
},
dom: "frtiS",
scrollY: 200,
deferRender: true,
scroller: {
loadingIndicator: true
}
} );
} );
相關文章
- Swift Server-sideSwiftServerIDE
- Client Side Cache 和 Server Side Cache 的區別clientIDEServer
- Server-side vulnerabilities :path traversalServerIDE
- server-side-events(SSE)開發指南(Node)ServerIDE
- 如何在SAP Server Side JavaScript裡消費destinationServerIDEJavaScript
- Server-side rendering for any React app on any FaaS providerServerIDEReactAPP
- 深度剖析Kubernetes API Server三部曲 - part 3APIServer
- Debug a Server–Side Rendered SAP Spartacus Storefront Using Chrome Dev ToolsServerIDEChromedev
- 《The Rust Programming language》程式碼練習(part 3 簡單web server)RustWebServer
- 想列印k8s資源YAML結果搞懂了Client-Side & Server-Side ApplyK8SYAMLclientIDEServerAPP
- React 發明的 server component 居然和 server-side render不是一個東西ReactServerIDE
- Oracle 19c Concepts(08):Server-Side Programming: PL/SQL and JavaOracleServerIDESQLJava
- 揭開React中server-side rending的神祕面紗ReactServerIDE
- 一文說通Blazor for Server-Side的專案結構BlazorServerIDE
- 使用HANA Web-based Development Workbench建立最簡單的Server Side JavaScriptWebdevServerIDEJavaScript
- mysqlbinlog命令詳解 Part 8 指定 Server IDMySqlServer
- dataTable元件使用元件
- DataTable.ImportRow()與DataTable.Rows.Add()的區別Import
- Study Plan For Algorithms - Part3Go
- Server-side activities have been updated. You need to restart SharePoint Designer to use the updated version of activities.ServerIDEREST
- IDAPython 讓你的生活更滋潤 – Part 3 and Part 4Python
- datatable資料解析
- Goroutines: the dark side of the runtimeGoIDE
- BUUCTF:Beautiful_SideIDE
- 將DataTable一行放入另一個DataTable中(筆記)筆記
- 深度剖析Kubernetes API Server三部曲 - part 1APIServer
- 深度剖析Kubernetes API Server三部曲 - part 2APIServer
- Oracle RAC Cache Fusion 系列十四:Oracle RAC CR Server Part OneOracleServer
- 部署 Node.js 應用以完成伺服器端渲染 Server Side Rendering 的效能調優Node.js伺服器ServerIDE
- 8086 彙編學習 Part 3
- 疫情視覺化part3視覺化
- 音影片領域中, 5.1 7.1聲道不同版本的區別 ---- 5.1 5.1(side) 7.1 7.1(side) 7.1(wide-side)IDE
- Hybris做增強的兩種方式:In App Extension和Side by Side ExtensionAPPIDE
- DataTable 轉為 List<dynamic>
- C# Datatable 新增列C#
- DataTable中的select()用法
- jQuery datatable 使用方法jQuery
- C#:Dictionary轉DataTableC#