skathan
skathan

Reputation: 689

Handling Tableau Date Filters in Javascript API?

I've been working in the JavaScript API Tutorial that tableau provides.

The filter option works well on the Region filter, but for some reason stalls out on the "Year" filter. Here's my code:

var viz;
var workbook;

// Initialize the javascript controls for Tableau
window.onload=function() {
    var vizDiv = document.getElementById('viz');
    var vizURL = 'http://public.tableau.com/views/WorldIndicators/GDPpercapita';
    var options = {
        width: '600px',
        height: '540px',
        hideToolbar: true,
        hideTabs: true,
        onFirstInteractive: function() {
        document.getElementById('sheetName').innerHTML=viz.getWorkbook().getActiveSheet().getName();
        }
    }
    viz = new tableauSoftware.Viz(vizDiv,vizURL, options);
};

// Filter By Years
function addValuesToFilter() {
    activeSheet = viz.getWorkbook().getActiveSheet();
    activeSheet.applyFilterAsync(
    "Year",
    ["2002", "2003"],
    tableau.FilterUpdateType.ADD);
}

// WORKING Filter Region
function addValuesToFilter() {
    activeSheet = viz.getWorkbook().getActiveSheet();
    activeSheet.applyFilterAsync(
    "Region",
    ["Europe", "The Americas"],
    tableau.FilterUpdateType.ADD);
}

So I figure there must be some kid of 'getProperty' function to dump out the column names or something, but the API is pretty scant, and I'm not that great of a programmer. I've taken a look into the .twb file's xml but it's mostly mumbo-jumbo and it's hard to make head or tail of what's going on in there.
Where am I going wrong here? Is there a place I can get a list of filter names, or are date filters perhaps handled differently in Tableau?

Upvotes: 0

Views: 1016

Answers (2)

user8513251
user8513251

Reputation: 11

The list of filter names helped me.

Once I had the filter name (ex. "YEAR(Date (year))") the following worked.

function addYearToFilter() {
  activeSheet.applyFilterAsync(
      "YEAR(Date (year))",
      ["2008","2009"],
     tableau.FilterUpdateType.ADD);
}

Upvotes: 1

Kevinth Heaven
Kevinth Heaven

Reputation: 36

I don't know if you ever figured this out, but I got here looking for something else, so maybe this will help someone.

Here's how I list out the filter names.

activeSheet.getFiltersAsync().then(function(filters) {
    for (var x=0; x<filters.length; x++) {
        console.log(filters[x].getFieldName());
    }
});

If the filter is indeed named "Year", try a more descriptive name. Could it be conflicting with another property in Tableau? Date filters do get treated a bit differently in some circumstances (like if this filter is dependent on a another filter applied earlier).

Upvotes: 1

Related Questions