"""Offline Playwright Chromium lab for jQuery EasyUI Ch 39: DataGrid Footer Summary."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>EasyUI DataGrid Footer Summary Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.dg-wrap { width: 400px; border: 1px solid #95b8e7; margin-bottom: 14px; }
.dg-title { background: #e0ecff; padding: 6px 8px; font-weight: bold; }
table.dg { width: 100%; border-collapse: collapse; table-layout: fixed; }
table.dg th, table.dg td { border: 1px solid #ccc; padding: 4px 6px; }
table.dg th { background: #f4f4f4; }
.datagrid-footer { background: #fafafa; font-weight: bold; border-top: 2px solid #95b8e7; }
.datagrid-footer.hidden { display: none; }
</style>
</head>
<body>
<div id="grid-box"></div>
<script>
function mountDataGrid(container, opts, payload) {
container.innerHTML = "";
const wrap = document.createElement("div");
wrap.className = "dg-wrap";
wrap.innerHTML = `
<div class="dg-title">${opts.title || "DataGrid"}</div>
<table class="dg">
<thead><tr>
${opts.rownumbers ? '<th style="width:32px">#</th>' : ""}
<th style="width:80%">Product Name</th>
<th style="width:20%;text-align:right">Unit Price</th>
</tr></thead>
<tbody class="datagrid-body"></tbody>
<tfoot class="datagrid-footer ${opts.showFooter ? "" : "hidden"}"></tfoot>
</table>`;
const tbody = wrap.querySelector(".datagrid-body");
const tfoot = wrap.querySelector(".datagrid-footer");
(payload.rows || []).forEach((r, idx) => {
const tr = document.createElement("tr");
tr.innerHTML = `${opts.rownumbers ? `<td>${idx + 1}</td>` : ""}<td>${r.name}</td><td style="text-align:right">${Number(r.price).toFixed(2)}</td>`;
tbody.appendChild(tr);
});
if (opts.showFooter && Array.isArray(payload.footer)) {
payload.footer.forEach(f => {
const tr = document.createElement("tr");
tr.innerHTML = `${opts.rownumbers ? "<td></td>" : ""}<td>${f.name}</td><td style="text-align:right">${Number(f.price).toFixed(2)}</td>`;
tfoot.appendChild(tr);
});
}
container.appendChild(wrap);
return {
getRows: () => payload.rows || [],
getFooterRows: () => (opts.showFooter ? (payload.footer || []) : [])
};
}
window.lab = { mountDataGrid };
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Single summary footer row with exact source JSON structure
res1 = page.evaluate("""() => {
const payload = {"total":1,"rows":[{"id":1,"name":"Chai","price":18.00}],"footer":[{"name":"Total","price":18.00}]};
const dg = window.lab.mountDataGrid(document.getElementById("grid-box"), { title: "DataGrid", rownumbers: true, showFooter: true }, payload);
const bodyRow = Array.from(document.querySelectorAll(".datagrid-body tr:first-child td")).map(td => td.textContent);
const footerRow = Array.from(document.querySelectorAll(".datagrid-footer tr:first-child td")).map(td => td.textContent);
return {
bodyCount: dg.getRows().length,
footerCount: dg.getFooterRows().length,
bodyRow,
footerRow
};
}""")
exp1 = {
"bodyCount": 1,
"footerCount": 1,
"bodyRow": ["1", "Chai", "18.00"],
"footerRow": ["", "Total", "18.00"],
}
cases.append(dict(name="showfooter_single_summary_row", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Multiple summary rows in footer separated from body data rows
res2 = page.evaluate("""() => {
const payload = {
total: 3,
rows: [
{ id: 1, name: "Chai", price: 18.00 },
{ id: 2, name: "Chang", price: 19.00 },
{ id: 3, name: "Tofu", price: 23.00 }
],
footer: [
{ name: "Average", price: 20.00 },
{ name: "Total", price: 60.00 }
]
};
const dg = window.lab.mountDataGrid(document.getElementById("grid-box"), { title: "DataGrid", rownumbers: true, showFooter: true }, payload);
const footerTexts = Array.from(document.querySelectorAll(".datagrid-footer tr")).map(tr =>
Array.from(tr.querySelectorAll("td")).slice(1).map(td => td.textContent)
);
return {
bodyCount: dg.getRows().length,
footerDomRows: document.querySelectorAll(".datagrid-footer tr").length,
footerTexts
};
}""")
exp2 = {
"bodyCount": 3,
"footerDomRows": 2,
"footerTexts": [["Average", "20.00"], ["Total", "60.00"]],
}
cases.append(dict(name="showfooter_multiple_summary_rows", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: showFooter=false hides footer even when payload contains footer array
res3 = page.evaluate("""() => {
const payload = {"total":1,"rows":[{"id":1,"name":"Chai","price":18.00}],"footer":[{"name":"Total","price":18.00}]};
const dg = window.lab.mountDataGrid(document.getElementById("grid-box"), { title: "DataGrid", rownumbers: true, showFooter: false }, payload);
const tfoot = document.querySelector(".datagrid-footer");
return {
bodyDomRows: document.querySelectorAll(".datagrid-body tr").length,
footerDomRows: document.querySelectorAll(".datagrid-footer tr").length,
footerDisplay: getComputedStyle(tfoot).display,
apiFooterCount: dg.getFooterRows().length
};
}""")
exp3 = {
"bodyDomRows": 1,
"footerDomRows": 0,
"footerDisplay": "none",
"apiFooterCount": 0,
}
cases.append(dict(name="showfooter_false_hides_footer", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/jeasyui-datagrid-datagrid17-lab/explore-datagrid-datagrid17.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datagrid-datagrid17.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a973.zip
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip