Conditionally Hide a Column in a Grid

See SAIL Recipes for information about how to work through recipes and adapt them to your application.

Goal

Conditionally hide a column in a read-only paging grid when all data for that column is a specific value.

In this case, only display the "Active?" column if there is at least one inactive employee.

Expression

=load(
  local!data: {
    {id: 1, name: "John Smith",      department: "Engineering",     active: true},
    {id: 2, name: "Michael Johnson", department: "Finance",         active: true},
    {id: 3, name: "Mary Reed",       department: "Engineering",     active: true},
    {id: 4, name: "Angela Cooper",   department: "Sales",           active: true},
    {id: 5, name: "Elizabeth Ward",  department: "Sales",           active: true},
    {id: 6, name: "Daniel Lewis",    department: "Human Resources", active: true}
  },
  /* batchSize is 3 to show more than 1 page of data in this recipe. Increase it as needed. */
  local!pagingInfo: a!pagingInfo(startIndex: 1, batchSize: 3),
  with(
    local!datasubset: todatasubset(local!data, local!pagingInfo),
    a!gridField(
      label: "SAIL Example: Display Data in a Read-Only Paging Grid",
      totalCount: local!datasubset.totalCount,
      columns: {
        a!gridTextColumn(
          label: "ID",
          field: "id",
          data: index(local!datasubset.data, "id", {}),
          alignment: "RIGHT"
        ),
        a!gridTextColumn(
          label: "Name",
          field: "name",
          data: index(local!datasubset.data, "name", {})
        ),
        a!gridTextColumn(
          label: "Department",
          field: "department",
          data: index(local!datasubset.data, "department", {})
        ),
        if(
          and(local!data.active),
          {},
          a!gridTextColumn(
            label: "Active?",
            field: "active",
            data: if(index(local!datasubset.data, "active", {}), "Yes", "No")
          )
        )
      },
      value: local!pagingInfo,
      saveInto: local!pagingInfo
    )
  )
)

Test it out

  1. Change one of the active fields to false and click the Test button. Notice that the column is no longer hidden.
FEEDBACK