]> saetta.ns0.it Git - rust/zakgrid/commitdiff
Callback for missing columns.
authorAndrea Zagli <azagli@libero.it>
Wed, 4 Jan 2023 14:21:42 +0000 (15:21 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 4 Jan 2023 14:24:33 +0000 (15:24 +0100)
examples/grid.rs
src/lib.rs

index 09dc5e9737dbe54faa44511474280f2d1bb21a21..0f776a2594223695cf96b55d0f93544f5244554a 100644 (file)
@@ -43,6 +43,10 @@ struct AppState {
        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> {
@@ -78,6 +82,10 @@ async fn index(
        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() {
index b161b661112402a2596f208ef57d0020d7ac7620..c5bebac3c3a0e158d22383c217ef62611e8d6677 100644 (file)
@@ -2,6 +2,8 @@ use serde_json::Value;
 use serde_derive::Serialize;
 use tera::Tera;
 
+pub type ToCall = fn(row: &serde_json::Value) -> String;
+
 #[derive(Copy, Clone, Serialize)]
 pub enum Align {
        Left,
@@ -14,6 +16,7 @@ pub struct Column {
        title: String,
        align: Align,
        hidden: bool,
+       pub to_call: Option<ToCall>,
 }
 
 impl Column {
@@ -23,6 +26,7 @@ impl Column {
                        title: String::from(name),
                        align: Align::Left,
                        hidden: false,
+                       to_call: None,
                }
        }
 
@@ -130,7 +134,7 @@ impl Grid {
                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();
@@ -141,7 +145,19 @@ impl Grid {
                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());
                        }