In this article
This script is used to dynamically create a filter expression. You can build your filter expression dynamically based on parameter values or a user role. Expression syntax is the same as for regular filter expressions.
To enter the script editor select Filter tree > Insert a filter script > Script.
Example – Setting a filter expression:
filter.Expression = "gender = \"male\"";Example – Setting a static filter expression based on user role:
if(user.HasRole("Test_Level_A"))
//Checks the user role
{
filter.Expression = 'Q4 = "1" ' ;
//Sets the expression. This expression is the same as when using a filter expression. It must be placed in single ' '.
}Another common scenario is pulling the user role set to create a filter expression. For example, user roles are set "1", "2" and "3". These roles all coincide with a question with the same numeric precode setup. In the filter expression we can place:
var rls : String[] = user.Roles;
for (var i = 0; i < rls.Length; i++){
if (user.HasRole(rls[i])){
filter.Expression = 'plist = "'+ rls[i]+'"';
break;
}
}
In the event we want to pull out the selected answer in a page, a text component with the following script can be used :
var rls : String[] = user.Roles;
text.Output.Append(rls);
Date filters that allow you to include or exclude respondents based on the interview start date can be built via scripts:
/----------------FilterExpression for dates-----------//
var dato : Date = new Date();
filter.Expression = 'interview_start > TODATE("2014-01-03")';
//or A SCRIPT TO REMOVE PEOPLE FROM TODAYS DATE
var dato : Date = new Date();
var da : String;
var Month : String = (dato.getMonth() +1);
if(Month.Length ==1){
var monthnew : String = "0" + (dato.getMonth() + 1);
}
else if(Month.Length ==2){
monthnew = (dato.getMonth() + 1);
}
var day : String = dato.getDate();
if(day.Length ==1){
var daynew : String = "0" + dato.getDate();
}
else if(day.Length ==2){
daynew = dato.getDate();
}
da = dato.getYear() + "-" + monthnew + "-" + daynew;
filter.Expression = 'interview_start < TODATE("' + da + '")' ;
//text.Output.Append(da); //Text output expression
A dynamic filter would involve a parameter or similar input device allowing the user to control the events on a page.
In the example below, a String parameter with several values is used. A filter expression which is based on the selected value is applied to the table.
If the parameter is being applied directly to the table, then the dynamic filter is not necessary, it is only to be used when you have multiple filters to be triggered from one selection.
Example - Setting a dynamic filter expression based on selected parameter value:
var com = state.Parameters.GetString("p_color"); //Connect the parameter to the variable
//run the different precodes to set the filter expression
if(com == "green")
{
filter.Expression = 'q3 = "1"';
}
else if(com == "red")
{
filter.Expression = 'q3 = "2"';
}
else if(com == "blue")
{
filter.Expression = 'q3 = "1" AND IN(q4, "3")';
}
else if(com == "yellow")
{
filter.Expression = 'q3 = "2" AND IN(q4, "3")';
}
else
{
filter.Expression = ''
}When no parameter value is selected, then no filters are applied to the table - show me.
Figure 1 - The chart palette is not selected, default colors are used
When the palette is selected, the chart categories change their colors - show me.
Figure 2 - The Rainbow palette is selected