custom-cards / flex-table-card

Highly Flexible Lovelace Card - arbitrary contents/columns/rows, regex matched, perfect to show appdaemon created content and anything breaking out of the entity_id + attributes concept

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sort_by doesnt seem to work with attributes list

Dinth opened this issue · comments

Sort_by function doesnt seem to work with x. data sources.
In the example below, adding or removing "sort_by" doesnt change the output at all.
I have also tried using: data, data.best_before_date, data.x.best_before_date as a parameter for sort_by without any luck
Here's my lovelace card:

columns:
  - data: items
    name: Product
    modify: x.name
  - data: items
    name: Amount
    modify: x.available_amount
  - data: items
    name: Expiring
    modify: x.best_before_date
sort_by: x.best_before_date
entities:
  include: binary_sensor.grocy_expired_products
title: Expiring products
type: 'custom:flex-table-card'

and the example data:

items: 
- name: Nuts - Brazil Nuts - Alesto
  id: 16
  product_group_id: null
  available_amount: 2
  best_before_date: '2020-07-12T00:00:00+00:00'
  barcodes:
    - '20202392'
  amount_missing: null
  is_partly_in_stock: null
- name: Nutmeg Extract
  id: 53
  product_group_id: null
  available_amount: 1
  best_before_date: '2017-07-22T00:00:00+00:00'
  barcodes:
    - '740101219146'
  amount_missing: null
  is_partly_in_stock: null
- name: Quinoa Puffed - Golden Sun
  id: 66
  product_group_id: null
  available_amount: 1
  best_before_date: '2019-02-28T00:00:00+00:00'
  barcodes:
    - '20897789'
  amount_missing: null
  is_partly_in_stock: null
- name: Pasta Conchiglie - Combino
  id: 71
  product_group_id: null
  available_amount: 1
  best_before_date: '2020-06-05T00:00:00+00:00'
  barcodes:
    - '20501198'
  amount_missing: null
  is_partly_in_stock: null
- name: Pasta Egg Spatzle - Combino
  id: 75
  product_group_id: null
  available_amount: 1
  best_before_date: '2017-08-14T00:00:00+00:00'
  barcodes:
    - '20049997'
  amount_missing: null
  is_partly_in_stock: null
- name: Rice Paella - Morrisons
  id: 76
  product_group_id: null
  available_amount: 1
  best_before_date: '2018-02-28T00:00:00+00:00'
  barcodes:
    - '5010251461289'
  amount_missing: null
  is_partly_in_stock: null
- name: 'Soba Noodles - Vitasia '
  id: 85
  product_group_id: null
  available_amount: 1
  best_before_date: '2020-02-28T00:00:00+00:00'
  barcodes:
    - '20561864'
  amount_missing: null
  is_partly_in_stock: null
- name: Honey - Miód wielokwiatowy - Favorit
  id: 111
  product_group_id: null
  available_amount: 1
  best_before_date: '2016-07-28T00:00:00+00:00'
  barcodes:
    - '4770148229361'
  amount_missing: null
  is_partly_in_stock: null
- name: 'Krem do Gulaszu - Univer '
  id: 117
  product_group_id: null
  available_amount: 1
  best_before_date: '2020-08-12T00:00:00+00:00'
  barcodes:
    - '5997010302024'
  amount_missing: null
  is_partly_in_stock: null

checking the docs I can see that "attribute" is kind of ambiguous there.
Usually you should be using data as the sorting column identifier, like: sort_by: items, in your case this does not identify a column in a unique way, therefore there is id which can be set for a column (which you can only find in the config reference ... not too well documented 😄 )
for your data I would try something like that:

columns:
  - data: items
    name: Product
    modify: x.name
  - data: items
    name: Amount
    modify: x.available_amount
  - data: items
    id: best_before
    name: Expiring
    modify: x.best_before_date
sort_by: best_before
entities:
  include: binary_sensor.grocy_expired_products
title: Expiring products
type: 'custom:flex-table-card'

Thanks @daringer.
It works, but in the mean time i have upgraded my config to change values in column to human readable format and i wanted to sort the items by original x.best_before_date instead of modified values.
That's how it works now:

columns:
  - data: items
    modify: x.name
    name: Product
  - data: items
    modify: x.available_amount
    name: Amount
  - data: items
    id: best_before
    modify: >-
      const dateDiff = (Date.now() - Date.parse(x.best_before_date));  const
      daysDiff = dateDiff/86400000; const days = Math.floor(daysDiff); days + "
      days "
    name: Expiring
    suffix: ' ago'
sort_by: best_before
entities:
  include: binary_sensor.grocy_expired_products
title: Expiring products
type: 'custom:flex-table-card'

the common approach to sort an human readable column is to add an additional column with the machine-readable data and set it to hidden: true and sort by this column.

Many thanks, it worked!