我有一个文本文件。有些数据需要制成表格,有些则不需要。如果我逐行读取文本文件,那么如果我逐行读取,如何设置html表并将数据制成表格呢?我知道这可能是一个愚蠢的问题,但我有点卡住了。
文本文件(.txt):
Chief Justice
Vote for 3
John Doe . . . . . . . . . . 89,000 33.00
Jane Doe . . . . . . . . . . 64,000 24.35
Jim Doe . . . . . . . . . . 24,000 9.09
Sally Roe . . . . . . . . . . 8,000 3.23
Next Office
Vote for 1
Jane Jones . . . . . . . . 18,000 19.13
Bob Jones . . . . . . . . 77,000 80.58
etc....
html: <div id="mydiv"></div>
jquery: var file = "myfile.txt";
$.get(file,function(data){
var lines = data.split("n");
for (var i = 0, len = lines.length; i < len; i++) {
if (lines[i].indexOf(". .") < 0) {
$('#mydiv').append(lines[i] + "<br/>");
} else {
//??
//need to feed these lines into an array
//and parse them
}
}
});
我将这些行推入数组:
$.get(file,function(data){
var lines = data.split("n");
for (var i = 0, len = lines.length; i < len; i++) {
var myArray= [];
if (lines[i].indexOf(". .") < 0) {
$('#mydiv').append(lines[i] + "<br/>");
if (myArray.length > 0) {
//there is something in the array
$('#mydiv').append("<table>");
for (var j = 0; j<myArray.length; j++) {
//parse, etc., here
}
$('#mydiv').append("</table>");
myArray= [];
}
} else {
myArray.push(lines[i]);
}
}
});