2014-02-10 75 views
0

我在這裏創建了一個代碼,通過按下按鈕將數組內容存儲到列表視圖中,我的問題是listview內容會堆疊。我想每次按下按鈕清除以前的列表視圖內容,並能夠再次顯示更新的存儲陣列內容。清除列表視圖的內容

<div id="MainPage" data-role="page" > 

     <div data-role="content"> 

      <a href="#ViewPage" data-role="button" onClick="displayArray()">RENAME</a> 

     </div> 

    </div> 

    <div id="ViewPage" data-role="page" > 

     <div data-role="header" data-position="fixed"> 
      <a href="#MainPage" data-role="button" data-icon="back">BACK</a> 
      <h1>View Page</h1> 
     </div> 

     <div data-role="content"> 
      <ul id="viewlist" data-role="listview" data-filter="true" data-filter-placeholder="Sample Contents" data-inset="true"> 
        <!-- array contents goes here --> 
      </ul> 
     </div> 

    </div> 

    <script> 

     var sampleContent = new Array(); 

     displayArray() 
     { 
     //i want to place a code here that would clear the listview contents 
      for(var scan=0; scan<sampleContent.length; detect++) 
      { 
       //looping of the array contents into the listview 
       $('#viewlist').append('<li><a href="#">' + sampleContent[scan] + '</a></li>').listview("refresh"); 
      }  
     } 

    </script> 

我被這個問題困住了,任何幫助或建議將很樂意接受預先感謝。

+0

可以顯示你的'sampleContent()'函數 –

+0

sampleContent是我用來存儲數據到列表視圖的數組。我想在for循環之前清除displayArray()函數中的listview。我試過('#viewlist')。empty();但之後不再添加listview內容。 –

回答

1

這應該做的工作。只需添加所有數據,然後清空列表並追加所有字符串以獲得更快的結果。

displayArray() 
    { 
    //i want to place a code here that would clear the listview contents 
    var data='';  
    for(var scan=0; scan<sampleContent.length; detect++) 
     { 
      data+='<li><a href="#">' + sampleContent[scan] + '</a></li>'; 
     } 
    $("#viewlist").empty().append(data);   
    } 
+0

這可行,但listview設計在第二次輸入後觸發displayArray()函數的第二個實例消失。這是我的輸出的截圖。 http://i.imgur.com/IN7UIOA.jpg –

+0

只需將.listview(「refresh」)添加到最後一行的末尾:$(「#viewlist」)。empty()。append(data).listview( 「刷新」); – ezanker

0

我最近與清單的工作,這是我來到這個:

displayArray() 
    { 
    //Emptys viewlist 
    $('#viewlist').empty(); 
    //i want to place a code here that would clear the listview contents 
     for(var scan=0; scan<sampleContent.length; detect++) 
     { 
      //looping of the array contents into the listview 
      $('#viewlist').append('<li><a href="#">' + sampleContent[scan] + '</a></li>'); 
     } 
     //Update list 
     $("#viewlist").listview("refresh"); 
    } 

必須清空,然後追加更新。

+0

它不起作用,列表視圖保持空白。 –