翻譯|使用教程|編輯:張瑩心|2021-11-03 10:25:15.310|閱讀 728 次
概述:在開發 Web 應用程序和界面時,可以將數據網格視為必不可少的組件。這個小部件允許通過以表格的形式顯示大量數據集來處理它們。在本文中,我們將展示如何使用 DHTMLX 構建 JavaScript 網格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在開發 Web 應用程序和界面時,可以將數據網格視為必不可少的組件。這個小部件允許通過以表格的形式顯示大量數據集來處理它們。
DHTMLX Grid 小部件提供了一個全面的功能集來處理大量數據并確保用戶友好的數據管理。它包括諸如數據過濾和排序、調整大小、行和列拖放、動態加載以及導出到 Excel 和 CSV 等方便的功能。
在本文中,我們將展示如何使用 DHTMLX 構建 JavaScript 網格。

<!DOCTYPE html>
<html>
<head>
<title>Grid demo</title>
// include source files of the DHTMLX Suite library
<script type="text/javascript" src="codebase/suite.js"></script>
<link rel="stylesheet" href="codebase/suite.css" />
// add dataset for the Grid
<script type="text/javascript" src="dataset.js"></script>
// add CSS classes with custom settings
<style>
...
</style>
</head>
接下來,您應該添加一個容器并為其指定一個 id,例如“grid”: <body>
<div id="grid" class="grid-wrapper"></div>
然后您必須使用對象構造函數初始化 Grid 小部件: <script>
// create a data set for the first column (instead you can add your data)
var users = [
{
name: "Gary Ortiz",
ava: "01.png",
},
{
name: "Albert Williamson",
ava: "02.png",
},
{
name: "Mildred Fuller",
ava: "03.png",
},
];
// specify Grid configurations
var config = {
// configure each Grid column
columns: [
...
],
},
// initialize the Grid widget and add specified configurations
this.widget = new dhx.Grid("grid", config);
var grid = this.widget;
</script>
</body>
</html>
.grid-wrapper {
width: 1100px;
height: 560px;
}
將背景顏色應用于網格行:.my_custom_row {
background: #b4dbfe;
}
設置用戶照片的位置:.demo_grid-user-photo {
border-radius: 50%;
width: 24px;
height: 24px;
background: #61c874;
text-align: center;
line-height: 23px;
border: solid 1px #fff;
color: white;
font-weight: 500;
margin-right: -3px;
}
根據任務的狀態指定具有任務的單元格的外觀:.demo_grid-task {
height: 24px;
max-width: 92px;
line-height: 25px;
margin: 8px auto 0 auto;
border-radius: 100px;
text-align: center;
}
.demo_grid-task__done {
background: linear-gradient(
0deg,
rgba(10, 177, 105, 0.1),
rgba(10, 177, 105, 0.1)
),
#ffffff;
color: #0ab169;
}
.demo_grid-task__progress {
background: linear-gradient(
0deg,
rgba(2, 136, 209, 0.1),
rgba(2, 136, 209, 0.1)
),
#ffffff;
color: #0288d1;
}
.demo_grid-task__not-started {
background: linear-gradient(
0deg,
rgba(122, 134, 140, 0.1),
rgba(122, 134, 140, 0.1)
),
#ffffff;
color: rgba(0, 0, 0, 0.7);
}
columns: [
{
// assign column width
width: 150,
// set the id of a column
id: "access",
// define the type of the editor and the content alignment
type: "string",
// configure the header and footer
header: [{ text: "Access", rowspan: 2 }],
footer: [{ text: "Total" }],
// define the ability to edit data
editable: false,
// set a template with content for a cell
template: function (value) {
if (!value) {
return;
}
var photos = value.reduce(function (total, user) {
return (
total +
(user.ava
? '<img src="./images/' +
user.ava +
'" class="demo_grid-user-photo"/>'
: '<div class="demo_grid-user-photo" style="background:' +
user.color +
';">' +
user.name.slice(0, 1) +
"</div>")
);
}, "");
return '<div class="demo_grid-user">' + photos + "</div>";
},
}
{
editorType: "select",
options: ["Done", "In Progress", "Not Started"],
}
此外,您可以應用創建的 CSS 類,這些類將根據所選選項修改 Grid 單元格:template: function (value) {
if (value === "Done") {
return (
'<div class="demo_grid-task demo_grid-task__done">' +
value +
"</div>"
);
}
if (value === "In Progress") {
return (
'<div class="demo_grid-task demo_grid-task__progress">' +
value +
"</div>"
);
}
if (value === "Not Started") {
return (
'<div class="demo_grid-task demo_grid-task__not-started">' +
value +
"</div>"
);
}
if (value === "Active") {
return (
'<div class="demo_grid-task demo_grid-task__active">' +
value +
"</div>"
);
}
},

id: "project",
header: [{ text: "Project" }, { content: "selectFilter" }],

id: "owner",
header: [{ text: "Owner" }, { content: "comboFilter" }],
指定 inputFilter 屬性后,您允許用戶使用文本字段過濾數據:id: "hours",
header: [{ text: "Number of hours" }, { content: "inputFilter" }],

this.widget.content["moneySum"] = {
calculate: function (data) {
return data.reduce(function (sum, c) {
return sum + parseFloat(c) || sum + 0;
}, 0);
},
toHtml: function (column, config) {
if (grid.data) {
var data = grid.data.map(function (item) {
return item[column.id];
});
return this.calculate(data).toLocaleString("en", {
style: "currency",
currency: "USD",
});
}
},
};

而且,你必須在列配置中提到這個公式:
id: "cost",
footer: [{ content: "moneySum" }],
想要購買dhtmlx正版授權,或了解慧都APS系統請點擊本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn