Struct build_html::Table
source · [−]pub struct Table { /* private fields */ }Expand description
Represents an HTML <table> element with all its children.
The easiest way to make a table is by simply passing in a 2D Array or Vec.
Using this method, the entire contents will be placed in <td> elements within
the <tbody>. If a header row is desired, one can be added manually.
Example
let source_table = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let html_table = Table::from(source_table)
.with_header_row(['A', 'B', 'C'])
.to_html_string();
assert_eq!(
html_table,
concat!(
"<table><thead>",
"<tr><th>A</th><th>B</th><th>C</th></tr>",
"</thead><tbody>",
"<tr><td>1</td><td>2</td><td>3</td></tr>",
"<tr><td>4</td><td>5</td><td>6</td></tr>",
"<tr><td>7</td><td>8</td><td>9</td></tr>",
"</tbody></table>"
)
);Implementations
sourceimpl Table
impl Table
sourcepub fn add_attributes<A, S>(&mut self, attributes: A) where
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn add_attributes<A, S>(&mut self, attributes: A) where
A: IntoIterator<Item = (S, S)>,
S: ToString,
Associates the specified map of attributes with this Table.
Note that this operation overrides all previous with_attribute calls on
this Table
Example
let mut table = Table::new();
table.add_attributes([("id", "my-table")]);
assert_eq!(
table.to_html_string(),
r#"<table id="my-table"><thead></thead><tbody></tbody></table>"#
);sourcepub fn with_attributes<A, S>(self, attributes: A) -> Self where
A: IntoIterator<Item = (S, S)>,
S: ToString,
pub fn with_attributes<A, S>(self, attributes: A) -> Self where
A: IntoIterator<Item = (S, S)>,
S: ToString,
Associates the specified map of attributes with this Table.
Note that this operation overrides all previous with_attribute calls on
this Table
Example
let table = Table::new()
.with_attributes([("id", "my-table")])
.to_html_string();
assert_eq!(
table,
r#"<table id="my-table"><thead></thead><tbody></tbody></table>"#
);sourcepub fn add_header_row<T>(&mut self, row: T) where
T: IntoIterator,
T::Item: Display,
pub fn add_header_row<T>(&mut self, row: T) where
T: IntoIterator,
T::Item: Display,
Adds the specified row to the table header
Note that no checking is done to ensure that the row is of the proper length
Example
let mut table = Table::new();
table.add_header_row(vec!["Mon", "Tues", "Wed", "Thurs", "Fri"]);
assert_eq!(
table.to_html_string(),
concat!(
"<table><thead>",
"<tr><th>Mon</th><th>Tues</th><th>Wed</th><th>Thurs</th><th>Fri</th></tr>",
"</thead><tbody></tbody></table>"
)
)sourcepub fn with_header_row<T>(self, row: T) -> Self where
T: IntoIterator,
T::Item: Display,
pub fn with_header_row<T>(self, row: T) -> Self where
T: IntoIterator,
T::Item: Display,
Adds the specified row to the table header
Note that no checking is done to ensure that the row is of the proper length
Example
let table = Table::new()
.with_header_row(vec!["Mon", "Tues", "Wed", "Thurs", "Fri"])
.to_html_string();
assert_eq!(
table,
concat!(
"<table><thead>",
"<tr><th>Mon</th><th>Tues</th><th>Wed</th><th>Thurs</th><th>Fri</th></tr>",
"</thead><tbody></tbody></table>"
)
)sourcepub fn add_body_row<T>(&mut self, row: T) where
T: IntoIterator,
T::Item: Display,
pub fn add_body_row<T>(&mut self, row: T) where
T: IntoIterator,
T::Item: Display,
Adds the specified row to the table body
Note that no checking is done to ensure that the row is of the proper length
Example
let mut table = Table::new();
table.add_body_row(vec![1, 2, 3, 4, 5]);
assert_eq!(
table.to_html_string(),
concat!(
"<table><thead></thead><tbody>",
"<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>",
"</tbody></table>"
)
)sourcepub fn with_body_row<T>(self, row: T) -> Self where
T: IntoIterator,
T::Item: Display,
pub fn with_body_row<T>(self, row: T) -> Self where
T: IntoIterator,
T::Item: Display,
Adds the specified row to the table body
Note that no checking is done to ensure that the row is of the proper length
Example
let table = Table::new()
.with_body_row(vec![1, 2, 3, 4, 5])
.to_html_string();
assert_eq!(
table,
concat!(
"<table><thead></thead><tbody>",
"<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>",
"</tbody></table>"
)
)Trait Implementations
sourceimpl<T> From<T> for Table where
T: IntoIterator,
T::Item: IntoIterator,
<<T as IntoIterator>::Item as IntoIterator>::Item: Display,
impl<T> From<T> for Table where
T: IntoIterator,
T::Item: IntoIterator,
<<T as IntoIterator>::Item as IntoIterator>::Item: Display,
sourceimpl Html for Table
impl Html for Table
sourcefn to_html_string(&self) -> String
fn to_html_string(&self) -> String
Convert this element into an HTML string Read more
impl Eq for Table
impl StructuralEq for Table
impl StructuralPartialEq for Table
Auto Trait Implementations
impl RefUnwindSafe for Table
impl Send for Table
impl Sync for Table
impl Unpin for Table
impl UnwindSafe for Table
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more