-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Suggested change to "Sort the table" task solution in "Modifying the document" article #1591
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
Comments
What if there are many tbodies? :) |
Well, with the current code in the task solution, all the rows in all the table (except the 1st, thanks to the With the code I recommended, just as it is, the first tbody would be sorted properly and the others left as they were. But if one wanted to handle multiple tbodies, it would be a simple matter to use the code I recommended, slightly modified to reflect indexing on
Seems to work. BUT, to be fair, for my HTML I had a separate thead for each tbody, which according to MDN you can't do (only one thead per table, they say), but still, it does seem to work that way in Chrome, Firefox, Edge, Opera, and Safari (Windows version). If you were doing it the way the MDN documentation says you have to, with each tbody having the headers inside it as (BTW, I found that while multiple thead elements in a table does seem to work (at least sometimes), despite what one might have heard, things do still get screwy if you have multiple tfoot elements in a table. I realize this may not be news to you.) |
Seriously? That doesn't seem right. |
If the table has only one tbody, is the solution wrong? |
Here I aim to provide the solution for 1 tbody. And then people can easily adjust it. Did I miss something from your words? Please let me know. |
As I initially stated, the current solution works for the example, with one The solution I gave would. Also, basically, I think it's more logical to take the rows to sort from the same element that you are returning them to ( So for the above reasons I think it would be a little better. From the point of view of just solving the example as given, without worrying about other possibilities, the main argument I would make is the logic one -- you want to have the sorted rows in So I think taking the rows from |
Given all that, let's have the task with 1 tbody case. Should I add that condition to the task formulation? |
Once again, thank you for your ideas and healthy criticism ;) |
I'm trying to be very clear, even redundant, but somehow I'm not making my point. I am not suggesting a solution for multiple tbodies (except yesterday, when I thought you were asking for one). The current solution WOULD sort any footer (if there were one), and put its row(s) into the tbody. Basically, it only works for the exact configuration of the example. And the change I suggested would not only be a bit more flexible in that regard, I think it would be simpler and more logical. And I think it is LESS, not more complex than the current solution, in the sense that it no longer needs the |
Oh, right. Sorry, hopefully now I got the point. P.S. It could be easier for me to read your text if it were split into paragraphs. Somehow github just puts the whole text together. Maybe when you're making line breaks, you make single breaks, not double breaks. Markdown merges such line breaks together, turning the whole text into a single huge line. |
I mean, not always, sometimes. |
Good to know! I do look at the previews here in GitHub (when I don't, I'm sorry), and it seems OK.. But I can add more spacing. In general, I'm a fan of whitespace. |
I just revised my longer comment from earlier today. Now, pretty much every sentence is its own paragraph, kind of like bullet points. Better? |
Definitely better. This way I would have never missed the thought from the first paragraph 🤦♂ |
Thanks, sorry once again for not getting this point immediately =) |
Thanks. I really did try to be very clear from the start in what I was saying, and I still don't quite understand why it didn't work, but I also really appreciate the sentiment. Onward and forward. |
In the "Modifying the document" article, for the last task "Sort the table" (https://javascript.info/modifying-document#tasks) the solution given is:
Which obviously works for the table provided. But it would not work for a table with no header or with a header with more than one row, or for a table with a footer. I would like to suggest a simple change to make the solution more flexible so that it can accommodate any type of header or footer situation:
(Only code between
'Array.from('
and'.sort'
is changed). The code withtable.tBodies[0]
instead of justtable
is about the same length, is maybe a little easier to understand (we get the array of rows fromtBodies[0]
, sort it, then write it back to the same place), and is more flexible. And it could easily be adapted to handle tables with multiple table bodies.The text was updated successfully, but these errors were encountered: