Automatically Applying Filters to Some Categories in a Banner
Jump to navigation
Jump to search
Q Technical Reference
Q Technical Reference
Q Technical Reference > Creating And Modifying Tables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Filters
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript > Table JavaScript and Plot JavaScript Examples Library
This example can be run using C:\Program Files\Q\Examples]Phone 1.Q (this file may be in a different place on your computer). Note in this example that:
- The filter variable that is automatically applied is not tagged as a filter and is not available in the Filter drop-down menu.
- This example only changes the values in the cells of the table. It will not update any statistics selected by Statistics - Below.
This script will cause significance results to be displayed incorrectly. Please read Caveats for Table JavaScript before using this script.
// Finds any columns in the span 'Gender' and applies the filter with variable name 'Ageunder35' to them.
var spans = table.columnSpans;
var columns = null;
for (var i=0; i<spans.length; i++)
if (spans[i].label == 'Gender')
columns = spans[i].indices;
var filtered_table = calculateTable(table.blue, table.brown,['Ageunder35'], '!UseQWeight');
var stats = table.statistics; //gets the names of the statistics shown on the table
if (columns != null)
for (var i = 0; i < columns.length; i++) {
var column = columns[i];
// Copy the weighted statistics for the column to the current table .
for (var stat = 0; stat < stats.length; stat++) {
var filtered_values = filtered_table.get(stats[stat]);
var main_values = table.get(stats[stat]);
for (var row = 0; row < table.numberRows; row++)
main_values[row][column] = filtered_values[row][column];
table.set(stats[stat], main_values);// Set the altered values to the main table.
}
}
See also
- Table JavaScript and Plot JavaScript for an explanation of how to run this code.
- Table JavaScript and Plot JavaScript Reference for technical information.
- Table JavaScript and Plot JavaScript Examples Library for other examples.
- JavaScript for information about the JavaScript programming language.
- QScript for tools for automating projects using JavaScript.
- JavaScript Variables for detail on how to create new variables in the Variables and Questions tab using JavaScript.
Q Technical Reference
Q Technical Reference
Q Technical Reference > Creating And Modifying Tables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Filters
Q Technical Reference > Updating and Automation > JavaScript > Table JavaScript and Plot JavaScript > Table JavaScript and Plot JavaScript Examples Library