2012-06-26 44 views
0

我已經看到了幾個關於jQuery模板的教程,據我瞭解是outdataed,所以我試圖使用。克隆我得到它工作正常,當我只顯示一個結果,但當我想要顯示完整的結果列表時,它不起作用,我相信因爲我的糟糕的jQuery。我想克隆整個.template類從響應陣列中的每個成員填寫,然後添加每個新克隆的模板#fillresultsdiv,如果有人能幫助找到我在做什麼錯jQuery的克隆與HTML模板

這裏是jQuery的:

    success: function (msg) { 
        var events = []; 
        var obj = $.parseJSON(msg.d); 
        $(obj.res).each(function() { 
         var newRow = $('.template').clone() 
         newRow.$('.day').text($(this).attr('day')), 
          newRow.$('.dayofweek').text($(this).attr('dayofweek')), 
          newRow.$('.month').text($(this).attr('month')), 
          newRow.$('.title').text($(this).attr('title')), 
          newRow.$('.time').text($(this).attr('time')), 
         newRow.$('.venue').text($(this).attr('venue')), 
         newRow.$('.description').text($(this).attr('description')) 
         $('#fillresultsdiv').append(newRow); 

下面是一個簡單的迴應:

d: "{"res":[{"day":"26","dayofweek":"Tue","month":"Jun","title":"Glen Hansard","venue":"Vic Theatre","time":"7:00 PM","ticketurl":"http://seatgeek.com/glen-hansard-t 

,這裏是我的模板HTML:

<div class="Template"> 
     <div class="accordian_head1"> 
     <div class="date_container"> 
      <a class="day"></a><br/><br/> 
      <a class="dayofweek"></a><br/> 
      <a class="month"></a> 
     </div> 
     <div class="title_container"> 
      <a class="title">Title</a> 
      <a class="venue"><br/></a><a class="time"></a> 
     </div> 
     <div class="links"> 
      <a href=" + dr(36).ToString() + ?aid=854">Buy Tickets</a><br/> 
      <a href="javascript:void(0)" onclick="fnAddToCalendar({ 'eventID' : ' dr(0).ToString() + '});">Watch</a><br/> 
     <a href="#">Email</a><br/> 
     <a href=""Calendar.aspx"">Calendar</a><br/> 
    </div> 
    </div> 
    <div class="accordian_body1new"><a class="description"></a> 
    </div> 

這是所有#fillresultsdiv是

 <div id="fillresultsdiv"></div> 
+1

你在html中的#fillresultsdiv在哪裏? – ewein

回答

1
// Parse the entire string, because `msg` is not an object, 
// so you can't use `msg.d` 
var obj = $.parseJSON(msg); 

$(obj.d.res).each(function() { 
    var newRow = $('.template').clone(); 

    // Now loop through the object 
    for (var prop in this) { 
     if (this.hasOwnProperty(prop)) { 

      // Lucky for you, the keys match the classes :) 
      $('.' + prop, newRow).text(this[ prop ]); 
     } 
    } 
    $('#fillresultsdiv').append(newRow); 
}); 

但你絕對應該使用DocumentFragment加快DOM操作,並立即追加一切。因爲請記住:DOM是緩慢的

var obj = $.parseJSON(msg); 

// Create a DocumentFragment 
var el = document.createDocumentFragment(); 
$(obj.d.res).each(function() { 
    var newRow = $('.template').clone(); 
    for (var prop in this) { 
     if (this.hasOwnProperty(prop)) { 
      $('.' + prop, newRow).text(this[ prop ]); 
     } 
    } 

    // Append to the DocumentFragment 
    $(el).append(newRow); 
}); 

// And append the DocumentFragment to the DIV 
$('#fillresultsdiv').append(el); 
+0

這看起來像一個更好的方式來做到這一點,我不能讓它工作,但它是通過if(this.hasOwnProperty(道具)){和道具設置爲正確的屬性,但之後沒有什麼是越來越添加到'#fillresultsdiv' - 即使$('。'+ this,newRow).text(this [prop]);我沒有做任何事情,我應該添加黑色模板,這裏什麼都沒有添加 –

+0

@ScottSelby是的,我改正使用'$('。'+ prop)'而不是'$('。'+ this)',我的壞。 'this'不是一個字符串,而是一個對象,這就是它失敗的原因。 –

+0

好吧,我有點工作,出於某種原因,雖然它一遍又一遍地顯示相同的結果,我仔細檢查JSON和每個事件只接收一次,但在這個循環 - 我顯示第一個結果一次,第二次兩次,第三次四次,第四次八次,第五次十六次,它變成了一個奇怪的二進制數學問題 –

1

試試這個:

  success: function (msg) { 
       var events = []; 
       var obj = $.parseJSON(msg.d); 
       $(obj.res).each(function() { 
        var newRow = $('.template').clone(); 
        newRow.find('.day').text($(this).attr('day')); 
        newRow.find('.dayofweek').text($(this).attr('dayofweek')); 
        newRow.find('.month').text($(this).attr('month')); 
        newRow.find('.title').text($(this).attr('title')); 
        newRow.find('.time').text($(this).attr('time')); 
        newRow.find('.venue').text($(this).attr('venue')); 
        newRow.find('.description').text($(this).attr('description')); 
        newRow.appendTo('#fillresultsdiv'); 
        } 
+0

沒有工作 - 我不太確定newRow。$('。day')。text($(this).attr('day'))是否合適jQuery –

+0

@ewein您是開發人員。當你看到這麼多的重複時,試着去做一些事情:)另外,使用逗號代替分號不會幫助你的代碼。 –

+0

我想給出一個類似於他的方法的答案,所以他會更容易理解。是的,逗號必須離開,直到現在我都沒有注意到它們。 – ewein