我正在使用Tabulator创建表。我使用JSON
从ajaxURL: "test.json"
文件导入信息。数据全部加载正常。我已经创建了一个表格来输入数据。提交后,将转到处理数据的PHP
文件(解码test.json,
添加表单数据,编码为json
)。一切正常,直到我尝试实现一种删除方式。我尝试了几种方法,但没有一种起作用。一种方法是在我的json
数组中创建嵌套数组,而Tabulator不会读取它。我希望能够在“删除”列中单击“按钮”,然后从表和JSON
文件中删除行。
总有没有删除JSON
文件元素而不嵌套数组的情况?
tabulator.html
<form action="process.php" method="POST">
Name:
<input type="text" name="name">
Description:
<input type="text" name="descript">
<br><br>
Latitude:
<input type="text" name="lat">
Longitude:
<input type="text" name="lon">
<br><br>
Start Date/Time:
<input type="date" name="startDate">
<input type="time" name="startTime">
<br><br>
End Date/Time:
<input type="date" name="endDate">
<input type="time" name="endTime">
<br><br>
<input type="hidden" name="deleteStatus" value="">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
//later in the file
columns:[ //define the table columns
{title:"Delete", field:"deleteStatus", formatter:"buttonCross", width:50, align:"center", headerSort:false, cellClick:function(e, cell){
if(confirm('Are you sure you want to delete this entry?')) {
cell.setValue("delete");
var row = cell.getRow();
$.ajax({
url: 'process.php',
method: 'post',
data: {row},
});
}
}},
process.php
<?php
$myFile = "test.json";
$arr_data = array(); // create empty array
$time = date("YmdHis");
try
{
//Get form data
$formdata = array(
'id'=> $time,
'deleteStatus'=> $_POST['deleteStatus'],
'name'=> $_POST['name'],
'descript'=> $_POST['descript'],
'lat'=> $_POST['lat'],
'lon'=> $_POST['lon'],
'startDate'=> $_POST['startDate'],
'startTime'=> $_POST['startTime'],
'endDate'=> $_POST['endDate'],
'endTime'=> $_POST['endTime'],
);
//Get data from existing json file
$jsondata = file_get_contents($myFile);
// converts json data into array
$arr_data = json_decode($jsondata, true);
// Push user data to array
array_push($arr_data,$formdata);
foreach($arr_data as $elementKey => $element) {
foreach($element as $valueKey => $value) {
if($valueKey == 'deleteStatus' && $value == 'delete'){
//delete this particular object from the $array
unset($arr_data[$elementKey]);
}
}
}
//Convert updated array to JSON
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
//write json data into data.json file
if(file_put_contents($myFile, $jsondata)) {
header("Refresh:0; url = tabulator.html");
}
else
echo "error";
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
答案 0 :(得分:1)
检查documentation,您需要调用从json文件删除的API(例如node.js)
table.deleteRow(15)
.then(function(){
//Call your api to delete from JSON file
})
.catch(function(error){
//handle error deleting row
});
row.delete()
.then(function(){
//run code after row has been deleted
})
.catch(function(error){
//handle error deleting row
});