0

我的表数据存储在名为 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 时如何调用文本文件。我尝试了以下方法并构建了表结构,但未加载数据,但在表中显示 ERROR。

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

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

Ajax 加载错误:SyntaxError:“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>

4

2 回答 2

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"}
]

于 2018-10-15T23:31:01.857 回答
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"}
]

您是否在浏览器网络开发人员工具中检查过响应是什么?您的服务器也可能没有返回您期望的文件,检查网络工具应该可以让您确认返回的文件

于 2018-10-15T17:15:43.117 回答