Creating a Calendar like table using underscore and backbone js templates

*爱你&永不变心* 提交于 2019-12-13 09:23:08

问题


Hi Guys I am a total noob at backbone and underscore. I need to create a table that shows appointment bookings for some dates. I have to make it look something like this:

This is what my collection looks like:

    [
    {"id":0, "startDate":"04/11/2013", "serviceID":241, "providerID":223, "timeSlots": ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"]},
    {"id":0, "startDate":"05/11/2013", "serviceID":241, "providerID":223, "timeSlots": ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"]}
]

What I am doing currently in my underscore template is:

<div class="table-responsive">
    <div class="row">

    </div>
    <table id="stbl" class="table table-striped table-condensed table-bordered">
        <% _.each(slots, function(slot) { %>
              <tr>
                <td>
                   <strong> <%- slot.startDate %> </strong>
                </td>
                <% _.each(slot.timeSlots, function(t) { %>
                    <td>
                       <button id="timeslot" data-provider="<%- slot.providerID %>" data-time="<%- t %>" data-date="<%- slot.startDate %>" class="btn btn-small btn-blue"><span><%- t %></span></button>
                    </td>
                <% }); %>
               </tr>
         <% }); %>
</table>
</div>

What changes do I make to my template to give it a structure like the picture above?

Thanks


回答1:


Your data structure is a little tedious to play with. I would suggest doing 2 passes in your template to build the table header & body separately.

Here's what I'm trying to say:

// template start 
<table>
  <thead>
    /* ...code to create header */
  </thead>
  <tbody>
  /* ... code to create row entries */
  </tbody>
</table>
// template end

fiddled it for you. Hope you can take it further from here.

Good Luck



来源:https://stackoverflow.com/questions/19773136/creating-a-calendar-like-table-using-underscore-and-backbone-js-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!