Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting should use the displayed value, not the data-sort value #306

Open
fisharebest opened this issue Nov 29, 2024 · 2 comments
Open

Comments

@fisharebest
Copy link
Contributor

See https://codepen.io/fisharebest/pen/dPbPddq for a live example.

Adding data-sort to a column causes it to be formatted according to the type of data-sort, rather than the type of the displayed value.

Without data-sort, column one is left-justified and column two is right-justified - as expected.

<table>
  <thead>
    <tr><td>Medal</td><td>Count</td></tr>
  </thead>
  <tbody>
    <tr><td>Gold</td><td>3</td></tr>
    <tr><td>Silver</td><td>2</td></tr>
    <tr><td>Bronze</td><td>4</td></tr>
  </tbody>
</table>

With data-sort, both columns are now right-justified - which is wrong. Changing the sorting shouldn't change the display.

<table>
  <thead>
    <tr><td>Medal</td><td>Count</td></tr>
  </thead>
  <tbody>
    <tr><td data-sort="1">Gold</td><td>3</td></tr>
    <tr><td data-sort="2">Silver</td><td>2</td></tr>
    <tr><td data-sort="3">Bronze</td><td>4</td></tr>
  </tbody>
</table>
@AllanJard
Copy link
Contributor

I take the point - that isn't ideal. Unfortunately at the moment there isn't orthogonal data types in DataTables - ideally there would be a data type for the sort, another for the display, another for the filter, etc. However at the moment they are all lumped together into a single "type". This is much simpler and better for performance, but the down side is that we can get weird quirks like this.

For now, the solution is to set the class for the column to align the text left (dt-left would do).

Another option would be to use the enum plugin and have it sort on Gold, Silver, Bronze.

Sorry I don't have a better answer at the moment - orthogonal types is something I've been thinking about, but it isn't yet on the roadmap.

@fisharebest
Copy link
Contributor Author

fisharebest commented Nov 29, 2024

Thanks for the quick reply and explanation.

As a simple workaround, I can force my sort-data to be text. e.g.

data-sort="<?= $n++ ?>"

becomes

data-sort="x<?= sprintf('%07d', $n++) ?>"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants