te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Loading a CSV file into an HTML table using javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Loading a CSV file into an HTML table using javascript - Stack Overflow

programmeradmin3浏览0评论

I want to create a webpage that loads a selected CSV file (from hard drive) and displays its contents using table HTML.

The project incorporates two ponents and so far, I've been researching the latter; generating a table out of a nested array in javascript.

For some reason, the columns do not appear the way they should.

My code

<!DOCTYPE html>
<html>
<body>

<table id="1"> </table>

<button onclick="createTable()">Create</button>

<script>
function createTable() {
    var array = [[1,2,3],[4,5,6],[7,8,9]];
	document.getElementById("1").innerHTML = ""; //Clear.
	
	for (i = 0; i < array.length; i++) { 
	document.getElementById("1").innerHTML += "<tr>";
		for (k = 0; k < array[0].length; k++) {
		  document.getElementById("1").innerHTML += "<td>" + array[i][k] + "</td>" ;
		}
		document.getElementById("1").innerHTML += "</tr>";
	  }
}
</script>

</body>
</html>

I want to create a webpage that loads a selected CSV file (from hard drive) and displays its contents using table HTML.

The project incorporates two ponents and so far, I've been researching the latter; generating a table out of a nested array in javascript.

For some reason, the columns do not appear the way they should.

My code

<!DOCTYPE html>
<html>
<body>

<table id="1"> </table>

<button onclick="createTable()">Create</button>

<script>
function createTable() {
    var array = [[1,2,3],[4,5,6],[7,8,9]];
	document.getElementById("1").innerHTML = ""; //Clear.
	
	for (i = 0; i < array.length; i++) { 
	document.getElementById("1").innerHTML += "<tr>";
		for (k = 0; k < array[0].length; k++) {
		  document.getElementById("1").innerHTML += "<td>" + array[i][k] + "</td>" ;
		}
		document.getElementById("1").innerHTML += "</tr>";
	  }
}
</script>

</body>
</html>

Share Improve this question edited Dec 28, 2015 at 13:05 CodeWizard 142k22 gold badges159 silver badges179 bronze badges asked Dec 28, 2015 at 13:02 JohnJohn 3531 gold badge5 silver badges16 bronze badges 1
  • A succinct solution based on D3.js: bl.ocks/ndarville/7075823 – Anton Tarasenko Commented Nov 12, 2019 at 2:23
Add a ment  | 

2 Answers 2

Reset to default 6

Save the table contents to a variable first and set it to the innerHTML afterwards. Everytime you add a <tr> (or any not singleton tag for that matter), the browser immediately renders the closing tag.

Try this:

function createTable() {
    var array = [[1,2,3],[4,5,6],[7,8,9]];
    var content = "";
    array.forEach(function(row) {
        content += "<tr>";
        row.forEach(function(cell) {
            content += "<td>" + cell + "</td>" ;
        });
        content += "</tr>";
    });
    document.getElementById("1").innerHTML = content;
}

Because you are planning on using the FileReader API, IE9 support is off the table anyways. Updated the above function to use the 'newer' forEach array function


ADDENDUM

To load a file with javascript you have to use the FileReader HTML5 API. I can give you some pointers as to how you should go about doing this. This is all untested code, but it gives you a little idea what to do

1.Create a input field

<input type="file" id="file" name="file">

2.Trigger a response on change of this input

var file = document.getElementById('file');
file.addEventListener('change', function() {
    var reader = new FileReader();
    var f = file.files[0];
    reader.onload = function(e) {
        var CSVARRAY = parseResult(e.target.result); //this is where the csv array will be
    };
    reader.readAsText(f);
});

3.Parse the result to an array by using split/push. This uses \n as row delimiter and , as cell delimiter.

function parseResult(result) {
    var resultArray = [];
    result.split("\n").forEach(function(row) {
        var rowArray = [];
        row.split(",").forEach(function(cell) {
            rowArray.push(cell);
        });
        resultArray.push(rowArray);
    });
    return resultArray;
}

(or you can use a third party plugin which will parse the CSV for you: papa parse, for instance)

After some long searching, this is probably the most simple and functional script that I could ever e across, also available for free. Although it doesn't deal with the array modification directly, it's easy to edit and elicit your desired results.

发布评论

评论列表(0)

  1. 暂无评论