如何在 GridJs 中自定义菜单和工具栏
Contents
[
Hide
]
关于自定义菜单和工具栏按钮
我们没有直接提供有用的 API。 但是我们可以根据 DOM 结构编写一些 JS 函数来实现它。
自定义菜单栏
例如:只保留文件菜单,假设GridJs的div ID为"gridjs-divid"
//get menubar parent dom
const menubar=document.querySelector("#gridjs-divid > div > div:nth-child(1) > div > div.x-spreadsheet-banner-info-s > div.x-spreadsheet-toolbar.x-spreadsheet-menubar");
var childs = menubar.childNodes;
for (var i = childs.length - 1; i >= 0; i--)
{
// keep File menu only
if(childs[i].childNodes[0].childNodes[0].textContent!=="File")
{
menubar.removeChild(childs[i]);
}
}
调用此函数后

自定义菜单栏中的项
例如:只保留“另存为XLSX”菜单项,假设GridJs的div ID为"gridjs-divid"
//get menubar parent dom
const menubar=document.querySelector("#gridjs-divid > div > div:nth-child(1) > div > div.x-spreadsheet-banner-info-s > div.x-spreadsheet-toolbar.x-spreadsheet-menubar");
var childs = menubar.childNodes;
// keep the first one ->File menu only
for (var i = childs.length - 1; i >= 0; i--)
{ //find the File menu
if(childs[i].childNodes[0].childNodes[0].textContent==="File")
{
var dropdownparent = childs[i].childNodes[0].childNodes[1];
var menuitems = dropdownparent.childNodes;
for (var ii = menuitems.length - 1; ii >=0; ii--)
{
//remove other menu item that is not "Download As XLSX"
if (menuitems[ii].textContent !== 'Download As XLSX')
{
dropdownparent.removeChild(menuitems[ii]);
}
}
}
}
调用此函数后

自定义工具栏项
例如:只保留缩放按钮,假设GridJs的div ID为"gridjs-divid"
//get toolbar parent dom
const toolbar=document.querySelector("#gridjs-divid > div > div.x-spreadsheet-toolbar > div.x-spreadsheet-toolbar-btns");
var childs = toolbar.childNodes;
for (var i = childs.length - 1; i >= 0; i--)
{
// keep File menu only
if(childs[i].getAttribute("data-tooltip")!=="Zoom")
{
toolbar.removeChild(childs[i]);
}
}
调用此函数后

自定义工具栏悬停效果
打开浏览器检查窗口,选择工具栏按钮,

然后我们可以找到与该按钮相关的CSS键是:冻结

添加以下CSS规则:
.x-spreadsheet-toolbar .x-spreadsheet-toolbar-btn:hover .x-spreadsheet-icon-img.freeze,
.x-spreadsheet-toolbar .x-spreadsheet-toolbar-btn.active .x-spreadsheet-icon-img.freeze {
background: rgba(4, 71, 33, 0.08);
filter: brightness(0) saturate(100%) invert(27%) sepia(51%) saturate(2878%) hue-rotate(346deg) brightness(104%) contrast(97%);
}
结果将是:

底部栏中的自定义项目
概览
底部栏包含两个互动按钮:
- 添加工作表按钮(
add类) - 创建新工作表 - 选择工作表按钮(
ellipsis类) - 管理工作表选择
DOM访问
你可以使用以下方法访问这些元素:
// Add Worksheet Button
const addButton = document.querySelector('.x-spreadsheet-icon-img.add');
// Select Worksheet Button
const selectButton = document.querySelector('.x-spreadsheet-icon-img.ellipsis');
自定义示例
- 隐藏按钮 要从DOM中移除按钮:
// Hide Add Worksheet Button
const addButton = document.querySelector('.x-spreadsheet-icon-img.add');
addButton.parentElement.removeChild(addButton);
// Hide Select Worksheet Button
const selectButton = document.querySelector('.x-spreadsheet-icon-img.ellipsis');
selectButton.parentElement.removeChild(selectButton);
- 更换图标 你可以使用外部SVG文件或内联SVG数据替换图标。
const addButton = document.querySelector('.x-spreadsheet-icon-img.add');
addButton.style.backgroundImage = "url('https://example.com/fish-icon.svg')";
// Adjust size and position
addButton.style.width = '18px';
addButton.style.height = '18px';
addButton.style.left = '0';
addButton.style.top = '0';
- 更改按钮行为 你可以修改点击事件以自定义功能:
const addButton = document.querySelector('.x-spreadsheet-icon-img.add');
addButton.addEventListener('click', function() {
// Custom action here
console.log('Custom add worksheet action');
});