gpt4 book ai didi

javascript - Tabulator JS 中的级联下拉列

转载 作者:行者123 更新时间:2023-12-05 06:55:24 25 4
gpt4 key购买 nike

我是 Tabulator JS 库的新手,我偶然发现了一个问题。我有 2 列,我试图找到一种方法来在每一行中实现级联下拉菜单。第二列值选项取决于第一列值选择。有什么简单的方法可以做到这一点?

预期结果:

  1. 用户在第一列中选​​择颜色,然后他应该会在该行的第二列下拉列表中看到蓝色和黄色
  2. 用户在第一列中选​​择样式,然后他应该会在该行的第二列下拉列表中看到S1 & S2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>

<script>
$(function () {
var tabledata = [{ Need: "1",Surface:"" }];
var NeedOptionSet = {};
NeedOptionSet["1"] = "Color";
NeedOptionSet["2"] = "Style";
var SurfaceOptionSet_Color = {};
SurfaceOptionSet_Color["1"] = "Blue";
SurfaceOptionSet_Color["2"] = "Yellow";
var SurfaceOptionSet_Style = {};
SurfaceOptionSet_Style["1"] = "Bold";
SurfaceOptionSet_Style["2"] = "Underline";
var table = new Tabulator("#example-table", {
height: 400, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
data: tabledata, //assign data to table
layout: "fitColumns", //fit columns to width of table (optional)
pagination: "local",
paginationSize: 15,
paginationSizeSelector: [15, 25, 35],
addRowPos: "top",
columns: [ //Define Table Columns
{
title: "Need", field: "Need", editor: "autocomplete", editorParams: {
showListOnEmpty: true,
freetext: true,
values: NeedOptionSet
}, formatter: 'lookup', formatterParams: NeedOptionSet

},
{
title: "Surface", field: "Surface", editor: "autocomplete", editorParams: {
showListOnEmpty: true,
freetext: true,
values: SurfaceOptionSet_Color
}, formatter: 'lookup', formatterParams: SurfaceOptionSet_Color
}
]
});

});


</script>

</head>
<body>

<div id="example-table"></div>

</body>
</html>

最佳答案

Tabulator 有一个内置的上下文菜单系统,用于调出级联菜单。

如果您希望它们出现在每一行上,那么您应该使用 rowContextMenu 选项。

var table = new Tabulator("#example-table", {
rowContextMenu:[
{
label:"Delete Row",
action:function(e, row){
row.delete();
}
},
{
label:"Sub Menu" //sub menu
menu:[
{
label:"Do Something"
action:function(e, column){
//do something
}
},
]
},
});

如果您希望更改每个菜单的值,则可以将回调传递给每次加载菜单时调用的 rowContextMenu 属性,它应该返回菜单数组,如上所示:

var table = new Tabulator("#example-table", {
rowContextMenu: function(component, e){
//component - column/cell/row component that triggered the menu
//e - click event object

var menu = [];

if(!component.getData().approved){
menu.push({
label:"Approve User",
action:function(e, column){
component.update({"approved":true});
}
})
}else{
menu.push({
label:"Unapprove User",
action:function(e, column){
component.update({"approved":false});
}
})
}

return menu;
}
});

完整的详细信息可以在 Context Menu Documentation 中找到

关于javascript - Tabulator JS 中的级联下拉列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65364295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com