【问题标题】:Tabulator - How can I delete a row from JSON file?Tabulator - 如何从 JSON 文件中删除一行?
【发布时间】:2019-05-03 04:56:09
【问题描述】:

我正在使用 Tabulator 创建表格。我使用ajaxURL: "test.json"JSON 文件导入信息。数据都加载正常。我创建了一个表格来输入数据。提交后,它转到处理数据的PHP 文件(解码test.json, 添加表单数据,编码为json)。一切正常,直到我尝试实施一种删除方式。我尝试了几种方法,但都没有奏效。一种方法是在我的 json 数组中创建嵌套数组,而 Tabulator 不会读取它。我希望能够单击 Delete 列中的 buttonCross 并从表和 JSON 文件中删除该行。

有没有在不嵌套数组的情况下删除JSON文件元素?

制表符.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},
                        });
                    }
                }},

进程.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";
}
?>

【问题讨论】:

  • 制表器有用于操作行的 API。向我们展示您的代码,以便我们了解发生了什么
  • 用一些代码编辑了我的帖子。

标签: json delete-row tabulator


【解决方案1】:

检查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
});

【讨论】:

  • 我最终将代码更改为使用 MySQL 数据库,然后退出 JSON 文件。似乎工作得更好。感谢您回答我在 Tabulator 上的所有问题。
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2020-10-15
  • 1970-01-01
  • 2022-11-16
相关资源
最近更新 更多