filling rows from struct-map

160 views
Skip to first unread message

Vesna Petkovic

unread,
Dec 22, 2011, 2:41:52 PM12/22/11
to enliv...@googlegroups.com
Hello everybody,

I have a question, I want to fill rows in html table from a struct-map. I have a problem with row snippet, because I don't know how to define cell selectors. Each key value pair from my struct map is for one cell in a row. So my question is how do I define cell selectors, do I have to go one by one cell, or is there any other way to do it (there probably is one but I just can't seem to find it). I would probably need some kind of dynamic selector...

Here is my row html from the file index.html
<tr>
                <td>Event Title</td>
                <td>Type of event</td>
                <td>Performer</td>
                <td>About</td>
                <td>Date</td>
                <td>Start time</td>
                <td>End time</td>
                <td>Place</td>
                <td>Video</td>
        </tr> Here are my selectors, snippets and template (def *first-cell-sel* [[:tr (html/nth-child 1)][:td html/first-child]]) (def *rest-cell-sel* [[:table] [:tr (html/nth-child 2)][:td (html/nth-child 2)]]) (defsnippet second-cell "eventsmashup/index.html" [[:table] [:tr (html/nth-child 1)][:td (html/nth-child 2)]] [{value :performer}] [:td] (html/content value)) (defsnippet cell-model "eventsmashup/index.html" *first-cell-sel* [{value :value}] [:td] (html/content value)) (defsnippet row-model "eventsmashup/index.html" *row-sel* [{:keys [event-data]} model] [:tr] (html/content (map model event-data))) (deftemplate index "eventsmashup/index.html" [{:keys [event-data]}] [:tbody] (html/content (map #(row-model % cell-model % second-cell)rows))) and here is part of dummy-content struct-map, that I use for testing (def *dummy-content* {:title "Events Mashup" :rows [ { :event-data [{:event-name "event name 1" :performer "performer 1" :date "date 1" :start-time "start time 1" :end-time "start time 1"}]}]}) When I start the app, the table is blank, my code doesn't insert any content, and I can't seem to get the reason why. If anybody can give me the answer I'd be really grateful. Thanks Vesna

gwanwyn

unread,
Mar 29, 2012, 2:02:34 PM3/29/12
to enliv...@googlegroups.com
I found the solution, and here it is , if anyone is interested

(html/defsnippet row-snippet table-template [[:tr (attr= :title "event")]]
  [{:keys [event-name performers date start-time end-time]}]

  [[:td (attr= :title "event-title")]] (html/content event-name)
  [[:td (attr= :title "performer")]] (html/content performers)
  [[:td (attr= :title "date")]] (html/content date)
  [[:td (attr= :title "start-time")]] (html/content start-time)
  [[:td (attr= :title "end-time")]] (html/content end-time))

(deftemplate indeks table-template
  [{:keys  [title event-data]}]
  [:title] (html/content title)
  [:tbody]  (html/content (map #(row-snippet %) (create-map-of-events)
                            )))

Also I added some changes to the html file
<tbody title="events">
<tr title="event">
<td title="event-title">Event Title 1</td>
<td title="performer">Performer1</td>
<td title="date">Date1</td>
<td title="start-time">Start time1</td>
<td title="end-time">End time1</td>

</tr>
Cheers.

Joe Snikeris

unread,
Mar 29, 2012, 5:31:04 PM3/29/12
to enliv...@googlegroups.com
If you name your map keys exactly as you name your titles, you can define a transformation that will reduce a lot of the repetition found in your solution. I did something similar w/ the 'class-as-kw-content' transformation found here:

https://github.com/jsnikeris/mtg-store/blob/master/src/mtg_store/web/template.clj

--
You received this message because you are subscribed to the Google Groups "Enlive" group.
To view this discussion on the web visit https://groups.google.com/d/msg/enlive-clj/-/1dl4Qjb9U_gJ.

To post to this group, send email to enliv...@googlegroups.com.
To unsubscribe from this group, send email to enlive-clj+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/enlive-clj?hl=en.

Christophe Grand

unread,
Mar 30, 2012, 3:51:47 AM3/30/12
to enliv...@googlegroups.com
Using your latest HTML, something like this should also work (untested).
Doing something purely positional (without the title attributes) is doable but more verbose.

(def mapping
  {"event-title" :event-name
   "performer" :performers
   "date" :date
   "start-time" :start-time
   "end-time" :end-time})

(deftemplate indeks table-template [{:keys  [title event-data]}]
  [:title] (html/content title)
  [:tr (nth-child 2)] (clone-for [event (event-data)]
                        [:td] #(-> % :attrs :title mapping event)))

Christophe

--
You received this message because you are subscribed to the Google Groups "Enlive" group.
To view this discussion on the web visit https://groups.google.com/d/msg/enlive-clj/-/1dl4Qjb9U_gJ.

To post to this group, send email to enliv...@googlegroups.com.
To unsubscribe from this group, send email to enlive-clj+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/enlive-clj?hl=en.



--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

gwanwyn

unread,
Mar 30, 2012, 9:10:59 AM3/30/12
to enliv...@googlegroups.com
I changed one thing in the template bellow, and this is more elegant now :

(deftemplate indeks table-template [{:keys  [title event-data]}]
  [:title] (html/content title)
  [:tr (html/nth-child 2)] (html/clone-for [event event-data]
                        [:td] #(-> % :attrs :title mapping event)))

event-data was in brackets, and it shouldn't have been, it was giving null pointer exception. However, now it won't fill the tds with content. I am looking for the solution.

Joe, I will definitely try your solution.

Thank you both, and if you anyone can find the bug in the template above, I'd be most grateful.

Cheers

Vesna

On Thursday, December 22, 2011 8:41:52 PM UTC+1, gwanwyn wrote:
On Thursday, December 22, 2011 8:41:52 PM UTC+1, gwanwyn wrote:

Joe Snikeris

unread,
Mar 30, 2012, 9:13:27 AM3/30/12
to enliv...@googlegroups.com
Very nice.

What's the purpose of the (nth-child 2) selector-step in this case?

Christophe Grand

unread,
Mar 30, 2012, 9:16:39 AM3/30/12
to enliv...@googlegroups.com
Hi,

On Fri, Mar 30, 2012 at 3:10 PM, gwanwyn <vesna.p...@gmail.com> wrote:

event-data was in brackets, and it shouldn't have been, it was giving null pointer exception.

Indeed.
 
However, now it won't fill the tds with content. I am looking for the solution.

Doh! (and it's also a side effect from writing code right in the mail)



(deftemplate indeks table-template [{:keys  [title event-data]}]
  [:title] (html/content title)
  [:tr (html/nth-child 2)] (html/clone-for [event event-data]
                        [:td] (fn [td] (assoc td :content [(-> td :attrs :title mapping event str)]))))

I hope this works this time.

Christophe

Christophe Grand

unread,
Mar 30, 2012, 9:18:13 AM3/30/12
to enliv...@googlegroups.com
On Fri, Mar 30, 2012 at 3:13 PM, Joe Snikeris <j...@snikeris.com> wrote:
Very nice.

What's the purpose of the (nth-child 2) selector-step in this case?

oops, another error:


(deftemplate indeks table-template [{:keys  [title event-data]}]
  [:title] (html/content title)
  [[:tr (html/nth-child 2)]] (html/clone-for [event event-data]
                        [:td] (fn [td] (assoc td :content [(-> td :attrs :title mapping event str)]))))

 It is there to select (and clone) teh 2nd row.

Joe Snikeris

unread,
Mar 30, 2012, 9:22:06 AM3/30/12
to enliv...@googlegroups.com

Isn't there only one row in the template?

Christophe Grand

unread,
Mar 30, 2012, 9:25:59 AM3/30/12
to enliv...@googlegroups.com
On Fri, Mar 30, 2012 at 3:22 PM, Joe Snikeris <j...@snikeris.com> wrote:
>  It is there to select (and clone) teh 2nd row.

Isn't there only one row in the template?


Not from what I inferred  from his original code.

Joe Snikeris

unread,
Mar 30, 2012, 9:29:58 AM3/30/12
to enliv...@googlegroups.com

Ah, gotcha. Thanks for the solution and subsequent discussion.

gwanwyn

unread,
Mar 30, 2012, 9:38:10 AM3/30/12
to enliv...@googlegroups.com
I tried this

[[:tr (attr= :title "event")]] 

to select the row, but now it's completely out. Trying some more.... 


On Thursday, December 22, 2011 8:41:52 PM UTC+1, gwanwyn wrote:
On Thursday, December 22, 2011 8:41:52 PM UTC+1, gwanwyn wrote:
On Thursday, December 22, 2011 8:41:52 PM UTC+1, gwanwyn wrote:

gwanwyn

unread,
Apr 3, 2012, 12:17:51 PM4/3/12
to enliv...@googlegroups.com
I'd answer sooner, but was heavy with flu.
This is what deftemplate should look like, it works fine.

(deftemplate indeks table-template [{:keys  [title event-data]}]
  [:title] (html/content title)
  [[:tr (nth-child 2)]] (html/clone-for [event event-data]
                        [:td] (fn [td] (assoc td :content [(-> td :attrs :title mapping event)]))));show the page

Pierre-Henry Perret

unread,
Apr 17, 2012, 9:01:07 AM4/17/12
to enliv...@googlegroups.com
Hi gwanwyn,

I've tried indexks template with this event-data:
_________________________________
(def content-t
  {:title "Events Mashup"
   :event-data
              {:event-name "event name 1"
                :performer "performer 1"
                :date "date 1"
                :start-time "start time 1"
                :end-time "start time 1"}
   })
_______________________

And it doesnt work. Is there something incorrect in the data ?

Pierre-Henry Perret

unread,
Apr 17, 2012, 9:34:40 AM4/17/12
to enliv...@googlegroups.com

I've tried gwanwyn's code with that content:
__________________________
(def content-t
  {:title "Events Mashup"
   :cont "voici le contenu"
   :event-data
              {:event-name "event name 1"
                :performer "performer 1"
                :date "date 1"
                :start-time "start time 1"
                :end-time "start time 1"}
   })
______________________________

But it produces nothing.

Am I correct ?

Le mardi 3 avril 2012 18:17:51 UTC+2, gwanwyn a écrit :

gwanwyn

unread,
Apr 17, 2012, 9:48:38 AM4/17/12
to enliv...@googlegroups.com
You should put event-data in square brackets, like this
[{ :event-name "event name 1"
                         :performer "performer 1"
                         :date "date 1"
                          :start-time "start time 1"
                         :end-time "end time 1"}]
     
Hope this solves your problem...

Pierre-Henry Perret

unread,
Apr 17, 2012, 10:12:51 AM4/17/12
to enliv...@googlegroups.com
Nope.. yest not !

Does your template only contains a tr like that :
_________________________
<tr>
<td title="event-name">Event Title 1</td>
<td title="performer">Performer1</td>
<td title="date">Date1</td>
<td title="start-time">Start time1</td>
<td title="end-time">End time1</td>

</tr>
___________________

?

gwanwyn

unread,
Apr 17, 2012, 3:31:49 PM4/17/12
to enliv...@googlegroups.com
it works for me, my template contains 2 trs, one for the header, and the other for the row, here it is
<tr class="main">
<td >Event Title</td>
<td>Performer</td>
<td>Date</td>
<td>Start time</td>
<td>End time</td>
<tr title="event">
<td title="event-title">Event Title 1</td>
<td title="performer">Performer1</td>
<td title="date">Date1</td>
<td title="start-time">Start time1</td>
<td title="end-time">End time1</td>

</tr>

hope, you'll find the solution.

Pierre-Henry Perret

unread,
Apr 17, 2012, 4:10:03 PM4/17/12
to enliv...@googlegroups.com
Thanks, and sorry for the spam, the groups client told me a message was suppressed, so I wrote another...
which resulted in two posts!

gwanwyn

unread,
Apr 17, 2012, 4:13:26 PM4/17/12
to enliv...@googlegroups.com
everything is ok. bonne chance!
Reply all
Reply to author
Forward
0 new messages