QScript FilterTerm

From Q
Jump to navigation Jump to search

FilterTerm

Represents a single filter condition (leaf node in a filter tree). A FilterTerm object tests one Question or Variable object against specific values using an operator.

Examples of FilterTerms:

  • "Gender is Male" (Question with value 1)
  • "Age >= 18" (Question with numeric comparison)
  • "Comments contains 'excellent'" (Question with text matching)
  • "Satisfaction_Service >= 4" (Variable from a grid question)

FilterTerm objects are created using DataFile.createFilterTerm and can be:

  • Used directly in DataFile.newFilterQuestion for simple filters
  • Combined with LogicalAnd objects / LogicalOr objects for complex filtering logic

The values for filtering can come from:

  • Direct values passed when creating the term (static filtering)
  • A Control object (dynamic filtering based on user selection in the UI)

children

Gets an array of child filter nodes.

For FilterTerm objects (leaf nodes), this always returns an empty array.

For LogicalAnd objects and LogicalOr objects, returns the child conditions being combined.

controlGuid

The GUID of the associated control (if control-based).

equals(obj)

Whether two objects are the same.
objThe object to compare against.
Returns:true or false
Example:
data_file.getQuestionByName('Q2').equals(data_file.questions()[0])

input

The input object (Question or Variable) that this filter term operates on.

For single-variable questions (Pick One, Number, Text, Date), this may be either a Question or Variable object.

For Pick Any questions, this will be a Question object.

For grid/multi questions (Pick One - Multi, Number - Multi, Pick Any - Grid, Number - Grid, Pick Any - Compact, Text - Multi), this will be a Variable object.

See DataFile.createFilterTerm for more details on input types.

isControlBased

Whether this filter term is based on a control (dynamic values).

label

Optional custom label for this filter node. If provided, this label may be used in the UI instead of the auto-generated description. Useful for providing human-readable names for complex filter conditions.

negated

Whether this filter node is negated (NOT logic applied). When true, the result of this node is inverted:
  • For FilterTerm object: matches become non-matches and vice versa
  • For LogicalAnd object: becomes NOT(A AND B AND ...) which is equivalent to (NOT A OR NOT B OR ...)
  • For LogicalOr object: becomes NOT(A OR B OR ...) which is equivalent to (NOT A AND NOT B AND ...)

operator_string

The operator used for filtering. Valid operators depend on the input type:

Pick Any questions: "Any of", "All of", "Only/exactly", "None of"

Categorical (Pick One questions, or categorical variables from grids): "Any of", "None of"

Numeric: "Equals", "Not equals", "Greater than", "Greater than or equals", "Less than", "Less than or equals"

Text: "Equals", "Not equals", "Contains text", "Contains word", "Starts with", "Ends with", "Regular expression"

Date: "Within last", "Within last period", "Equals", "Not equals", "Greater than", "Greater than or equals", "Less than", "Less than or equals"

type

Returns the name we use to refer to this class in the documentation.

values

The values used in the filter operation. The type and meaning of values depends on the input type:
  • Categorical single-response (Pick One): Array of numeric category codes
  • Pick Any questions: Array of Variable objects representing selected categories
  • Numeric: Array of numbers to compare against
  • Text: Array of strings to match
  • Date: Array of DateTime objects

For control-based filters, this returns the current values from the control.