Search code examples
htmlkdb

how to highlight/change color to particular word in a table in html5?


If there is a table, and the last column is called result. I want to highlight all the cell/text that has "Failed".

How can I do that?

I need to use html5 with a table from kdb.

Currently my email function is this

htmlMailBody:{[emailadd;subject;message]
cmd:"echo \"",message, "\" | mutt -e  \"my_hdr 
From:[email protected]\" -e \"my_hdr Content-Type: 
text/html\" ",emailadd, " -s \"",subject,"\"";
sent:@[{system x;1b};cmd;{.log.error"Failure sending email. Reason: ",x;0b}];
 if[sent; .log.info "Sent email to ",emailadd ];
};



mailRCP:bbc.gmail.com



htmlMailBody[mailRCP ;"health check";(,/)("<h2>SOD CHECKS<hr /></h2>";"<br />";markup[result];"<br />")];

This didnt work. if replace markup[result] with a kdb table it will work.


Solution

  • To markup your HTML table direct from q, use the markup functions in the .h namespace.

    Let your table be t.

    q)t
    a  b  c  d  result
    -------------------
    94 66 8  82 success
    8  24 62 47 failed
    97 60 95 26 success
    52 69 59 93 success
    

    Make a corresponding table at of attributes for the HTML td elements. Start with empty dictionaries, for no attributes. An empty dictionary is ()!().

    q)show at:flip (cols t)! (count each(cols t;t))#enlist ()!()
    a     b     c     d     result
    ------------------------------
    ()!() ()!() ()!() ()!() ()!()
    ()!() ()!() ()!() ()!() ()!()
    ()!() ()!() ()!() ()!() ()!()
    ()!() ()!() ()!() ()!() ()!()
    

    Update the result column of at according to the result column of t.

    q)f:t[`result]=`failed
    q)update result:([]color:(sum f)#enlist"red")from `at where f
    q)at
    a     b     c     d     result
    ----------------------------------------
    ()!() ()!() ()!() ()!() ()!()
    ()!() ()!() ()!() ()!() (,`color)!,"red"
    ()!() ()!() ()!() ()!() ()!()
    ()!() ()!() ()!() ()!() ()!()
    

    We can use .h.htac to mark up the table cells with the attribute dictionaries. First the table cells as strings:

    q)string t cols t
    "94"      ,"8"     "97"      "52"
    "66"      "24"     "60"      "69"
    ,"8"      "62"     "95"      "59"
    "82"      "47"     "26"      "93"
    "success" "failed" "success" "success"
    

    Never mind that they’ve been flipped. Now the dictionaries from at – also flipped.

    q)at cols t
    ()!() ()!()            ()!() ()!()
    ()!() ()!()            ()!() ()!()
    ()!() ()!()            ()!() ()!()
    ()!() ()!()            ()!() ()!()
    ()!() (,`color)!,"red" ()!() ()!()
    

    We can use these as the second and first arguments respectively of .h.htac. The each-both adverb will iterate over corresponding rows, but we want corresponding cells, so .h.htac'' to iterate within cells within rows.

    q).h.htac''[`td;at cols t;string t cols t]
    "<td>94</td>"      "<td>8</td>"                    "<td>97</td>"      "<td>52..
    "<td>66</td>"      "<td>24</td>"                   "<td>60</td>"      "<td>69..
    "<td>8</td>"       "<td>62</td>"                   "<td>95</td>"      "<td>59..
    "<td>82</td>"      "<td>47</td>"                   "<td>26</td>"      "<td>93..
    "<td>success</td>" "<td color=\"red\">failed</td>" "<td>success</td>" "<td>su.. 
    

    Function markup assembles the HTML table element:

    markup:{[t]
      th:.h.htc[`tr;]raze .h.htc[`th;] each string cols t;                           / table head
      at:flip (cols t)! (count each(cols t;t))#enlist ()!();                         / empty attribute dictionaries
      f:t[`result]=`failed;
      at:update result:([]color:(sum f)#enlist"red")from at where f;                 / attributes for result failed
      tr:.h.htc[`tr;]each raze each flip .h.htac''[`td;at cols t;string t cols t];   / table rows
      .h.htc[`table;] .h.htc[`thead;th],.h.htc[`tbody;raze tr]
      }
    

    Using a table of attribute dictionaries is a robust technique that can be adapted to all kinds of highlighting, or providing IDs for client-side scripts.