"""Offline Playwright Chromium lab for jQuery EasyUI Ch 37: DataGrid Merge Cells."""
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 Merge Cells Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.dg-box { width: 550px; border: 1px solid #95b8e7; }
.dg-title { background: #e0ecff; padding: 6px 8px; font-weight: bold; }
table#tt { width: 100%; border-collapse: collapse; }
table#tt th, table#tt td { border: 1px solid #ccc; padding: 4px 6px; height: 24px; box-sizing: border-box; }
table#tt th { background: #f4f4f4; }
.frozen-col { background: #fafafa; }
.dg-pager { background: #f4f4f4; padding: 4px 8px; border-top: 1px solid #ccc; font-size: 12px; }
</style>
</head>
<body>
<div class="dg-box">
<div class="dg-title">Merge Cells</div>
<table id="tt">
<thead>
<tr>
<th rowspan="2" data-role="rownumber">#</th>
<th rowspan="2" data-frozen="true" data-field="productid">Product ID</th>
<th rowspan="2" data-frozen="true" data-field="itemid">Item ID</th>
<th colspan="2" data-group="Price">Price</th>
<th rowspan="2" data-field="attr1">Attribute</th>
<th rowspan="2" data-field="status">Status</th>
</tr>
<tr>
<th data-field="listprice">List Price</th>
<th data-field="unitcost">Unit Cost</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div class="dg-pager">Page 1 of 1 (10 items)</div>
</div>
<script>
const colOrder = ["productid", "itemid", "listprice", "unitcost", "attr1", "status"];
const rows = [
{ productid: "FI-SW-01", itemid: "EST-1", listprice: 16.5, unitcost: 10, attr1: "Large", status: "P" },
{ productid: "K9-DL-01", itemid: "EST-10", listprice: 18.5, unitcost: 12, attr1: "Spotted Adult Female", status: "P" },
{ productid: "RP-SN-01", itemid: "EST-11", listprice: 18.5, unitcost: 12, attr1: "Venomless", status: "P" },
{ productid: "RP-SN-01", itemid: "EST-12", listprice: 18.5, unitcost: 12, attr1: "Rattleless", status: "P" },
{ productid: "RP-LI-02", itemid: "EST-13", listprice: 18.5, unitcost: 12, attr1: "Green Adult", status: "P" },
{ productid: "FL-DSH-01", itemid: "EST-14", listprice: 58.5, unitcost: 12, attr1: "Tailless", status: "P" },
{ productid: "FL-DSH-01", itemid: "EST-15", listprice: 23.5, unitcost: 12, attr1: "With tail", status: "P" },
{ productid: "FL-DLH-02", itemid: "EST-16", listprice: 93.5, unitcost: 12, attr1: "Adult Female", status: "P" },
{ productid: "FL-DLH-02", itemid: "EST-17", listprice: 93.5, unitcost: 12, attr1: "Adult Male", status: "P" },
{ productid: "AV-CB-01", itemid: "EST-18", listprice: 193.5, unitcost: 92, attr1: "Adult Male", status: "P" }
];
function renderRows() {
const tbody = document.querySelector("#tt tbody");
tbody.innerHTML = "";
rows.forEach((row, idx) => {
const tr = document.createElement("tr");
tr.dataset.index = String(idx);
const rn = document.createElement("td");
rn.textContent = String(idx + 1);
rn.className = "frozen-col";
tr.appendChild(rn);
colOrder.forEach(field => {
const td = document.createElement("td");
td.dataset.field = field;
if (field === "productid" || field === "itemid") td.className = "frozen-col";
td.textContent = String(row[field]);
tr.appendChild(td);
});
tbody.appendChild(tr);
});
}
function mergeCells(opts) {
const startRow = opts.index;
const field = opts.field;
const rowspan = opts.rowspan || 1;
const colspan = opts.colspan || 1;
const startColIdx = colOrder.indexOf(field);
const firstTd = document.querySelector(`#tt tbody tr[data-index="${startRow}"] td[data-field="${field}"]`);
if (!firstTd) return;
if (rowspan > 1) firstTd.rowSpan = rowspan;
if (colspan > 1) firstTd.colSpan = colspan;
for (let r = startRow; r < startRow + rowspan; r++) {
for (let c = startColIdx; c < startColIdx + colspan; c++) {
if (r === startRow && c === startColIdx) continue;
const hiddenTd = document.querySelector(`#tt tbody tr[data-index="${r}"] td[data-field="${colOrder[c]}"]`);
if (hiddenTd) hiddenTd.style.display = "none";
}
}
}
renderRows();
// Exact onLoadSuccess merges from source
const merges = [
{ index: 2, rowspan: 2 },
{ index: 5, rowspan: 2 },
{ index: 7, rowspan: 2 }
];
for (let i = 0; i < merges.length; i++) {
mergeCells({ index: merges[i].index, field: "productid", rowspan: merges[i].rowspan });
}
window.lab = { mergeCells };
</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: Frozen columns + multi-row grouped Price header + pager
res1 = page.evaluate("""() => {
const frozenFields = Array.from(document.querySelectorAll('#tt thead th[data-frozen="true"]')).map(el => el.dataset.field);
const priceTh = document.querySelector('#tt thead th[data-group="Price"]');
const attrTh = document.querySelector('#tt thead th[data-field="attr1"]');
return {
headerRowCount: document.querySelectorAll('#tt thead tr').length,
frozenFields,
priceColSpan: priceTh.colSpan,
attrRowSpan: attrTh.rowSpan,
hasPager: document.querySelector('.dg-pager') !== null
};
}""")
exp1 = {
"headerRowCount": 2,
"frozenFields": ["productid", "itemid"],
"priceColSpan": 2,
"attrRowSpan": 2,
"hasPager": True,
}
cases.append(dict(name="frozen_and_multi_row_header_structure", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: onLoadSuccess rowspan merges hide covered cells without detaching them
res2 = page.evaluate("""() => {
const check = (idx) => {
const td = document.querySelector(`#tt tbody tr[data-index="${idx}"] td[data-field="productid"]`);
return { rowSpan: td.rowSpan, display: getComputedStyle(td).display, text: td.textContent };
};
const mergedTops = [2, 5, 7].map(check);
const coveredHidden = [3, 6, 8].map(check);
const visibleCount = Array.from(document.querySelectorAll('#tt tbody td[data-field="productid"]'))
.filter(td => getComputedStyle(td).display !== "none").length;
const domCount = document.querySelectorAll('#tt tbody td[data-field="productid"]').length;
return { mergedTops, coveredHidden, visibleCount, domCount };
}""")
exp2 = {
"mergedTops": [
{"rowSpan": 2, "display": "table-cell", "text": "RP-SN-01"},
{"rowSpan": 2, "display": "table-cell", "text": "FL-DSH-01"},
{"rowSpan": 2, "display": "table-cell", "text": "FL-DLH-02"},
],
"coveredHidden": [
{"rowSpan": 1, "display": "none", "text": "RP-SN-01"},
{"rowSpan": 1, "display": "none", "text": "FL-DSH-01"},
{"rowSpan": 1, "display": "none", "text": "FL-DLH-02"},
],
"visibleCount": 7,
"domCount": 10,
}
cases.append(dict(name="onloadsuccess_mergecells_rowspan", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Colspan merge and merged cell vertical height check
res3 = page.evaluate("""() => {
window.lab.mergeCells({ index: 0, field: "listprice", colspan: 2 });
const lp = document.querySelector('#tt tbody tr[data-index="0"] td[data-field="listprice"]');
const uc = document.querySelector('#tt tbody tr[data-index="0"] td[data-field="unitcost"]');
const mergedH = Math.round(document.querySelector('#tt tbody tr[data-index="2"] td[data-field="productid"]').getBoundingClientRect().height);
const singleH = Math.round(document.querySelector('#tt tbody tr[data-index="0"] td[data-field="productid"]').getBoundingClientRect().height);
return {
listpriceColSpan: lp.colSpan,
unitcostDisplay: getComputedStyle(uc).display,
mergedHeightGreaterThanSingle: mergedH > singleH + 15
};
}""")
exp3 = {
"listpriceColSpan": 2,
"unitcostDisplay": "none",
"mergedHeightGreaterThanSingle": True,
}
cases.append(dict(name="mergecells_colspan_and_bounds", 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-datagrid13-lab/explore-datagrid-datagrid13.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datagrid-datagrid13.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a969.zip
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip