Skip to main content
pranavk513898
February 25, 2022
Question

Custom filters on CDT that is not associated with any table.

  • February 25, 2022
  • 3 replies
  • 0 views

Hello community,

I am trying to make UI which has a read only grid which is populated based on data from integration, we passed a cdt as an input to this grid, now I wanted to put filters on that data using drop downs and date filters (start date and end date), I did came up filtering the cdt using wherecontains() function and foreach() loop and got different index for them. but I couldn't make them work like record filters. how do I group them ?

some code below for understanding.

Thanks.

a!localVariables(
      local!selection,
      local!responseData: rule!PNK_DataForGrid(),
      local!actualData: local!responseData,
      local!customerAddress: union(
        local!actualData.customerName,
        local!actualData.customerName
      ),
      local!customerNameSelection,
      local!ftaNames: union(
        local!actualData.customerAddress,
        local!actualData.customerAddress
      ),
      local!customerAddressselection,
      local!startDate,
     
      local!endDate,
     
      local!idx1: wherecontains(
        touniformstring(local!customerNameSelection),
        touniformstring(local!actualData.customerName)
      ),
      local!idx2: wherecontains(
        touniformstring(local!customerAddressselection),
        touniformstring(local!actualData.customerAddress)
      ),
      local!idx3: fn!reject(
        fn!isnull,
        a!forEach(
          items: local!actualData,
          expression: if(
            or(
              rule!fn!IsNull(local!startDate),
              rule!fn!IsNull(local!endDate),
              local!startDate > local!endDate
            ),
            null(),
            if(
              and(
                local!startDate - 1 < fv!item.periodStart,
                fv!item.periodEnd < local!endDate + 1
              ),
              fv!index,
              null
            )
          )
        )
      ),
      local!idx: union(
        local!idx1,
        local!idx2,
        tointeger(local!idx3)
      ),
      local!filteredData: a!forEach(
        items: local!idx,
        expression: { local!actualData[fv!item] }
      ),
      {
   
        a!sectionLayout(
          label: "Customer DATA",
          contents: {
           a!columnsLayout(
              columns: {
                a!columnLayout(
                  contents: {
                    rule!PNK_DropDownForCustomerSelection(
                      customerName: local!customerName,

                      customerNameSelection: local!customerNameSelection,
                      customerAddress: local!customerAddress,
                      customerAddressSelection: local!customerAddressSelection
                    )
                  }
                ),
                a!columnLayout(
                  contents: {
                    rule!PNK_DateComponentFilters(
                      requestStartDate: local!startDate,
                      requestEndDate: local!endDate
                    )
                  }
                )
              }
            ),
            a!gridField(
              label: "",
              labelPosition: "ABOVE",
              data: if(
                fn!IsNull(local!idx),
                local!actualData,
                local!filteredData
              ),
              columns: {
               
                a!gridColumn(
                  label: "Name",
                  value: fv!row.customerName
                ),
                a!gridColumn(
                  label: "Address",
                  value: fv!row.customerAddress
                ),
                a!gridColumn(
                  label: "Start Date",
                  value: fv!row.periodstart
                ),
                a!gridColumn(
                  label: "End Date",
                  value: fv!row.periodend
                )
              },
             
              selectable: true,
              selectionStyle: "CHECKBOX",
              selectionValue: local!selection,
              selectionSaveInto: local!selection,
              selectionRequired: true,
              validations: {}
            )
          }
        ),
        
      }
    )

    3 replies

    andrewh0007
    February 25, 2022

    Hi PK, see if this tutorial can help you.

    docs.appian.com/.../recipe-filter-the-data-in-a-grid.html

    pranavk513898
    February 25, 2022

    No unfortunately, my grid is not backed by record. so cannot use this.

    peter.lewis
    Employee
    February 25, 2022

    I think you're overcomplicating the logic for your filters. In general the easiest way to filter data from a variable like this is to use a!forEach() and use logic for each item to determine if you should return the item or an empty list. Then, any items that return an empty list are excluded from the result.

    Here's an example for creating a dropdown based on "Type" that filters down the result set:

    a!localVariables(
      local!typeSelection,
      local!data: {
        a!map(id: 1, name: "Name 1", type: "Active"),
        a!map(id: 2, name: "Name 2", type: "Inactive"),
        a!map(id: 3, name: "Name 3", type: "Active")
      },
      local!uniqueTypes: union(
        local!data.type,
        touniformstring({})
      ),
      local!filteredData: if(
        a!isNullOrEmpty(local!typeSelection),
        local!data,
        a!forEach(
          items: local!data,
          expression: if(
            fv!item.type = local!typeSelection,
            fv!item,
            {}
          )
        )
      ),
      {
        a!dropdownField(
          label: "Type",
          choiceValues: local!uniqueTypes,
          choiceLabels: local!uniqueTypes,
          value: local!typeSelection,
          saveInto: local!typeSelection,
          placeholder: "Select a type..."
        ),
        a!gridField(
          label: "Data",
          data: local!filteredData,
          columns: {
            a!gridColumn(
              label: "ID",
              value: fv!row.id
            ),
            a!gridColumn(
              label: "Name",
              value: fv!row.name
            ),
            a!gridColumn(
              label: "Type",
              value: fv!row.type
            )
          }
        )
      },
    )