我正在尝试在代码中使用数据表库,但是在JavaScript控制台中却不断收到SyntaxError:Unexpected token'{'错误。
我试图在线找到解决方案,但发现我的JavaScript可能不正确,但是javascript没有显示错误。
我已经在代码中嵌入了这两个库,但是似乎在jQuery.dataTable.min.css中有错误。
https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css
https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js
下面是代码
我们将不胜感激:)
<html>
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<button id="btn-show-all-children" type="button">Expand All</button>
<button id="btn-hide-all-children" type="button">Collapse All</button>
<table class ="display table" id="myTable" align="center" <table class="display nowrap" width="100%">
<thead>
<tr>
<th> ID </th>
<th> START </th>
<th> END </th>
<th> QUALIFICATION </th>
<th class="none">ID</th>
<th class="none">NAME</th>
</tr>
</thead>
<?php
function _isCurl(){
return function_exists('curl_version');
}
if (_iscurl()){
//curl is enabled
$url = "http://18.130.253.44/getCourses.php?action=getUnpaid";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$output = substr($output , strpos($output, "{"));
$stringreplaced = str_replace(');', '', str_replace('\\', '', $output));
$decoded = json_decode($stringreplaced, true);
if (count($decoded['courses'])) {
?>
<tbody>
<?php
// Open the table
foreach($decoded['courses'] as $classarray){
foreach ($classarray['students']as $key1) {
?>
<tr>
<td><?php echo $classarray['id'];?> </td>
<td><?php echo $classarray['start'];?> </td>
<td><?php echo $classarray['end'];?> </td>
<td><?php echo $classarray['qualification'];?> </td>
<td><?php echo $key1['id'];?> </td>
<td><?php echo $key1['name'];?> </td>
</tr>
<?php
}
?>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
<?php
}
?>
<script src="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
var table = $('#myTable').DataTable({
'responsive': true
});
// Handle click on "Expand All" button
$('#btn-show-all-children').on('click', function(){
// Expand row details
table.rows(':not(.parent)').nodes().to$().find('td:first-child').trigger('click');
});
// Handle click on "Collapse All" button
$('#btn-hide-all-children').on('click', function(){
// Collapse row details
table.rows('.parent').nodes().to$().find('td:first-child').trigger('click');
});
});
</script>
</html>
答案 0 :(得分:0)
我们使用标签<link>
调用文件css,并使用标签<script>
调用js脚本
您需要更改此行
<script src="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"></script>
作者:
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.20/datatables.min.css"/>