web, App, Error, HttpResponse, HttpServer, Responder, Result,
};
+use futures::TryStreamExt;
+use sqlx::{Connection, SqliteConnection, Row, Column};
+use std::str::FromStr;
+
#[derive(Debug, Deserialize)]
#[allow(unused)]
struct Http {
s.push_str("<html>");
s.push_str("<body>");
- let mut col = zakgrid::Column::new("col name");
+ let mut col = zakgrid::Column::new("id");
s.push_str(format!("column added: {}<br/>\n", col.name()).as_str());
col.set_title("the column title");
s.push_str(format!("column changed title: {}<br/>\n", col.title()).as_str());
grid.add_column(col);
- let mut col = zakgrid::Column::new("col2 name");
+ let mut col = zakgrid::Column::new("name");
col.set_title("the second column title");
grid.add_column(col);
- let mut col = grid.get_column("col2 name").unwrap();
+ let mut col = zakgrid::Column::new("surname");
+ grid.add_column(col);
+
+ let mut col = zakgrid::Column::new("age");
col.set_align(zakgrid::Align::Right);
+ grid.add_column(col);
+
+ let mut col = zakgrid::Column::new("income");
+ col.set_align(zakgrid::Align::Right);
+ grid.add_column(col);
let cols = grid.columns();
s.push_str("<table style='border: 1px solid;'>\n");
let header = grid.header();
s.push_str(header.as_str());
+
+ let mut conn = SqliteConnection::connect("examples/grid.db").await.unwrap();
+
+ let mut rows = sqlx::query("SELECT * FROM clients").fetch(&mut conn);
+ while let Some(row) = rows.try_next().await.unwrap() {
+ let cols = row.columns();
+
+ let mut r = String::new();
+ r.push_str("{");
+ for col in cols {
+ let s: &str = row.try_get_unchecked(col.name()).unwrap();
+ r.push_str(format!("\"{}\": \"{}\",", col.name(), s).as_str());
+ }
+ r.truncate(r.len() - 1);
+ r.push_str("}");
+
+ let val = serde_json::Value::from_str(r.as_str()).unwrap();
+ s.push_str(grid.row_from_json(&val).as_str());
+ }
+
s.push_str("</table>\n");
s.push_str("</body>\n");
+use serde_json::Value;
use serde_derive::Serialize;
use tera::Tera;
context.insert("row", s.as_str());
String::from(tera.render("header", &context).unwrap().as_str())
}
+
+ pub fn row_from_json(&self, value: &serde_json::Value) -> String {
+ let mut s = String::new();
+
+ let mut tera = Tera::default();
+
+ tera.add_raw_template("row", &self.row_tmpl);
+ tera.add_raw_template("row_col", &self.row_col_tmpl);
+
+ for c in &self.columns {
+ if !c.hidden {
+ let mut context = tera::Context::new();
+ context.insert("value", &value[c.name.as_str()]);
+ context.insert("align", &c.align);
+ s.push_str(tera.render("row_col", &context).unwrap().as_str());
+ }
+ }
+
+ let mut context = tera::Context::new();
+ context.insert("row", s.as_str());
+ String::from(tera.render("row", &context).unwrap().as_str())
+ }
}