制表符 - 如何从 JSON 文件中删除一行?
Tabulator - How can I delete a row from JSON file?
我正在使用 Tabulator 创建 table。我使用 ajaxURL: "test.json"
从 JSON
文件导入信息。数据全部加载正常。我创建了一个表单来输入数据。提交后,它会转到处理数据的 PHP
文件(解码 test.json,
添加表单数据,编码为 json
)。一切正常,直到我尝试实现一种删除方式。我尝试了几种方法,其中 none 行得通。一种方法是在我的 json
数组中创建嵌套数组,而 Tabulator 不会读取它。我希望能够单击删除列中的按钮交叉并从 table 和 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";
}
?>
检查 documentation 您需要调用 API(如 node.js)从 json 文件
中删除
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
});
我正在使用 Tabulator 创建 table。我使用 ajaxURL: "test.json"
从 JSON
文件导入信息。数据全部加载正常。我创建了一个表单来输入数据。提交后,它会转到处理数据的 PHP
文件(解码 test.json,
添加表单数据,编码为 json
)。一切正常,直到我尝试实现一种删除方式。我尝试了几种方法,其中 none 行得通。一种方法是在我的 json
数组中创建嵌套数组,而 Tabulator 不会读取它。我希望能够单击删除列中的按钮交叉并从 table 和 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";
}
?>
检查 documentation 您需要调用 API(如 node.js)从 json 文件
中删除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
});