2013-04-25 51 views
1

我的jQuery代碼以取得名稱,代碼和類別:如何放置在不同的ListView?

 $(req.responseText).find("Table").each(function(i) { 
       var item = $(this), 
       name = item.find('Name').text(), 
       code = item.find('Code').text(), 
       catg = item.find('Category').text();      

       var $content = $('<li><a href="#"><img src="../../img/album-bb.jpg"><h3>Name: '+ name + '</h3><p>Code: '+ code + '</p><p>Category: '+ catg + '</p></a><a href="#purchase" data-rel="popup" data-position-to="window" data-transition="pop">Add to favorites</a></li>');        
       $('#RecipeList').append($content).listview('refresh'); 



     }); 

這是我的我的XML的樣本響應:

<Table diffgr:id="Table1" msdata:rowOrder="0"> 
    <Code>106377</Code> 
    <Name>Blackened red snapper</Name> 
    <Category>123</Category> 
    <Yield>4</Yield> 
    <YieldUnit/> 
</Table> 
<Table diffgr:id="Table2" msdata:rowOrder="1"> 
    <Code>303570</Code> 
    <Name>Celery soup</Name> 
    <Category>123</Category> 
    <Yield>1</Yield> 
    <YieldUnit/> 
</Table> 
<Table diffgr:id="Table3" msdata:rowOrder="2"> 
    <Code>303675</Code> 
    <Name>Challah French Toast</Name> 
    <Category>123</Category> 
    <Yield>6</Yield> 
    <YieldUnit/> 
    </Table> 

ListView控件輸出中是這樣的:

Name: Blackened red snapperCelerySoupChallah French toast 
Code: 106377303570303675 
Category: 123123123 

如何放在不同的列表視圖?..當它append.Thanks

+0

什麼是你不同的listview?你的問題是什麼? – Gajotres 2013-04-25 07:54:32

+0

我的意思是列表視圖的輸出是這樣的:名稱:燻烤紅魚代碼:106377類別:123名稱:芹菜湯代碼:303570類別:123名稱:challah的法式吐司代碼:303675類別123 – 2013-04-25 07:56:57

+0

其格式類似於列表視圖,但所有數據僅在一(1)個列表視圖中,而不是三(3)列表視圖預計存在 – 2013-04-25 08:01:00

回答

1

試試這個:

var ni = 0; 
var ci = 0; 
var cati = 0; 
var NameArr = new Array(); 
var CodeArr = new Array(); 
var CatArr = new Array(); 

$(req.responseText).find('Name').each(function() { 
    NameArr[ni] = $(this).text(); 
    ni++; 
} 

$(req.responseText).find('Code').each(function() { 
    CodeArr[ci] = $(this).text(); 
    ci++; 
} 

$(req.responseText).find('Category').each(function() { 
    CatArr[cati] = $(this).text(); 
    cati++; 
} 

for(var i=0; i<NameArr.length; i++){ 
    var $content = $('<li><a href="#"><img src="../../img/album-bb.jpg"><h3>Name: '+ NameArr[i] + '</h3><p>Code: '+ CodeArr[i]+ '</p><p>Category: '+ CatArr[i]+ '</p></a><a href="#purchase" data-rel="popup" data-position-to="window" data-transition="pop">Add to favorites</a></li>');        
    $('#RecipeList').append($content).listview('refresh'); 
} 
+0

好男人!謝謝! – 2013-04-25 08:17:31

+0

歡迎您。如果我的回答很有用,請點贊,我可以使用一些要點;-) – venki 2013-04-25 08:24:32

1

工作例如:http://jsfiddle.net/Gajotres/uzphF/

$('#index').live('pagebeforeshow',function(e,data){  
    $('#table-data').empty(); 
    $.ajax({ 
     type: "POST", 
     url: "/echo/xml/", 
     dataType: "xml", 
     data: { 
      xml: '<Tables><Table><Code>106377</Code><Name>Blackened red snapper</Name><Category>123</Category><Yield>4</Yield><YieldUnit/></Table><Table><Code>303570</Code><Name>Celery soup</Name><Category>123</Category><Yield>1</Yield><YieldUnit/></Table><Table><Code>303675</Code><Name>Challah French Toast</Name><Category>123</Category><Yield>6</Yield><YieldUnit/></Table></Tables>' 
     }, 
     success: function(xml) { 
      ajax.parseXML(xml); 
     }, 
     error: function (request,error) { 
      alert('Network error has occurred!'); 
     } 
    }); 
}); 

var ajax = { 
    parseXML:function(result){ 
     $(result).find("Table").each(function(){ 
      var code = $(this).find('Code').text(); 
      var name = $(this).find('Name').text(); 
      var cat = $(this).find('Category').text(); 
      var yield = $(this).find('Yield').text(); 

      $('#table-data').append('<li><a href="#cars"><h3>Table code:<span> '+code+'</span></h3><p>Table name: ' + name + '</p></a></li>'); 
     });  
     $('#table-data').listview('refresh'); 
    } 
} 

本例使用樣機的jsfiddle功能測試XML AJAX調用,但是這部分是不是必需的。只有你需要知道的是如何循環訪問XML。還有一件事,你的XML例子必須包裝在父標籤中,我將它命名爲

相關問題