2017-03-10 41 views
1

我有一個按鈕,當單擊該按鈕時,此文件的內容將列出它們到一個表中。有人可以給我一個想法如何做到這一點? TIA如何將數據列表到表

+0

的可能的複製[如何從文本文件加載數據到HTML表(http://stackoverflow.com/questions/33006410/how-to-load-the-data-into-html-從一個文本文件) –

+0

好吧謝謝你的啓發我,我一直在尋找它昨天我找不到。謝謝@SaravananSachi – Princess

回答

0
var tableContent = ''; 


$.get('file path', function(data) { 

    //this will split the string into array line by line 
    var lineByline = data.split('\n'); 
    //here we're itraing the array which you've created and printing the values 
    $.each(lineByline , function(key,value){ 
     tableContent += '<tr>'; 
     tableContent += '<td>' + value + '</td>'; 
     tableContent += '</tr>'; 
    }); 

    $('#listHere').html(tableContent); 
}); 
1

檢查這個代碼,

<table id="listHere"> 

</table> 

您不能從您的本地系統訪問文件。將文本文件放在您的服務器上。

var txtData = new Array(); 
$.get('passwords.txt', function (data) { 
    txtData = data.split('\n'); 
    var htmTable = ""; 
    for (var i = 0; i < txtData.length; i++) { 
     htmTable = htmTable + "<tr><td>" + txtData[i] + "</td></tr>"; 
    } 
    $('#listHere').append(htmTable); 
});