从文本文件加载 table 数据
Load table data from text file
我的 table 数据存储在名为 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"},
];
设置数据时如何调用文本文件。我尝试了下面的方法并构建了 table 结构,但数据未加载,但在 table.
中显示错误
//load sample data into the table
table.setData("../textfiles/test_array.txt");
在我的浏览器控制台中,我收到以下错误消息。
Ajax 加载错误:语法错误:"JSON.parse: expected property name or '}' at line 2 column 2 of the JSON data"
这是我的整个脚本
<!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>
你的代码第 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"}
]
您是否在浏览器网络开发者工具中检查过响应是什么?您的服务器也可能没有返回您期望的文件,检查网络工具应该能让您确认返回的内容
我在键上使用双引号让它工作
[
{"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"}
]
我的 table 数据存储在名为 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"},
];
设置数据时如何调用文本文件。我尝试了下面的方法并构建了 table 结构,但数据未加载,但在 table.
中显示错误 //load sample data into the table
table.setData("../textfiles/test_array.txt");
在我的浏览器控制台中,我收到以下错误消息。
Ajax 加载错误:语法错误:"JSON.parse: expected property name or '}' at line 2 column 2 of the JSON data"
这是我的整个脚本
<!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>
你的代码第 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"}
]
您是否在浏览器网络开发者工具中检查过响应是什么?您的服务器也可能没有返回您期望的文件,检查网络工具应该能让您确认返回的内容
我在键上使用双引号让它工作
[
{"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"}
]