从文本文件加载表数据

时间:2018-10-14 18:58:42

标签: tabulator

我的表格数据存储在名为test_array.txt的文本文件中

[
{id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
{id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
{id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
{id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
]; 

setData时如何调用文本文件。我在下面尝试过,并且表结构已构建,但是数据未加载,但表中显示错误。

       //load sample data into the table
    table.setData("../textfiles/test_array.txt");

在我的浏览器控制台中,收到以下错误消息。

Ajax加载错误:语法错误:“ JSON.parse:JSON数据的第2行第2列的预期属性名称或'}'”

这是我的整个剧本

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Johnson County Pharmacy Association, Johnson County, Iowa" />
<meta name="keywords" content="Johnson County Iowa Pharmacy Association pharmacist technician" />
<link href="../css/main.css" rel="stylesheet" />
<link href="../css/tabulator.css" rel="stylesheet" />
<link href="../css/tabulator.css.map" rel="stylesheet" />
<script type="text/javascript" src="../js/tabulator.js"></script>
<title>JCPA</title>
</head>

<body>
<div id="tblwrap">
<h2>Meeting Information Editor</h2>

    <div id="example-table"></div>

    <script>
        //create Tabulator on DOM element with id "example-table"
        var table = new Tabulator("#example-table", {
     	height:205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
     	layout:"fitDataFill",//fit columns to fit data and width of table (optional)
		//data:tableData, //set initial table data
     	columns:[ //Define Table Columns
    	 	{title:"Name", field:"name", width:150},
    	 	{title:"Age", field:"age", align:"left", formatter:"progress"},
    	 	{title:"Favourite Color", field:"col"},
    	 	{title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
     	],
     	rowClick:function(e, row){ //trigger an alert message when the row is clicked
     		alert("Row " + row.getData().id + " Clicked!!!!");
     	},
        });
      
        //load sample data into the table
    table.setData("../textfiles/test_array.txt");
    </script> 
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

我使用按键上的双引号使它起作用

[
{"name":"Oli Bob", "age":"12", "col":"red", "dob":""},
{"name":"Mary May", "age":"1", "col":"blue", "dob":"14/05/1982"},
{"name":"Christine Lobowski", "age":"42", "col":"green", "dob":"22/05/1982"},
{"name":"Brendon Philips", "age":"125", "col":"orange", "dob":"01/08/1980"},
{"name":"Margret Marmajuke", "age":"16", "col":"yellow", "dob":"31/01/1999"}
]

答案 1 :(得分:0)

您在代码的第6行上有一个无效的结尾逗号,id为5的行的末尾不应带有逗号,并且在数组后也不需要分号,它是无效的JSON。应该是:

[
{id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
{id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
{id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
{id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
{id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"}
]

您是否已在浏览器网络开发人员工具中检查了响应,这是什么?您的服务器也可能未返回您期望的文件,签入网络工具应该可以让您确认返回的信息