Prism · Real world

Search, sort, paginate

What a list view does, as one config: keep what matches the search, sort it, and cut out one page.

The source
{
  products: [
    { name: 'Apple iPhone', price: 999 },
    { name: 'Samsung Galaxy', price: 899 },
    { name: 'Apple Watch', price: 399 },
    { name: 'Apple iPad', price: 599 },
    { name: 'Google Pixel', price: 699 },
    { name: 'Apple MacBook', price: 1299 },
  ],
  pageSize: 2,
  pageIndex: 1,
}
The config
{
  $with: {
    let: {
      filtered: {
        $sortBy: {
          over: {
            $filter: {
              over: { $ref: '$.products' },
              as: 'p',
              when: {
                $contains: {
                  value: { $get: { from: { $var: 'p' }, path: ['name'] } },
                  search: { $const: 'Apple' },
                },
              },
            },
          },
          as: 'p',
          by: { $get: { from: { $var: 'p' }, path: ['price'] } },
          dir: 'asc',
        },
      },
    },
    value: {
      total: { $count: { over: { $var: 'filtered' } } },
      page: {
        $take: {
          from: { $drop: { from: { $var: 'filtered' }, count: 2 } },
          count: 2,
        },
      },
    },
  },
}
What comes out
{
  total: 4,
  page: [
    { name: 'Apple iPhone', price: 999 },
    { name: 'Apple MacBook', price: 1299 },
  ],
}
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.