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
GNU General Public License v3.0
198 stars 23 forks source link

[FR] Sorting: use a "raw" value instead of "modified" #129

Open ildar170975 opened 5 months ago

ildar170975 commented 5 months ago

Consider smth like:

type: custom:auto-entities
filter:
  include:
    - domain: automation
      sort:
        count: 5
card:
  type: custom:flex-table-card
  columns:
    - name: name
      data: name
    - name: last_triggered
      data: last_triggered

изображение

We may sort the table by clicking. Let's sort by "last_triggered":

изображение

So far it works OK.

Now assume I need to apply modify for this last_triggered column:

type: custom:auto-entities
filter:
  include:
    - domain: automation
      sort:
        count: 5
card:
  type: custom:flex-table-card
  columns:
    - name: name
      data: name
    - name: last_triggered
      data: last_triggered
      modify: >-
        if (x) {
          if(x.length == 0){
            "-"
          }
          else {
            if (x != "н/д") {
              var date = new Date(x);

              (String(date.getDate()).padStart(2,'0')) +
              "/" +
              (String(date.getMonth()+ 1).padStart(2,'0')) + 
              "/" +
              date.getFullYear() +
              " " +
              String(date.getHours()).padStart(2,'0') +
              ":" +
              String(date.getMinutes()).padStart(2,'0') +
              ":" +
              String(date.getSeconds()).padStart(2,'0')
            }
            else {
              "-"
            }
          }
        }
        else {
          "-"
        }

изображение

And now sorting works in a wrong way - it sorts MODIFIED strings:

изображение

Proposal: add a per-column option like sort_by_raw (boolean): false - use a modified string for sorting, true - use a raw (untouched) string for sorting.


Current workaround - use another modifier which creates "sortable" strings starting by "YYYY/MM/DD":

if (x) {
  if(x.length == 0){
    "-"
  }
  else {
    if (x != "н/д") {
      var date = new Date(x);

      date.getFullYear() +
      "/" +
      (String(date.getMonth()+ 1).padStart(2,'0')) + 
      "/" +
      (String(date.getDate()).padStart(2,'0')) +
      " " +
      String(date.getHours()).padStart(2,'0') +
      ":" +
      String(date.getMinutes()).padStart(2,'0') +
      ":" +
      String(date.getSeconds()).padStart(2,'0')
    }
    else {
      "-"
    }
  }
}
else {
  "-"
}
ildar170975 commented 5 months ago

This is only crucial for a "sort by a header" use case. For a "static" (fixed) sorting we may just sort by a hidden column with "raw" data.