简体   繁体   English

我如何创建一个动态的 React 表来操作一个动态的 JSON 文件?

[英]How do I create a dynamic React table that also manipulates a dynamic JSON file?

I have seen a lot of examples and tutorials for dynamic React tables, and also examples that import JSON data into a react table, but I have not yet found one that dynamically changes a React table and a JSON file.我看过很多动态 React 表的示例和教程,还有将 JSON 数据导入 React 表的示例,但我还没有找到动态更改 React 表和 JSON 文件的示例。

I am basing my code off this one ( https://codesandbox.io/s/k9r9692m77 ), which is the accepted answer to a StackOverflow post ( Add and remove data to table - React )我的代码基于这个( https://codesandbox.io/s/k9r9692m77 ),这是对 StackOverflow 帖子( 添加和删​​除数据到表 - 反应)的公认答案

I would like to be able to submit data through the UI with a "submit" button like the one in the example, and that data be displayed in a UI table, but I would also like the data that is added to the table to also be added to a JSON file.我希望能够使用示例中的“提交”按钮通过 UI 提交数据,并将该数据显示在 UI 表中,但我也希望添加到表中的数据也可以添加到 JSON 文件中。

Example:例子:

There are two fields, one for the USER and one for EMAIL , so for two entries I would get a table like the one below:有两个字段,一个用于USER ,一个用于EMAIL ,因此对于两个条目,我会得到一个如下所示的表:

|------------------|-------------------|------------------|
|       USER       |      EMAIL        |       Actions    |
|   Bennie Factor  | bennie@factor.com |       Delete     |
|   Horace Cope    | horace@cope.com   |       Delete     |     
|------------------|-------------------|------------------|

Which would then produce a JSON that looks something like:然后会生成一个类似于以下内容的 JSON:

{"data": [
    {"name": "Bennie Factor", "email": "bennie@factor.com"},
    {"name": "Horace Cope", "email": “horace@cope.com"}
]}

If I then added the user Jay Walker with an email of jay@walker.com and clicked submit, the new table and json would be:如果我然后添加用户 Jay Walker 并使用电子邮件 jay@walker.com 并单击提交,则新表和 json 将是:

|------------------|-------------------|------------------|
|       USER       |      EMAIL        |       Actions    |
|   Bennie Factor  | bennie@factor.com |       Delete     |
|   Horace Cope    | horace@cope.com   |       Delete     |
|   Jay Walker     | jay@walker.com    |       Delete     |     
|------------------|-------------------|------------------|

And:和:

 {"data": [
    {"name": "Bennie Factor", "email": "bennie@factor.com"},
    {"name": "Horace Cope", "email": “horace@cope.com"},
    {"name": "Jay Walker", "email": “jay@walker.com"}
]}

The process would be:该过程将是:

  1. You type data into the relevant boxes您在相关框中键入数据
  2. Click submit and that data is then displayed in a table, and also appended to a JSON file单击提交,然后该数据将显示在表格中,并附加到 JSON 文件中

I would also like the ability to delete the entry from the JSON file through the UI table.我还希望能够通过 UI 表从 JSON 文件中删除条目。 Basically anything that can happen to the UI table will also happen in the JSON (eg adding an entry to a UI table will then add the same data to a JSON, and removing an entry from the UI table will also remove the same entry from the table).基本上,UI 表中可能发生的任何事情也将在 JSON 中发生(例如,向 UI 表添加条目将向 JSON 添加相同的数据,从 UI 表中删除条目也会从 UI 表中删除相同的条目。桌子)。

Please let me know if more information is needed to clarify my question.如果需要更多信息来澄清我的问题,请告诉我。

Everything happens in JS is stored in memory by default, as opposed to written to some "file" on disk.默认情况下,JS 中发生的一切都存储在内存中,而不是写入磁盘上的某个“文件”。 For security reason web browsers normally don't have write access to users' local disk unless explicitly granted by user interaction .出于安全原因,除非由用户交互明确授予,否则 Web 浏览器通常没有对用户本地磁盘的写访问权限。

You can allow user to download those data in memory to a .json file on disk by clicking a button.您可以允许用户通过单击按钮将内存中的这些数据下载到磁盘上的.json文件。 The trick is to generate a dataURI link on the fly:诀窍是动态生成一个 dataURI 链接:

class App extends Component {

  // ...

  saveAsFile() {
    const str = JSON.stringify(this.state.people);
    const dataUri = "data:text/json," + str;
    const link = document.createElement("a");
    link.download = "table.json";
    link.href = dataUri;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }

  render() {
    return (
      <div>
        ...
        <button onClick={this.saveAsFile.bind(this)}>download</button>
      </div>
    )
  }
}

Demo: https://codesandbox.io/s/async-cherry-0tke6演示: https : //codesandbox.io/s/async-cherry-0tke6

Another option is to use the Native File System API .另一种选择是使用本机文件系统 API But I won't dive into it here since it's still experimental and not a web standard.但我不会在这里深入研究它,因为它仍然是实验性的,而不是网络标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM