prettytable-rs icon indicating copy to clipboard operation
prettytable-rs copied to clipboard

.printhtml() method doesnt insert table headers (titles) correctly

Open belakm opened this issue 1 year ago • 2 comments

Description

When using .printhtml() method on a table, it doesnt work correctly for titles. It inserts <td> elements into <th> elements, treating <th> as <tr> and <td> as <th>.

Current solution around this is to treat table headers as just a row as those seem to get printed just fine.

Example snippet:

let mut out = File::create("table.html").unwrap();
let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
table.set_titles(row!["Title 1", "Title 2"]);
table.print_html(&mut out);

it renders the below table as such in HTML:

<table>
  <th>
    <td style="text-align: left;">Title 1</td>
    <td style="text-align: left;">Title 2</td>
  </th>
  <!-- ... data rows -->
</table>

The correct way for table headers would be:

<table>
  <tr>
    <th style="text-align: left;">Title 1</th>
    <th style="text-align: left;">Title 2</th>
  </tr>
  <tr>
  <!-- ... data rows -->
</table>

Chrome based browsers try their best to render this and they somehow get it, but it is skewed because of the empty <th> element:

image

belakm avatar Nov 09 '23 13:11 belakm

Hi there, thanks for this nice and simple crate!

Since I ran into the exact same issue as described by @belakm and this thread remained unanswered, I decided to quickly put together a fix. PR #161

michaelsippel avatar Jan 21 '24 18:01 michaelsippel

Hey thanks for the fix and very nicely detailed issue - I left a comment in your PR just honing the API a bit :)

pinkforest avatar Feb 01 '24 02:02 pinkforest