Sort, Page and Total a Grid Without a Query
A read-only grid shows invoices that are already loaded into a local variable: maps with id, customer and amount. The grid sorts and pages them itself, and a footer shows totals.
Given the rows, a sort field, a direction, a start index and a batch size, return one map:
data: the rows of the requested page, after sorting all rowstotalCount: the number of rows in all pagespageTotal: the sum ofamounton this pagegrandTotal: the sum ofamountacross all rowshasNext: true when there is a page after this one
Every amount is a Decimal, written 80.0 rather than 80. In Appian, todatasubset() will not sort on a field that mixes Integer and Decimal values; it stops with "The sort field must be a consistent type".
Why it comes up: a grid built on an integration response or a local list has to sort and page in memory. The footer is where people slip: they sum the current page and show it as the total.
ri! Rule Inputs
ri!rowsList of Map{
a!map(id: 1, customer: "Zeta", amount: 120.5),
a!map(id: 2, customer: "Acme", amount: 80.0),
a!map(id: 3, customer: "Mono", amount: 300.0),
a!map(id: 4, customer: "Beta", amount: 80.0),
a!map(id: 5, customer: "Kilo", amount: 45.0)
}ri!sortFieldText"amount"
ri!ascendingBooleanfalse
ri!startIndexInteger1
ri!batchSizeInteger2
Example 1: Largest amounts first
{
a!map(id: 1, customer: "Zeta", amount: 120.5),
a!map(id: 2, customer: "Acme", amount: 80.0),
a!map(id: 3, customer: "Mono", amount: 300.0),
a!map(id: 4, customer: "Beta", amount: 80.0),
a!map(id: 5, customer: "Kilo", amount: 45.0)
}"amount"
false
1
2
a!map(data: {a!map(id: 3, customer: "Mono", amount: 300.0), a!map(id: 1, customer: "Zeta", amount: 120.5)}, totalCount: 5, pageTotal: 420.5, grandTotal: 625.5, hasNext: true)Example 2: Second page by customer
{
a!map(id: 1, customer: "Zeta", amount: 120.5),
a!map(id: 2, customer: "Acme", amount: 80.0),
a!map(id: 3, customer: "Mono", amount: 300.0),
a!map(id: 4, customer: "Beta", amount: 80.0),
a!map(id: 5, customer: "Kilo", amount: 45.0)
}"customer"
true
3
2
a!map(data: {a!map(id: 5, customer: "Kilo", amount: 45.0), a!map(id: 3, customer: "Mono", amount: 300.0)}, totalCount: 5, pageTotal: 345, grandTotal: 625.5, hasNext: true)Example 3: Short last page
{
a!map(id: 1, customer: "Zeta", amount: 120.5),
a!map(id: 2, customer: "Acme", amount: 80.0),
a!map(id: 3, customer: "Mono", amount: 300.0),
a!map(id: 4, customer: "Beta", amount: 80.0),
a!map(id: 5, customer: "Kilo", amount: 45.0)
}"customer"
true
5
2
a!map(data: {a!map(id: 1, customer: "Zeta", amount: 120.5)}, totalCount: 5, pageTotal: 120.5, grandTotal: 625.5, hasNext: false)Example 4: Everything on one page
{
a!map(id: 1, customer: "Zeta", amount: 120.5),
a!map(id: 2, customer: "Acme", amount: 80.0),
a!map(id: 3, customer: "Mono", amount: 300.0),
a!map(id: 4, customer: "Beta", amount: 80.0),
a!map(id: 5, customer: "Kilo", amount: 45.0)
}"id"
false
1
10
a!map(data: {a!map(id: 5, customer: "Kilo", amount: 45.0), a!map(id: 4, customer: "Beta", amount: 80.0), a!map(id: 3, customer: "Mono", amount: 300.0), a!map(id: 2, customer: "Acme", amount: 80.0), a!map(id: 1, customer: "Zeta", amount: 120.5)}, totalCount: 5, pageTotal: 625.5, grandTotal: 625.5, hasNext: false)Appian Expression Tips
- • Use
ri!variableNamefor rule inputs - • Use
a!localVariables(local!x: …, body)for locals - • Use
a!forEach(items: …, expression: fv!item)to iterate - • Use
where()/reject(fn!isnull, list)to filter