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.

If you need more control, for example to add attributes to individual cells, you can configure custom rows using the TableRow and TableCell builders. These rows can then be added to the table body or header with with_custom_body_row and with_custom_header_row, respectively.

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§

source§

impl Table

source

pub fn new() -> Self

Creates a new table with an empty header and body

source

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 add_attributes 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>"#
);
source

pub fn with_attributes<A, S>(self, attributes: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,

Associates the specified map of attributes with this Table.

Note that this operation overrides all previous with_attributes 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>"#
);
source

pub fn add_caption<H: Html>(&mut self, caption: H)

Set the caption for the table

Example
let mut table = Table::new();
table.add_caption("Demo table");
assert_eq!(
    table.to_html_string(),
    "<table><caption>Demo table</caption><thead></thead><tbody></tbody></table>",
);
source

pub fn with_caption<H: Html>(self, caption: H) -> Self

Set the caption for the table

Example
let table = Table::from([[1, 2],[3, 4]])
    .with_header_row(['a', 'b'])
    .with_caption("A demo table")
    .to_html_string();
assert_eq!(table, concat!(
    "<table><caption>A demo table</caption>",
    "<thead><tr><th>a</th><th>b</th></tr></thead>",
    "<tbody><tr><td>1</td><td>2</td></tr>",
    "<tr><td>3</td><td>4</td></tr></tbody></table>",
));
source

pub fn add_thead_attributes<A, S>(&mut self, attributes: A)where A: IntoIterator<Item = (S, S)>, S: ToString,

Associates the specified map of attributes with the thead of this Table.

Note that this operation overrides all previous add_thead_attributes calls on this Table

Example
let mut table = Table::new();
table.add_thead_attributes([("id", "table-header")]);

assert_eq!(
    table.to_html_string(),
    r#"<table><thead id="table-header"></thead><tbody></tbody></table>"#
);
source

pub fn with_thead_attributes<A, S>(self, attributes: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,

Associates the specified map of attributes with the thead of this Table.

Note that this operation overrides all previous with_thead_attributes calls on this Table

Example
let table = Table::new()
    .with_attributes([("id", "my-table")])
    .with_thead_attributes([("id", "my-thead")])
    .to_html_string();

assert_eq!(
    table,
    r#"<table id="my-table"><thead id="my-thead"></thead><tbody></tbody></table>"#
);
source

pub fn add_tbody_attributes<A, S>(&mut self, attributes: A)where A: IntoIterator<Item = (S, S)>, S: ToString,

Associates the specified map of attributes with the tbody of this Table.

Note that this operation overrides all previous add_tbody_attributes calls on this Table

Example
let mut table = Table::new();
table.add_tbody_attributes([("id", "table-body")]);

assert_eq!(
    table.to_html_string(),
    r#"<table><thead></thead><tbody id="table-body"></tbody></table>"#
);
source

pub fn with_tbody_attributes<A, S>(self, attributes: A) -> Selfwhere A: IntoIterator<Item = (S, S)>, S: ToString,

Associates the specified map of attributes with the tbody of this Table.

Note that this operation overrides all previous with_tbody_attributes calls on this Table

Example
let table = Table::new()
    .with_attributes([("id", "my-table")])
    .with_tbody_attributes([("id", "my-body")])
    .to_html_string();

assert_eq!(
    table,
    r#"<table id="my-table"><thead></thead><tbody id="my-body"></tbody></table>"#
);
source

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>"
    )
)
source

pub fn with_header_row<T>(self, row: T) -> Selfwhere 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>"
    )
)
source

pub fn add_custom_header_row(&mut self, row: TableRow)

Add the specified row to the table header

Example
let mut table = Table::new();
table.add_custom_header_row(
    TableRow::new()
        .with_cell(TableCell::new(TableCellType::Header).with_raw("col1"))
        .with_cell(TableCell::new(TableCellType::Header).with_raw("col2"))
        .with_cell(TableCell::new(TableCellType::Header).with_raw("col3"))
);

assert_eq!(
    table.to_html_string(),
    concat!(
        "<table><thead>",
        "<tr><th>col1</th><th>col2</th><th>col3</th></tr>",
        "</thead><tbody></tbody></table>",
    ),
);
source

pub fn with_custom_header_row(self, row: TableRow) -> Self

Add the specified row to the table header

Example
let table = Table::new()
    .with_custom_header_row(
        TableRow::new()
            .with_attributes([("class", "long-row")])
            .with_cell(TableCell::new(TableCellType::Header).with_raw("col1"))
            .with_cell(TableCell::new(TableCellType::Data).with_raw("col2"))
            .with_cell(
                TableCell::new(TableCellType::Header)
                    .with_attributes([("id", "third")])
                    .with_raw("col3")
            ),
    )
    .to_html_string();

assert_eq!(
    table,
    concat!(
        r#"<table><thead><tr class="long-row">"#,
        r#"<th>col1</th><td>col2</td><th id="third">col3</th>"#,
        "</tr></thead><tbody></tbody></table>",
    ),
);
source

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>"
    )
)
source

pub fn with_body_row<T>(self, row: T) -> Selfwhere 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>"
    )
)
source

pub fn add_custom_body_row(&mut self, row: TableRow)

Add the specified row to the table body

Example
let mut table = Table::new();
table.add_custom_body_row(
    TableRow::new()
        .with_cell(TableCell::default().with_raw("col1"))
        .with_cell(TableCell::default().with_raw("col2"))
        .with_cell(TableCell::default().with_raw("col3"))
);

assert_eq!(
    table.to_html_string(),
    concat!(
        "<table><thead></thead><tbody>",
        "<tr><td>col1</td><td>col2</td><td>col3</td></tr>",
        "</tbody></table>",
    ),
);
source

pub fn with_custom_body_row(self, row: TableRow) -> Self

Add the specified row to the table body

Example
let table = Table::new()
    .with_custom_body_row(
        TableRow::new()
            .with_attributes([("class", "long-row")])
            .with_cell(TableCell::default().with_raw("col1"))
            .with_cell(TableCell::default().with_raw("col2"))
            .with_cell(
                TableCell::default()
                    .with_attributes([("id", "third")])
                    .with_raw("col3")
            ),
    )
    .to_html_string();

assert_eq!(
    table,
    concat!(
        r#"<table><thead></thead><tbody><tr class="long-row">"#,
        r#"<td>col1</td><td>col2</td><td id="third">col3</td>"#,
        "</tr></tbody></table>",
    ),
);

Trait Implementations§

source§

impl Debug for Table

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Table

source§

fn default() -> Table

Returns the “default value” for a type. Read more
source§

impl<T> From<T> for Tablewhere T: IntoIterator, T::Item: IntoIterator, <<T as IntoIterator>::Item as IntoIterator>::Item: Display,

source§

fn from(source: T) -> Self

Converts to this type from the input type.
source§

impl Html for Table

source§

fn to_html_string(&self) -> String

Convert this element into an HTML string Read more
source§

impl PartialEq<Table> for Table

source§

fn eq(&self, other: &Table) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Table

source§

impl StructuralEq for Table

source§

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.