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

Sort by column keys #181

Merged
merged 8 commits into from
Jan 23, 2020
Merged

Sort by column keys #181

merged 8 commits into from
Jan 23, 2020

Conversation

harimohanraj89
Copy link
Contributor

Issue

This PR allows users to fix the issues they encountered in #48.

Context

As noted in #48, tablesort currently assumes that the columns in table headers match the columns in the table body of every row. This leads to arguably erratic behavior when tablesort is applied to tables that utilize colspans.

I believe #130 attempts to rectify this problem by calculating the appropriate column to sort by, accounting for varying colspans in every row. However, it appears this approach wasn't seen to completion as @tristen noted here. There were also some interesting edge cases to be considered with this mathematical approach. What happens when a row doesn't have any cells lining up with the clicked header because of, say, a full-width cell in that row? Do we treat the row as empty, or do we use the value of the full-width cell in sorting by any of the columns?

This PR's Approach

This PR takes a more configuration-heavy approach, adding support for a data-sort-column-key data attribute to both headers and cells. Users can configure exactly which cells in each row they wish to be used for sorting when the corresponding header is clicked. This allows for users to "fix" the behavior of their tablesorts even when using colspans, as they are free to manually assign the appropriate column keys to table cells to sort the table as they see fit.

Documentation

I've taken a stab at updating the documentation too, but I'm not 100% sure how to test that I've done that correctly. If you can provide guidance on how to test it out, I'd be happy to fix it up if it's broken.

Copy link
Owner

@tristen tristen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Thanks for addressing #48! This looks really solid.

Tests are failing on Travis because https://www.npmjs.com/package/phantomjs-prebuilt is long dead (+ the test framework is a little dusty here in general). Tests pass locally though, so I'm not too worried about that. I just left a couple of small comments then this looks good to go!

This PR takes a more configuration-heavy approach

💯

demo/index.md Outdated Show resolved Hide resolved
demo/index.md Outdated Show resolved Hide resolved
demo/index.md Outdated Show resolved Hide resolved
</table>
{% endhighlight %}

<table id='column-keys' class='sort'>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the documentation of this project is a little horrendous but for your code example to work, can you add

new Tablesort(document.getElementById('column-keys'));

Within the <script> tag here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I incorrectly assumed that the script at the bottom would initiate Tablesorts on all table.sort elements in here. My mistake!

I've given your suggestion a whirl!

@harimohanraj89
Copy link
Contributor Author

Thanks for the review, @tristen! I figured the Travis failures were unrelated, good to know!

I've addressed your feedback, let me know if there's anything else, thanks!

@tristen tristen merged commit c381b5b into tristen:gh-pages Jan 23, 2020
@tristen
Copy link
Owner

tristen commented Jan 23, 2020

Published to npm as v5.2.0

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

Successfully merging this pull request may close these issues.

None yet

2 participants