"""Offline Playwright Chromium lab for jQuery EasyUI Ch 25: DataGrid Complex Toolbar."""
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 Complex Toolbar Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 12px; }
.datagrid-wrap { width: 600px; border: 1px solid #95b8e7; box-sizing: border-box; overflow: hidden; }
.datagrid-title { background: #e0ecff; padding: 6px 8px; font-weight: 600; font-size: 13px; border-bottom: 1px solid #95b8e7; }
#tb { padding: 5px; height: auto; background: #f4f4f4; border-bottom: 1px solid #ddd; font-size: 12px; }
.easyui-linkbutton { display: inline-block; padding: 2px 6px; margin-right: 4px; text-decoration: none; color: #333; border: 1px solid #ccc; background: #fff; font-size: 12px; }
.easyui-linkbutton[plain="true"] { border-color: transparent; background: transparent; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 12px; }
th, td { border: 1px solid #ddd; padding: 4px 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
th { background: #efefef; }
tr.dg-row { cursor: pointer; }
tr.dg-row.selected { background: #ffe48d; }
</style>
</head>
<body>
<div id="tb" style="padding:5px;height:auto">
<div id="tb-row1" style="margin-bottom:5px">
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true"></a>
<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true"></a>
</div>
<div id="tb-row2">
Date From: <input id="date-from" type="date" class="easyui-datebox" style="width:110px">
To: <input id="date-to" type="date" class="easyui-datebox" style="width:110px">
Language:
<select id="lang-combo" class="easyui-combobox" style="width:100px" data-value-field="id" data-text-field="text"></select>
<a href="#" id="btn-search" class="easyui-linkbutton" iconCls="icon-search" onclick="applyFilter();return false;">Search</a>
</div>
</div>
<table id="tt" class="easyui-datagrid" style="width:600px;height:250px"
title="DataGrid - Complex Toolbar" toolbar="#tb" singleSelect="true" fitColumns="true">
<thead>
<tr>
<th field="itemid" width="60">Item ID</th>
<th field="productid" width="80">Product ID</th>
<th field="listprice" align="right" width="70">List Price</th>
<th field="unitcost" align="right" width="70">Unit Cost</th>
<th field="attr1" width="200">Address</th>
<th field="status" width="50">Status</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
const LANGS = [
{ id: "", text: "All" },
{ id: "java", text: "Java" },
{ id: "python", text: "Python" },
{ id: "js", text: "JavaScript" },
];
const ROWS = [
{ itemid: "EST-1", productid: "FI-SW-01", listprice: 16.5, unitcost: 10.0, attr1: "Large", status: "P", lang: "java", date: "2025-01-12" },
{ itemid: "EST-2", productid: "K9-DL-01", listprice: 18.5, unitcost: 12.0, attr1: "Spotted", status: "P", lang: "python", date: "2025-01-18" },
{ itemid: "EST-3", productid: "RP-SN-01", listprice: 28.5, unitcost: 12.0, attr1: "Venomless", status: "P", lang: "java", date: "2025-02-05" },
];
const combo = document.getElementById('lang-combo');
LANGS.forEach(item => {
const opt = document.createElement('option');
opt.value = item.id;
opt.textContent = item.text;
combo.appendChild(opt);
});
const tbl = document.getElementById('tt');
const wrap = document.createElement('div');
wrap.className = 'datagrid-wrap';
wrap.id = 'tt-wrap';
const titleDiv = document.createElement('div');
titleDiv.className = 'datagrid-title';
titleDiv.textContent = tbl.getAttribute('title');
wrap.appendChild(titleDiv);
wrap.appendChild(document.getElementById('tb'));
tbl.parentNode.insertBefore(wrap, tbl);
wrap.appendChild(tbl);
// Fit columns proportionally to container width
const ths = Array.from(tbl.querySelectorAll('thead th'));
const sumW = ths.reduce((s, th) => s + parseInt(th.getAttribute('width'), 10), 0);
ths.forEach(th => {
const pct = (parseInt(th.getAttribute('width'), 10) / sumW) * 100;
th.style.width = pct.toFixed(4) + '%';
});
let selectedItemId = null;
window.lastFilter = null;
function renderRows(list) {
const tbody = tbl.querySelector('tbody');
tbody.innerHTML = '';
selectedItemId = null;
list.forEach((r, idx) => {
const tr = document.createElement('tr');
tr.className = 'dg-row';
tr.dataset.itemid = r.itemid;
tr.dataset.idx = String(idx);
tr.innerHTML = `<td>${r.itemid}</td><td>${r.productid}</td><td style="text-align:right">${r.listprice}</td><td style="text-align:right">${r.unitcost}</td><td>${r.attr1}</td><td>${r.status}</td>`;
tr.addEventListener('click', () => {
// singleSelect="true"
tbody.querySelectorAll('tr.selected').forEach(el => el.classList.remove('selected'));
tr.classList.add('selected');
selectedItemId = r.itemid;
});
tbody.appendChild(tr);
});
}
window.applyFilter = function() {
const dateFrom = document.getElementById('date-from').value;
const dateTo = document.getElementById('date-to').value;
const langId = combo.value;
const langText = combo.options[combo.selectedIndex].textContent;
window.lastFilter = { dateFrom, dateTo, languageId: langId, languageText: langText };
const filtered = ROWS.filter(r => {
if (dateFrom && r.date < dateFrom) return false;
if (dateTo && r.date > dateTo) return false;
if (langId && r.lang !== langId) return false;
return true;
});
renderRows(filtered);
};
window.getSelectedIds = () => Array.from(tbl.querySelectorAll('tbody tr.selected')).map(tr => tr.dataset.itemid);
window.getVisibleIds = () => Array.from(tbl.querySelectorAll('tbody tr')).map(tr => tr.dataset.itemid);
renderRows(ROWS);
</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": 760, "height": 480})
page.set_content(HTML_DOC)
# Case 1: Two-row complex toolbar with 5 plain icons, 2 dateboxes, 1 combobox, 1 search button
res1 = page.evaluate("""() => {
const row1Icons = Array.from(document.querySelectorAll('#tb-row1 .easyui-linkbutton')).map(a => a.getAttribute('iconCls'));
const dateCount = document.querySelectorAll('#tb-row2 .easyui-datebox').length;
const combo = document.getElementById('lang-combo');
const searchBtn = document.getElementById('btn-search');
return {
toolbarInsideWrap: document.getElementById('tt-wrap').contains(document.getElementById('tb')),
row1Icons,
dateboxCount: dateCount,
comboFields: { valueField: combo.dataset.valueField, textField: combo.dataset.textField },
searchIcon: searchBtn.getAttribute('iconCls'),
searchText: searchBtn.textContent.trim(),
};
}""")
exp1 = {
"toolbarInsideWrap": True,
"row1Icons": ["icon-add", "icon-edit", "icon-save", "icon-cut", "icon-remove"],
"dateboxCount": 2,
"comboFields": {"valueField": "id", "textField": "text"},
"searchIcon": "icon-search",
"searchText": "Search",
}
cases.append(dict(name="two_row_complex_toolbar_structure", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: singleSelect="true" & fitColumns="true"
page.click('tr.dg-row[data-itemid="EST-1"]')
sel_after_first = page.evaluate("() => window.getSelectedIds()")
page.click('tr.dg-row[data-itemid="EST-2"]')
res2 = page.evaluate("""(selFirst) => {
const wrapRect = document.getElementById('tt-wrap').getBoundingClientRect();
const tblRect = document.getElementById('tt').getBoundingClientRect();
return {
selAfterFirst: selFirst,
selAfterSecond: window.getSelectedIds(),
fitsContainerWithoutOverflow: tblRect.width <= wrapRect.width + 1,
wrapWidth: Math.round(wrapRect.width),
};
}""", sel_after_first)
exp2 = {
"selAfterFirst": ["EST-1"],
"selAfterSecond": ["EST-2"],
"fitsContainerWithoutOverflow": True,
"wrapWidth": 600,
}
cases.append(dict(name="single_select_and_fit_columns", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Date range + combobox filter interaction
page.fill("#date-from", "2025-01-01")
page.fill("#date-to", "2025-01-31")
page.select_option("#lang-combo", "java")
page.click("#btn-search")
res3 = page.evaluate("""() => ({
filter: window.lastFilter,
visibleIds: window.getVisibleIds(),
})""")
exp3 = {
"filter": {
"dateFrom": "2025-01-01",
"dateTo": "2025-01-31",
"languageId": "java",
"languageText": "Java",
},
"visibleIds": ["EST-1"],
}
cases.append(dict(name="datebox_combobox_filter_interaction", 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-datagrid19-lab/explore-datagrid-datagrid19.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-datagrid-datagrid19.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a975.zip
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip