jEasyUI 自定义对话框
核对纯 HTML 选择器方式自定义 Dialog 左右分栏搜索工具栏与带图标的底部按钮栏,以及选择器字符串与 JS 数组两种配置方式实测。
学完这一章,你能够
- 核对纯 HTML 选择器方式自定义 Dialog 左右分栏搜索工具栏与带图标的底部按钮栏,以及选择器字符串与 JS 数组两种配置方式实测。
声明对话框与自定义复杂工具栏、按钮栏
借助 HTML 标记与 CSS 选择器绑定,无需编写初始化 JavaScript 即可为对话框装配高度定制的顶部工具栏与底部按钮栏。先定义目标 `<div id="dd" class="easyui-dialog" toolbar="#dlg-toolbar" buttons="#dlg-buttons">`。
当 `toolbar` 与 `buttons` 传入字符串选择器(如 `#dlg-toolbar`、`#dlg-buttons`)时,Dialog 会选中对应的 DIV 并将其直接移动到对话框内部的工具栏与底部按钮区。因此可以在 `#dlg-toolbar` 内用 100% 宽度布局在左侧放置 `Edit`/`Help` 扁平按钮、右侧放置搜索 `<input>` 与 `icon-search` 按钮;在 `#dlg-buttons` 左侧放置状态图标(原示例引用 `email.gif`),右侧放置 `Save` 与调用 `$('#dd').dialog('close')` 的 `Close` 按钮。
DOM 移动装配与选择器/数组双模式实测
本站离线无头浏览器实测确认:`#dlg-toolbar` 与 `#dlg-buttons` 被整体移入对话框容器内且文档中各自仅保留 `1` 份节点(无重复拷贝);在顶部搜索框输入 `easyui` 并点击搜索记录 `search:easyui`;底部保留 `email.gif` 图标引用(离线以内联 SVG 替代),点击 `Save` 记录 `save`,点击 `Close` 关闭对话框;同时对照验证以 JS 数组形式传入 `toolbar` 与 `buttons` 配置项也可正确生成 `Refresh` 与 `Confirm` 按钮并响应事件。来源示例包 `jeasyui-win-dlg2.zip` 未联网下载,已由本离线实验完整覆盖。
运行 EasyUI Dialog 自定义搜索工具栏、图文按钮栏与数组模式离线实验
展开查看收起
"""Offline Playwright Chromium lab for jQuery EasyUI Ch 50: Custom Dialog Toolbar & Buttons."""
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 Dialog Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.dialog-wrap { border: 1px solid #95b8e7; background: #e0ecff; padding: 4px; box-sizing: border-box; margin-bottom: 14px; }
.dialog-wrap.closed { display: none; }
.dialog-header { padding: 4px 6px; font-weight: bold; }
.dialog-toolbar { background: #f4f4f4; border: 1px solid #95b8e7; border-bottom: 0; padding: 4px 6px; }
.dialog-body { background: #fff; border: 1px solid #95b8e7; box-sizing: border-box; }
.dialog-button { background: #f4f4f4; border: 1px solid #95b8e7; border-top: 0; padding: 4px 6px; }
.l-btn { display: inline-block; padding: 2px 8px; border: 1px solid #bbb; background: #fff; text-decoration: none; color: #333; margin-left: 4px; }
.l-btn.plain { border-color: transparent; background: transparent; }
</style>
</head>
<body>
<div id="dd" class="easyui-dialog" title="My Dialog" style="width:400px;height:200px;padding:10px"
toolbar="#dlg-toolbar" buttons="#dlg-buttons">
Dialog Content.
</div>
<div id="dlg-toolbar">
<table cellpadding="0" cellspacing="0" style="width:100%">
<tr>
<td>
<a href="#" id="btn-edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">Edit</a>
<a href="#" id="btn-help" class="easyui-linkbutton" iconCls="icon-help" plain="true">Help</a>
</td>
<td style="text-align:right">
<input id="tb-search-input" type="text" style="width:120px" /><a href="#" id="btn-search" class="easyui-linkbutton" iconCls="icon-search" plain="true">Search</a>
</td>
</tr>
</table>
</div>
<div id="dlg-buttons">
<table cellpadding="0" cellspacing="0" style="width:100%">
<tr>
<td>
<img id="email-icon" data-src="email.gif" src="data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='16'%3E%3Crect width='20' height='16' fill='%2395b8e7'/%3E%3C/svg%3E" />
</td>
<td style="text-align:right">
<a href="#" id="btn-save" class="easyui-linkbutton" iconCls="icon-save">Save</a>
<a href="#" id="btn-close" class="easyui-linkbutton" iconCls="icon-cancel">Close</a>
</td>
</tr>
</table>
</div>
<div id="dd-array" style="padding:10px;width:360px;height:120px;">Array Dialog Content.</div>
<script>
const events = [];
const dialogs = new Map();
function mountDialog(el, opts = {}) {
const title = opts.title ?? el.getAttribute("title") ?? "";
const tbOpt = opts.toolbar ?? el.getAttribute("toolbar");
const btOpt = opts.buttons ?? el.getAttribute("buttons");
const wrap = document.createElement("div");
wrap.className = "dialog-wrap";
wrap.dataset.for = el.id;
wrap.style.width = el.style.width || "400px";
const header = document.createElement("div");
header.className = "dialog-header";
header.textContent = title;
el.parentNode.insertBefore(wrap, el);
wrap.appendChild(header);
if (typeof tbOpt === "string") {
const tbEl = document.querySelector(tbOpt);
if (tbEl) {
tbEl.classList.add("dialog-toolbar");
wrap.appendChild(tbEl);
}
} else if (Array.isArray(tbOpt)) {
const tbDiv = document.createElement("div");
tbDiv.className = "dialog-toolbar";
tbOpt.forEach(item => {
const a = document.createElement("a");
a.href = "#";
a.className = "l-btn plain";
a.textContent = item.text;
a.addEventListener("click", (e) => { e.preventDefault(); item.handler(); });
tbDiv.appendChild(a);
});
wrap.appendChild(tbDiv);
}
el.classList.add("dialog-body");
el.style.width = "100%";
wrap.appendChild(el);
if (typeof btOpt === "string") {
const btEl = document.querySelector(btOpt);
if (btEl) {
btEl.classList.add("dialog-button");
wrap.appendChild(btEl);
}
} else if (Array.isArray(btOpt)) {
const btDiv = document.createElement("div");
btDiv.className = "dialog-button";
btDiv.style.textAlign = "right";
btOpt.forEach(item => {
const a = document.createElement("a");
a.href = "#";
a.className = "l-btn";
a.textContent = item.text;
a.addEventListener("click", (e) => { e.preventDefault(); item.handler(); });
btDiv.appendChild(a);
});
wrap.appendChild(btDiv);
}
dialogs.set(el.id, {
wrap,
close() { wrap.classList.add("closed"); },
open() { wrap.classList.remove("closed"); }
});
}
document.querySelectorAll(".easyui-dialog").forEach(el => mountDialog(el));
document.querySelectorAll("a.easyui-linkbutton").forEach(a => {
a.classList.add("l-btn");
if (a.getAttribute("plain") === "true") a.classList.add("plain");
});
document.getElementById("btn-search").addEventListener("click", (e) => {
e.preventDefault();
events.push("search:" + document.getElementById("tb-search-input").value);
});
document.getElementById("btn-save").addEventListener("click", (e) => {
e.preventDefault();
events.push("save");
});
document.getElementById("btn-close").addEventListener("click", (e) => {
e.preventDefault();
dialogs.get("dd").close();
});
mountDialog(document.getElementById("dd-array"), {
title: "Array Configured Dialog",
toolbar: [{ text: "Refresh", handler: () => events.push("array:refresh") }],
buttons: [{ text: "Confirm", handler: () => events.push("array:confirm") }]
});
window.lab = { dialogs, events };
</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: Custom table-layout toolbar with Edit/Help on left and search input on right
page.fill("#tb-search-input", "easyui")
page.click("#btn-search")
res1 = page.evaluate("""() => {
const wrap = window.lab.dialogs.get("dd").wrap;
const tb = document.getElementById("dlg-toolbar");
return {
toolbarMovedInsideWrap: wrap.contains(tb),
toolbarCountInDoc: document.querySelectorAll("#dlg-toolbar").length,
leftButtons: [
document.getElementById("btn-edit").textContent,
document.getElementById("btn-help").textContent
],
events: [...window.lab.events]
};
}""")
exp1 = {
"toolbarMovedInsideWrap": True,
"toolbarCountInDoc": 1,
"leftButtons": ["Edit", "Help"],
"events": ["search:easyui"],
}
cases.append(dict(name="custom_toolbar_table_and_search_input", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Custom buttons table with left icon graphic and right Save/Close buttons
page.click("#btn-save")
page.click("#btn-close")
res2 = page.evaluate("""() => {
const wrap = window.lab.dialogs.get("dd").wrap;
const bt = document.getElementById("dlg-buttons");
return {
buttonsMovedInsideWrap: wrap.contains(bt),
leftIconRef: document.getElementById("email-icon").dataset.src,
events: [...window.lab.events],
closedAfterCloseBtn: getComputedStyle(wrap).display === "none"
};
}""")
exp2 = {
"buttonsMovedInsideWrap": True,
"leftIconRef": "email.gif",
"events": ["search:easyui", "save"],
"closedAfterCloseBtn": True,
}
cases.append(dict(name="custom_buttons_table_icon_and_close", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Both CSS selector string and JS array options work for toolbar/buttons
page.click('.dialog-wrap[data-for="dd-array"] .dialog-toolbar a')
page.click('.dialog-wrap[data-for="dd-array"] .dialog-button a')
res3 = page.evaluate("""() => ({
arrayToolbarText: document.querySelector('.dialog-wrap[data-for="dd-array"] .dialog-toolbar a').textContent,
arrayButtonText: document.querySelector('.dialog-wrap[data-for="dd-array"] .dialog-button a').textContent,
events: window.lab.events.slice(-2)
})""")
exp3 = {
"arrayToolbarText": "Refresh",
"arrayButtonText": "Confirm",
"events": ["array:refresh", "array:confirm"],
}
cases.append(dict(name="selector_string_vs_array_option_binding", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
选择器方式允许在外部 DIV 中自由编排输入框、下拉框、图标或左右分栏等任意复杂 HTML 结构,且无需编写额外的 JavaScript 初始化代码。