"""Offline Playwright Chromium lab for jQuery EasyUI Ch 26: Frozen Columns."""
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 Frozen Columns Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 12px; }
.dg-panel { width: 500px; border: 1px solid #95b8e7; box-sizing: border-box; margin-bottom: 16px; }
.dg-title { background: #e0ecff; padding: 6px 8px; font-weight: 600; font-size: 13px; border-bottom: 1px solid #95b8e7; }
.datagrid-scroll { width: 500px; height: 180px; overflow-x: auto; overflow-y: auto; position: relative; }
table.dg-frozen-tbl { border-collapse: separate; border-spacing: 0; table-layout: fixed; font-size: 12px; }
table.dg-frozen-tbl th, table.dg-frozen-tbl td {
border-right: 1px solid #ccc; border-bottom: 1px solid #ccc;
padding: 4px 6px; box-sizing: border-box; background: #fff; white-space: nowrap;
}
table.dg-frozen-tbl th { background: #efefef; }
table.dg-frozen-tbl .frozen-col { position: sticky; z-index: 2; background: #f8fbff; }
table.dg-frozen-tbl th.frozen-col { z-index: 3; background: #e6eefb; }
</style>
</head>
<body>
<div id="js-grid-host"></div>
<table id="decl-tt" title="Frozen Columns" class="easyui-datagrid" style="width:500px;height:250px"
singleSelect="true" iconCls="icon-save">
<thead frozen="true">
<tr>
<th field="itemid" width="80">Item ID</th>
<th field="productid" width="80">Product ID</th>
</tr>
</thead>
<thead>
<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="150">Attribute</th>
<th field="status" width="60" align="center">Status</th>
</tr>
</thead>
</table>
<script>
const ROWS = [
{ itemid: "EST-1", productid: "FI-SW-01", listprice: 16.5, unitcost: 10.0, attr1: "Large Adult Male Tropical Fish", status: "P" },
{ itemid: "EST-10", productid: "K9-DL-01", listprice: 18.5, unitcost: 12.0, attr1: "Spotted Adult Female Dalmatian", status: "P" },
{ itemid: "EST-11", productid: "RP-SN-01", listprice: 28.5, unitcost: 12.0, attr1: "Venomless Desert Rattlesnake", status: "P" },
];
function buildFrozenGrid(hostEl, cfg) {
const panel = document.createElement('div');
panel.className = 'dg-panel';
panel.innerHTML = `<div class="dg-title">${cfg.title}</div><div class="datagrid-scroll" id="scroll-box"></div>`;
const scrollBox = panel.querySelector('#scroll-box');
const frozen = cfg.frozenColumns[0];
const normal = cfg.columns[0];
const allCols = [...frozen.map(c => ({ ...c, frozen: true })), ...normal.map(c => ({ ...c, frozen: false }))];
let leftOffset = 0;
allCols.forEach(c => {
if (c.frozen) {
c.stickyLeft = leftOffset;
leftOffset += c.width;
}
});
const totalWidth = allCols.reduce((s, c) => s + c.width, 0);
const tbl = document.createElement('table');
tbl.className = 'dg-frozen-tbl';
tbl.style.width = totalWidth + 'px';
const thead = document.createElement('thead');
const htr = document.createElement('tr');
allCols.forEach(c => {
const th = document.createElement('th');
th.dataset.field = c.field;
th.style.width = c.width + 'px';
th.style.minWidth = c.width + 'px';
th.textContent = c.title;
if (c.frozen) {
th.className = 'frozen-col';
th.style.left = c.stickyLeft + 'px';
}
htr.appendChild(th);
});
thead.appendChild(htr);
tbl.appendChild(thead);
const tbody = document.createElement('tbody');
ROWS.forEach(r => {
const tr = document.createElement('tr');
allCols.forEach(c => {
const td = document.createElement('td');
td.dataset.field = c.field;
td.style.width = c.width + 'px';
td.style.minWidth = c.width + 'px';
if (c.align) td.style.textAlign = c.align;
td.textContent = r[c.field];
if (c.frozen) {
td.className = 'frozen-col';
td.style.left = c.stickyLeft + 'px';
}
tr.appendChild(td);
});
tbody.appendChild(tr);
});
tbl.appendChild(tbody);
scrollBox.appendChild(tbl);
hostEl.appendChild(panel);
}
buildFrozenGrid(document.getElementById('js-grid-host'), {
title: 'Frozen Columns',
width: 500,
height: 250,
frozenColumns: [[
{ field: 'itemid', title: 'Item ID', width: 80 },
{ field: 'productid', title: 'Product ID', width: 80 },
]],
columns: [[
{ field: 'listprice', title: 'List Price', width: 120, align: 'right' },
{ field: 'unitcost', title: 'Unit Cost', width: 120, align: 'right' },
{ field: 'attr1', title: 'Attribute', width: 240 },
{ field: 'status', title: 'Status', width: 100 },
]]
});
window.parseDeclarativeFrozen = function() {
const tbl = document.getElementById('decl-tt');
const frozenThead = tbl.querySelector('thead[frozen="true"]');
const normalThead = tbl.querySelector('thead:not([frozen="true"])');
return {
title: tbl.getAttribute('title'),
singleSelect: tbl.getAttribute('singleSelect') === 'true',
frozenFields: Array.from(frozenThead.querySelectorAll('th')).map(th => th.getAttribute('field')),
normalFields: Array.from(normalThead.querySelectorAll('th')).map(th => th.getAttribute('field')),
};
};
</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": 700, "height": 500})
page.set_content(HTML_DOC)
# Case 1: JS frozenColumns sticky configuration
res1 = page.evaluate("""() => {
const c0 = getComputedStyle(document.querySelector('th[data-field="itemid"]'));
const c1 = getComputedStyle(document.querySelector('th[data-field="productid"]'));
const c2 = getComputedStyle(document.querySelector('th[data-field="listprice"]'));
const box = document.getElementById('scroll-box');
return {
itemidPosition: c0.position,
itemidLeft: c0.left,
productidPosition: c1.position,
productidLeft: c1.left,
listpricePosition: c2.position,
canScrollHorizontally: box.scrollWidth > box.clientWidth,
};
}""")
exp1 = {
"itemidPosition": "sticky",
"itemidLeft": "0px",
"productidPosition": "sticky",
"productidLeft": "80px",
"listpricePosition": "static",
"canScrollHorizontally": True,
}
cases.append(dict(name="js_frozen_columns_config_and_sticky", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Horizontal scroll keeps frozen cells pinned while normal columns scroll
res2 = page.evaluate("""() => {
const box = document.getElementById('scroll-box');
const beforeItem = document.querySelector('tbody td[data-field="itemid"]').getBoundingClientRect().left;
const beforeProd = document.querySelector('tbody td[data-field="productid"]').getBoundingClientRect().left;
const beforePrice = document.querySelector('tbody td[data-field="listprice"]').getBoundingClientRect().left;
box.scrollLeft = 180;
const afterItem = document.querySelector('tbody td[data-field="itemid"]').getBoundingClientRect().left;
const afterProd = document.querySelector('tbody td[data-field="productid"]').getBoundingClientRect().left;
const afterPrice = document.querySelector('tbody td[data-field="listprice"]').getBoundingClientRect().left;
return {
scrollLeft: box.scrollLeft,
itemidDelta: Math.round(afterItem - beforeItem),
productidDelta: Math.round(afterProd - beforeProd),
listpriceDelta: Math.round(afterPrice - beforePrice),
};
}""")
exp2 = {
"scrollLeft": 180,
"itemidDelta": 0,
"productidDelta": 0,
"listpriceDelta": -180,
}
cases.append(dict(name="horizontal_scroll_keeps_frozen_in_viewport", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Declarative <thead frozen="true"> equivalence
res3 = page.evaluate("() => window.parseDeclarativeFrozen()")
exp3 = {
"title": "Frozen Columns",
"singleSelect": True,
"frozenFields": ["itemid", "productid"],
"normalFields": ["listprice", "unitcost", "attr1", "status"],
}
cases.append(dict(name="declarative_thead_frozen_true_equivalence", 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-datagrid5-lab/explore-datagrid-datagrid5.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datagrid-datagrid5.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a985.zip
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip