简体   繁体   English

Tableau JS Api筛选器不接受变量

[英]Tableau JS Api Filter does not accept variable

I am writing some code, that is supposed to automatically filter a Tableau workbook, based on the values of an array. 我正在编写一些代码,该代码应根据数组的值自动过滤Tableau工作簿。

The function is called through: 该函数通过以下方式调用:

showOnly2('Disponent',filterString);

'Disponent' is hard coded, filterString is an array of this format: 'Disponent'是硬编码的,filterString是这种格式的数组:

['201','202','203','204','205','206','207','208','209']

In the following block of code: 在下面的代码块中:

function showOnly2(filterName, values) {
        document.getElementById("header").innerHTML = values;
    sheet = viz.getWorkbook().getActiveSheet();
    if(sheet.getSheetType() === 'worksheet') {
        sheet.applyFilterAsync(filterName, values, 'REPLACE');
    } else {
        worksheetArray = sheet.getWorksheets();
        for(var i = 0; i < worksheetArray.length; i++) {
            worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE');  
        }
    }
}

The worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE'); worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE'); part does not work when called with the variable "values". 用变量“值”调用时,部分无效。

However, if I hard-code the value of the array into the formula: 但是,如果我将数组的值硬编码到公式中:

worksheetArray[i].applyFilterAsync(filterName, ['201','202','203','204','205','206','207','208','209'] , 'REPLACE');

it works like a charm...Unfortunately I cannot hard-code it, as the numbers in the array (the filter values I want to set), change over time. 它的工作原理就像一种魅力……不幸的是,我无法对其进行硬编码,因为数组中的数字(我要设置的过滤器值)会随着时间而变化。

Does anybody have an idea, why the variable version does not work? 有人知道为什么可变版本不起作用吗?

(I also checked for scope issues, by implementing document.getElementById("header").innerHTML = values; within the function, just to doublecheck that the array value is read properly. (我还通过在函数中实现document.getElementById("header").innerHTML = values;检查范围问题,只是要仔细检查一下数组值是否已正确读取。

Any pointers are much appreciated. 非常感谢任何指针。

The answer is straight forward. 答案很简单。

The API expectes an array-object not a string (although when displayed as a string both look the same). API期望数组对象不是字符串(尽管当显示为字符串时,两者看起来相同)。

Hence, the following code worked flawlessly: 因此,以下代码可以完美地工作:

var filterArray = [];
for(var i = 201; i < 300; i++) {
    filterArray.push(i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM