【发布时间】:2021-06-14 00:34:02
【问题描述】:
在用户编辑表格后,我无法从表格的前三行获取值。
表格应通过匹配列“sp1、sp2、… spn”(选定的试点)和列“ro”(官方结果)来为列“pts1、pts2、… ptsn”(得分)奖励积分。
1.- 如果“spn”值与同一行中的“ro”匹配,用户从“po”列获得积分(飞行员位置的官方积分)。
2.- 否则,如果“spn”值在讲台上(前 3 行),那么用户对于任何匹配且不满足第一个条件的值只能获得 2 分。
3.- 其他用户得 0 分。
用户可以编辑“ro”列进行预测。
问题: 如果用户在页面上编辑表格,则 mutator 将处理第一个和第三个条件,但不会处理第二个条件,因此它永远不会奖励 2 分。控制台日志导致 var podium 上未定义。如果加载了“ro”值,则不会发生这种情况,所以我猜逻辑有效。
四处寻找,无法弄清楚这一点,我会错过什么?
这是我的示例代码:
var tabledata = [
{id:1, ro:'', op: 25, sp1:"HAM", pts1:''},
{id:2, ro:'', op: 18, sp1:"VER", pts1:''},
{id:3, ro:'', op: 15, sp1:"BOT", pts1:''},
{id:4, ro:'', op: 12, sp1:"PER", pts1:''},
{id:5, ro:'', op: 10, sp1:"RIC", pts1:''},
{id:6, ro:'', op: 8, sp1:"ALO", pts1:''} // table is only 10 rows first 10 pilots
];
var table = new Tabulator("#pronosticos", {
height:"310px",
dataTree: true,
reactiveData:true,
data:tabledata,
dataChanged:function(data){
document.getElementById("bt-puntaje").style.display="inline-block";
},
cellHozAlign:"center",
layout:"fitColumns",
columns:[
{title:"POS", field: "pos", formatter:"rownum", width:40, frozen:true},
{title:"RES PRE", field:"ro", width:70, frozen:true, editable:true, editor:'autocomplete',
editorParams: {
allowEmpty:true,
showListOnEmpty:true,
values:["ALO", "BOT", "GAS", "GIO", "HAM","LAT","LEC","MAZ","NOR", "OCO", "PER", "RAI", "RIC", "RUS", "SAI", "MCH", "STR", "TSU", "VET", "VER"] // list of pilots to select
},
{title:"PTS", field:"op", width:40, frozen:true, visible:false},
// ***************************************** sp1 *****************************************
{title:"PN", headerTooltip: "Players Name",
columns:[
{title:"SEL", field:"sp1", width:50},
{title:"PTS", field:"pts1", bottomCalc:"sum", width:10,
mutator:function(value, data){
var selected = data.sp1;
var winner = data.ro;
var podium = [tabledata[0].ro, tabledata[1].ro, tabledata[2].ro]; // Error Undefine if table edited
if(selected === ""){
return
} else {
if(podium.length > 0){
if(selected == winner){
return data.op; // This part works ok
} else {
if(podium.includes(selected) && data.id < 4){
return 2; // this part does not work
} else {
return 0;}}}}}
]
},
],
});
$("#bt-puntaje").click(function () {
table.updateData(tabledata);
});
【问题讨论】:
标签: javascript tabulator