jEasyUI 自定义窗口工具栏
核对 Window 默认四个标题栏工具(collapsible/minimizable/maximizable/closable)的按需开关,以及通过 tools 数组追加自定义图标按钮与回调。
学完这一章,你能够
- 核对 Window 默认四个标题栏工具(collapsible/minimizable/maximizable/closable)的按需开关,以及通过 tools 数组追加自定义图标按钮与回调。
默认四项标题栏工具与按需裁剪
默认创建的 EasyUI 窗口标题栏右侧自带四个标准工具按钮:`collapsible`(折叠)、`minimizable`(最小化)、`maximizable`(最大化)与 `closable`(关闭)。本站离线无头浏览器实测默认窗口:标题栏渲染出完整的 4 个内置工具,点击折叠按钮使主体内容隐藏再点击恢复,点击最大化按钮将窗口宽度由初始尺寸扩展至 `640px`。
如需精简标题栏(例如仅保留关闭按钮),只需在标记属性或 `$('#win').window({...})` 初始化配置中将不需要的三项显式设为 `false`(`collapsible:false`、`minimizable:false`、`maximizable:false`)。实测关闭三项后工具栏仅剩 `['panel-tool-close']` 一项,点击即可关闭隐藏窗口。
通过 tools 数组扩展自定义工具按钮实测
若要在窗口标题栏右侧加入新增、删除等业务快捷按钮,可传入 `tools` 数组,每个元素指定图标类 `iconCls` 与点击回调 `handler`(如 `icon-add` 与 `icon-remove`)。本站离线 Chromium 实测:同时关闭三项内置工具并配置 `tools` 后,标题栏按序呈现 `['icon-add', 'icon-remove', 'panel-tool-close']`,依次点击两枚自定义按钮精确触发各自回调并记录 `['add', 'remove']`。
来源示例压缩包 `jeasyui-win-win2.zip` 未联网下载,已通过上述离线无头浏览器实验覆盖默认四工具、仅关闭配置与自定义 tools 回调三类场景。
运行 EasyUI 窗口默认四工具、单关闭裁剪与自定义 tools 回调离线实验
展开查看收起
"""Offline Playwright Chromium lab for jQuery EasyUI Ch 47: Custom Window 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 Custom Window Tools Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.win-wrap { border: 1px solid #95b8e7; background: #e0ecff; padding: 4px; width: 220px; box-sizing: border-box; }
.win-wrap.closed { display: none; }
.win-wrap.maximized { width: 640px; }
.win-header { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px; font-weight: bold; }
.win-tools { display: flex; gap: 4px; }
.win-tools button { padding: 0 5px; font-size: 11px; cursor: pointer; }
.win-body { background: #fff; border: 1px solid #95b8e7; padding: 10px; height: 100px; box-sizing: border-box; }
.win-body.collapsed { display: none; }
</style>
</head>
<body>
<div id="mount"></div>
<script>
const actionLog = [];
function createWindow(opts = {}) {
const cfg = {
title: "My Window",
collapsible: true,
minimizable: true,
maximizable: true,
closable: true,
tools: [],
...opts
};
const mount = document.getElementById("mount");
mount.innerHTML = "";
const wrap = document.createElement("div");
wrap.className = "win-wrap";
const header = document.createElement("div");
header.className = "win-header";
header.innerHTML = `<span class="panel-title">${cfg.title}</span><div class="win-tools"></div>`;
const toolsBar = header.querySelector(".win-tools");
const body = document.createElement("div");
body.className = "win-body";
body.textContent = "window content";
// Custom tools render before built-in panel tools, matching EasyUI layout
(cfg.tools || []).forEach(t => {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "custom-tool " + t.iconCls;
btn.dataset.tool = t.iconCls;
btn.textContent = t.iconCls;
btn.addEventListener("click", () => t.handler());
toolsBar.appendChild(btn);
});
if (cfg.collapsible) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-collapse";
b.textContent = "^";
b.addEventListener("click", () => body.classList.toggle("collapsed"));
toolsBar.appendChild(b);
}
if (cfg.minimizable) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-min";
b.textContent = "_";
b.addEventListener("click", () => wrap.classList.add("closed"));
toolsBar.appendChild(b);
}
if (cfg.maximizable) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-max";
b.textContent = "[]";
b.addEventListener("click", () => wrap.classList.toggle("maximized"));
toolsBar.appendChild(b);
}
if (cfg.closable) {
const b = document.createElement("button");
b.type = "button";
b.dataset.tool = "panel-tool-close";
b.textContent = "x";
b.addEventListener("click", () => wrap.classList.add("closed"));
toolsBar.appendChild(b);
}
wrap.append(header, body);
mount.appendChild(wrap);
return {
getToolNames: () => Array.from(toolsBar.querySelectorAll("button")).map(b => b.dataset.tool),
isBodyVisible: () => getComputedStyle(body).display !== "none",
isWinVisible: () => getComputedStyle(wrap).display !== "none",
getWidth: () => Math.round(wrap.getBoundingClientRect().width)
};
}
window.lab = { createWindow, actionLog };
</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: Default window exposes all 4 built-in tools
page.evaluate("() => { window.w1 = window.lab.createWindow(); }")
tools_default = page.evaluate("() => window.w1.getToolNames()")
page.click('button[data-tool="panel-tool-collapse"]')
collapsed_vis = page.evaluate("() => window.w1.isBodyVisible()")
page.click('button[data-tool="panel-tool-collapse"]')
restored_vis = page.evaluate("() => window.w1.isBodyVisible()")
page.click('button[data-tool="panel-tool-max"]')
max_w = page.evaluate("() => window.w1.getWidth()")
res1 = {
"tools": tools_default,
"bodyVisibleWhenCollapsed": collapsed_vis,
"bodyVisibleWhenRestored": restored_vis,
"maximizedWidthPx": max_w,
}
exp1 = {
"tools": ["panel-tool-collapse", "panel-tool-min", "panel-tool-max", "panel-tool-close"],
"bodyVisibleWhenCollapsed": False,
"bodyVisibleWhenRestored": True,
"maximizedWidthPx": 640,
}
cases.append(dict(name="default_four_window_tools", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Disabling collapsible/minimizable/maximizable leaves only closable
page.evaluate("""() => {
window.w2 = window.lab.createWindow({ collapsible: false, minimizable: false, maximizable: false });
}""")
tools_only_close = page.evaluate("() => window.w2.getToolNames()")
page.click('button[data-tool="panel-tool-close"]')
visible_after_close = page.evaluate("() => window.w2.isWinVisible()")
res2 = {"tools": tools_only_close, "visibleAfterClose": visible_after_close}
exp2 = {"tools": ["panel-tool-close"], "visibleAfterClose": False}
cases.append(dict(name="closable_only_window_configuration", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Custom tools array with icon-add & icon-remove handlers
page.evaluate("""() => {
window.lab.actionLog.length = 0;
window.w3 = window.lab.createWindow({
collapsible: false,
minimizable: false,
maximizable: false,
tools: [
{ iconCls: "icon-add", handler: () => window.lab.actionLog.push("add") },
{ iconCls: "icon-remove", handler: () => window.lab.actionLog.push("remove") }
]
});
}""")
tools_custom = page.evaluate("() => window.w3.getToolNames()")
page.click('button[data-tool="icon-add"]')
page.click('button[data-tool="icon-remove"]')
actions = page.evaluate("() => window.lab.actionLog")
res3 = {"tools": tools_custom, "firedHandlers": actions}
exp3 = {
"tools": ["icon-add", "icon-remove", "panel-tool-close"],
"firedHandlers": ["add", "remove"],
}
cases.append(dict(name="custom_tools_array_handlers", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
将 collapsible、minimizable、maximizable 均设为 false(closable 保持默认 true),并通过 tools: [{iconCls:'icon-reload', handler: ...}] 追加自定义刷新工具。