Creating a Calendar like table using underscore an

2019-09-19 12:49发布

问题:

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