-
Notifications
You must be signed in to change notification settings - Fork 252
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
Preserve HTML cell attributes in rendered table #346
Conversation
946c183
to
ecb2579
Compare
docs/demos/1-simple/index.html
Outdated
@@ -26,7 +26,7 @@ <h1>Simple</h1> | |||
<thead> | |||
<tr> | |||
<th> | |||
<b>N</b>ame | |||
<b>Name</b> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the need for this change here? Are there issues when only part of the contents of a cell are styled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an artifact of my testing; I'll revert it.
Was checking if my eyes were playing me a trick, but it seems that the headers are already bold with the default styling. Maybe a better option would be to have this header in italics, so it is visually distinct from the others?
@SandroHc This is the largest of your changes and so I want to make sure I fully understand what is going on before I merge it. I have been telling people for some time now that they should just create new data columns if they have more data points instead of adding attributes to individual cells. Now with this they can go back to what they used to do. Question - it looks like adding attributed to a cell is only supported if you start out with a dom structure, not if you start out with your data as objects and arrays. So for exam ple in I'll continue studying your PR... |
I may have a niche use case. I want to transform lots of "dumb" tables (that may have custom styling either on a row or a cell) and convert them into rich datatables with the bare minimum JavaScript necessary. Something like this: <table>
<thead>
<tr>
<th style="color: red;">Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr class="fancy-row">
<td style="color: red;">Value 1</td>
<td>Value 1</td>
</tr>
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest"></script>
<script>
document.querySelectorAll('table').forEach(table => new simpleDatatables.DataTable(table));
</script> From what I read, I thought I can put these changes behind a "preserveDomAttributes" option. Something opt-in for exotic use-cases like mine. |
Guarantees that custom attributes from the HTML table headers (<th>) or cells (<td>) are preserved in the rendered datatable. See the following example for a practical use case. Note the attributes in the `<th>` and `<td>` elements. ```html <table id="example-table"> <thead> <tr> <th class="name">Name</th> <!-- header with custom class --> <th>City</th> </tr> </thead> <tbody> <tr> <td style="background-color: yellow;">Unity Pugh</td> <!-- cell with custom colour --> <td>Curicó</td> </tr> <tr> <td>Theodore Duran</td> <td>Dhanbad</td> </tr> </tbody> </table> <script> const dt = new DataTable("example-table"); </script> ```
ecb2579
to
3982ce8
Compare
No that's fine. If the attributes are there already and one reads from the dom to initialize the table, then I think it's reasonable if we can keep the attributes of the cell. The only reason I didn't do that so far is that it takes time to program - time that you now have invested already. It seems like there are quite a few users who use it like that. I was just wondering if one initiates from just JSON. I have added a demo 25 for that and just did need to add one minor detail to make it work. I have not tested whether it is also possible to add cells with attributes after initialization. Thanks for all this work @SandroHc ! I guess we should make a release. Let me know if you want to include other things before I create a release. |
Preserves attributes from the HTML table headers (
<th>
) or cells (<td>
) in the rendered datatable.See the following example for a practical use case. Note the attributes in the
<th>
and<td>
elements.