settings: Settings,
}
+fn get_not_exists(value: &serde_json::Value) -> String {
+ String::from("not exists (returned by fn)")
+}
+
async fn index(
data: web::Data<AppState>,
) -> Result<impl Responder, Error> {
col.set_align(zakgrid::Align::Right);
grid.add_column(col);
+ let mut col = zakgrid::Column::new("not_exists");
+ col.to_call = Some(get_not_exists);
+ grid.add_column(col);
+
let cols = grid.columns();
for (i, v) in cols.iter().enumerate() {
use serde_derive::Serialize;
use tera::Tera;
+pub type ToCall = fn(row: &serde_json::Value) -> String;
+
#[derive(Copy, Clone, Serialize)]
pub enum Align {
Left,
title: String,
align: Align,
hidden: bool,
+ pub to_call: Option<ToCall>,
}
impl Column {
title: String::from(name),
align: Align::Left,
hidden: false,
+ to_call: None,
}
}
String::from(tera.render("header", &context).unwrap().as_str())
}
- pub fn row_from_json(&self, value: &serde_json::Value) -> String {
+ pub fn row_from_json(&self, row: &serde_json::Value) -> String {
let mut s = String::new();
let mut tera = Tera::default();
for c in &self.columns {
if !c.hidden {
let mut context = tera::Context::new();
- context.insert("value", &value[c.name.as_str()]);
+ match &row[c.name.as_str()] {
+ serde_json::Value::Null => {
+ match c.to_call {
+ None => context.insert("value", ""),
+ Some(call) => {
+ let v = (call)(&row);
+ context.insert("value", v.as_str());
+ }
+ }
+ },
+ _ => context.insert("value", &row[c.name.as_str()])
+ }
+
context.insert("align", &c.align);
s.push_str(tera.render("row_col", &context).unwrap().as_str());
}