Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

javascript - Creating a Calendar like table using underscore and backbone js templates

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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...