"""Offline Playwright Chromium lab for jQuery EasyUI Ch 31: Column Group."""
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 Column Group Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 12px; }
.dg-panel { width: 550px; border: 1px solid #95b8e7; box-sizing: border-box; }
.dg-title { background: #e0ecff; padding: 6px 8px; font-weight: 600; font-size: 13px; border-bottom: 1px solid #95b8e7; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 12px; }
th, td { border: 1px solid #ccc; padding: 4px 6px; box-sizing: border-box; }
th { background: #efefef; }
tr.dg-row { cursor: pointer; }
tr.dg-row.selected { background: #ffe48d; }
</style>
</head>
<body>
<div class="dg-panel">
<div class="dg-title">Column Group</div>
<table id="tt" title="Column Group" singleSelect="true" iconCls="icon-save" rownumbers="true">
<thead>
<tr>
<th rowspan="2" width="28">#</th>
<th rowspan="2" field="itemid" width="80">Item ID</th>
<th rowspan="2" field="productid" width="80">Product ID</th>
<th colspan="4" id="grp-details">Item Details</th>
</tr>
<tr>
<th field="listprice" width="80" align="right">List Price</th>
<th field="unitcost" width="80" align="right">Unit Cost</th>
<th field="attr1" width="100">Attribute</th>
<th field="status" width="60" align="center">Status</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<script>
const FLAT_DATA = [
{ itemid: "EST-1", productid: "FI-SW-01", listprice: 16.5, unitcost: 10.0, attr1: "Large", status: "P" },
{ itemid: "EST-10", productid: "K9-DL-01", listprice: 18.5, unitcost: 12.0, attr1: "Spotted", status: "P" },
{ itemid: "EST-11", productid: "RP-SN-01", listprice: 28.5, unitcost: 12.0, attr1: "Venomless", status: "P" },
];
const leafHeaders = Array.from(document.querySelectorAll('#tt thead th[field]'));
const tbody = document.querySelector('#tt tbody');
FLAT_DATA.forEach((row, idx) => {
const tr = document.createElement('tr');
tr.className = 'dg-row';
tr.dataset.itemid = row.itemid;
let html = `<td style="text-align:center">${idx + 1}</td>`;
leafHeaders.forEach(th => {
const f = th.getAttribute('field');
const align = th.getAttribute('align');
html += `<td data-field="${f}" style="${align ? 'text-align:' + align : ''}">${row[f]}</td>`;
});
tr.innerHTML = html;
tr.addEventListener('click', () => {
tbody.querySelectorAll('tr.selected').forEach(el => el.classList.remove('selected'));
tr.classList.add('selected');
});
tbody.appendChild(tr);
});
</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(viewport={"width": 680, "height": 420})
page.set_content(HTML_DOC)
# Case 1: 2-row header with rowspan=2 and colspan=4 over 6 leaf fields
res1 = page.evaluate("""() => {
const rows = Array.from(document.querySelectorAll('#tt thead tr'));
const row0 = Array.from(rows[0].querySelectorAll('th')).map(th => ({
text: th.textContent.trim(),
field: th.getAttribute('field'),
rowspan: th.rowSpan,
colspan: th.colSpan,
}));
const row1Fields = Array.from(rows[1].querySelectorAll('th')).map(th => th.getAttribute('field'));
const leafFields = Array.from(document.querySelectorAll('#tt thead th[field]')).map(th => th.getAttribute('field'));
return { headerRowCount: rows.length, row0, row1Fields, leafFields };
}""")
exp1 = {
"headerRowCount": 2,
"row0": [
{"text": "#", "field": None, "rowspan": 2, "colspan": 1},
{"text": "Item ID", "field": "itemid", "rowspan": 2, "colspan": 1},
{"text": "Product ID", "field": "productid", "rowspan": 2, "colspan": 1},
{"text": "Item Details", "field": None, "rowspan": 1, "colspan": 4},
],
"row1Fields": ["listprice", "unitcost", "attr1", "status"],
"leafFields": ["itemid", "productid", "listprice", "unitcost", "attr1", "status"],
}
cases.append(dict(name="column_group_rowspan_colspan_structure", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Flat JSON data maps cleanly across row number + 6 leaf cells
res2 = page.evaluate("""() => {
const firstTr = document.querySelector('#tt tbody tr:first-child');
const tds = Array.from(firstTr.querySelectorAll('td'));
return {
rowCount: document.querySelectorAll('#tt tbody tr').length,
cellCount: tds.length,
firstRowTexts: tds.map(td => td.textContent.trim()),
firstRowFields: tds.slice(1).map(td => td.dataset.field),
};
}""")
exp2 = {
"rowCount": 3,
"cellCount": 7,
"firstRowTexts": ["1", "EST-1", "FI-SW-01", "16.5", "10", "Large", "P"],
"firstRowFields": ["itemid", "productid", "listprice", "unitcost", "attr1", "status"],
}
cases.append(dict(name="flat_data_mapping_under_grouped_header", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Group header bounding width equals sum of 4 child headers & singleSelect works
page.click('tr.dg-row[data-itemid="EST-1"]')
page.click('tr.dg-row[data-itemid="EST-11"]')
res3 = page.evaluate("""() => {
const grpW = document.getElementById('grp-details').getBoundingClientRect().width;
const childSum = Array.from(document.querySelectorAll('#tt thead tr:nth-child(2) th'))
.reduce((s, th) => s + th.getBoundingClientRect().width, 0);
const selected = Array.from(document.querySelectorAll('#tt tbody tr.selected')).map(tr => tr.dataset.itemid);
return {
groupCoversFourChildren: Math.abs(grpW - childSum) <= 2,
singleSelectedIds: selected,
};
}""")
exp3 = {"groupCoversFourChildren": True, "singleSelectedIds": ["EST-11"]}
cases.append(dict(name="group_header_bounding_geometry_and_single_select", 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-datagrid9-lab/explore-datagrid-datagrid9.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datagrid-datagrid9.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a989.zip
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip