react网格生成_如何在React中构建实时可编辑数据网格

react网格生成

by Peter Mbanugo

彼得·姆巴努戈(Peter Mbanugo)

如何在React中构建实时可编辑数据网格 (How to Build a Real-time Editable Datagrid In React)

A datagrid enables you to display and edit data. This is a vital feature in most data-driven applications.

数据网格使您可以显示和编辑数据。 在大多数数据驱动的应用程序中,这是至关重要的功能。

You may have implemented this in one of your React apps in the past. Maybe you used libraries like react-bootstrap-table, react-grid, or react-table. With those, you can add a Datagrid to your React app. But what if you want the changes to be done in real-time and updates synchronized across all connected devices and their browsers?

您过去可能已经在您的一个React应用中实现了这一点。 也许您使用了诸如react-bootstrap-tablereact-gridreact-table之类的库 。 有了这些,您可以将Datagrid添加到您的React应用程序中。 但是,如果您希望实时进行更改并在所有连接的设备及其浏览器之间同步更新,该怎么办?

In this article, I will show you how to build a real-time datagrid in React using react-table and Hamoni Sync.

在本文中,我将向您展示如何使用react-tableHamoni Sync在React中构建实时数据网格。

react-table is a lightweight and fast library for rendering tables in React, and it supports pagination and many more features.

react-table是一个轻量级,快速的库,用于在React中渲染表,它支持分页和许多其他功能。

Hamoni Sync is a real-time state synchronization service which enables you to synchronize your application state in real-time. I will show you how to build a datagrid with people’s first and last names.

Hamoni Sync是一种实时状态同步服务,使您可以实时同步应用程序状态。 我将向您展示如何使用人们的名字和姓氏构建数据网格。

If you want to follow along, you should have some knowledge of React and have the following tools installed:

如果您想继续学习,那么您应该对React有所了解,并安装了以下工具:

  1. NodeJS

    节点JS

  2. npm & npx. If you have installed npm version 5.2.0 or greater, it installs npx alongside npm.

    npmnpx 。 如果您已安装npm 5.2.0或更高版本,它将与npm一起安装npx。

  3. create-react-app

    创建React应用

创建React应用 (Create the React app)

First we will create a new React project using create-react-app.

首先,我们将使用create-react-app创建一个新的React项目。

Open the command line and run npx create-react-app realtime-react-datatable. This will bootstrap a React application for us by creating a new directory realtime-react-datatable with the files needed to build a React application.

打开命令行并运行npx create-react-app realtime-react-datatable 。 这将通过使用构建React应用程序所需的文件创建一个新目录realtime-react-datatable为我们引导一个React应用程序。

With the React app created, we need to install react-table and Hamoni Sync. Still on the command line, run cd realtime-react-datatable to switch to the directory for the app. Run npm i react-table hamoni-sync in the command line to install both packages.

创建React应用程序后,我们需要安装react-table和Hamoni Sync。 仍然在命令行上,运行cd realti

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要为 ag-grid-react 自定义单元格编辑器创建带搜索和输入的输入框,你可以按照以下步骤进行操作: 1. 创建一个新的 React 组件,该组件将作为自定义单元格编辑器。 2. 在该组件的 render() 方法,使用一个文本输入框和一个搜索输入框来创建你的输入框。 3. 在 componentDidMount() 方法,使用 ag-grid-react 的 ICellEditorParams 接口的方法获取单元格的当前值。 4. 在 ag-grid-react 的 ICellEditorParams 接口实现 getValue() 方法,该方法将返回输入框的当前值。 5. 在 ag-grid-react 的 ICellEditorParams 接口实现 isPopup() 方法,该方法将返回 true,以便输入框显示为弹出式窗口。 6. 在 ag-grid-react 使用你的新自定义单元格编辑器。 以下是一个示例代码,演示如何创建一个带搜索和输入的输入框作为 ag-grid-react 自定义单元格编辑器: ```javascript import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { AgEditorCellDef } from 'ag-grid-react'; class SearchInputEditor extends Component { constructor(props) { super(props); this.state = { value: props.value, }; } componentDidMount() { this.refs.input.focus(); } getValue() { return this.state.value; } isPopup() { return true; } handleChange = (event) => { this.setState({ value: event.target.value }); } handleKeyDown = (event) => { if (event.keyCode === 13) { this.props.stopEditing(); } } render() { return ( <div> <input ref="input" type="text" value={this.state.value} onChange={this.handleChange} onKeyDown={this.handleKeyDown} /> <input type="text" placeholder="Search..." /> </div> ); } } SearchInputEditor.propTypes = { value: PropTypes.string, stopEditing: PropTypes.func.isRequired, }; export default class GridExample extends Component { constructor(props) { super(props); this.state = { columnDefs: [ { headerName: 'Name', field: 'name', editable: true, cellEditorFramework: SearchInputEditor, }, { headerName: 'Age', field: 'age', editable: true, }, { headerName: 'Gender', field: 'gender', editable: true, }, ], rowData: [ { name: 'John Smith', age: 35, gender: 'Male' }, { name: 'Jane Doe', age: 28, gender: 'Female' }, { name: 'Bob Johnson', age: 42, gender: 'Male' }, ], }; } render() { return ( <div className="ag-theme-balham" style={{ height: '500px', width: '600px' }} > <AgGridReact columnDefs={this.state.columnDefs} rowData={this.state.rowData} enableSorting={true} /> </div> ); } } ``` ### 回答2: ag-grid-react允许我们自定义单元格编辑器,以满足特定的需求。要创建一个带搜索和输入框的自定义单元格编辑器,我们可以按照以下步骤进行: 1. 首先,我们需要创建一个React组件作为单元格编辑器。我们可以使用antd或其他UI库的输入框组件,该组件已经带有搜索功能。在该组件内部,我们可以添加输入框和搜索按钮。 2. 在自定义编辑器组件,我们需要实现ag-Grid的IAgEditorReact接口,以便它可以与ag-Grid进行交互。我们必须实现接口的相关方法,如getValue()、isPopup()、isCancelBeforeStart()等。 3. 实现getValue()方法时,我们可以在函数内部获取输入框的值并返回它。这样,编辑器就可以将输入框的值传回给ag-Grid。 4. 其他方法如isPopup()和isCancelBeforeStart()可以根据需求进行实现,以控制编辑器的弹出和取消行为。 5. 在创建自定义单元格时,我们需要指定单元格的编辑器。我们可以设置editType为"custom",并将我们之前创建的自定义编辑器组件作为cellEditor设置。这将使ag-Grid在需要编辑单元格时使用我们的自定义编辑器。 这样,我们就可以利用自定义单元格编辑器为ag-grid-react创建一个带有搜索功能的输入框了。每次需要编辑单元格时,ag-Grid将显示我们定义的自定义编辑器组件,并且我们可以在自定义编辑器组件自由定义输入框的样式和功能。 ### 回答3: ag-grid-react是一个用于创建数据网格React组件库。它允许我们自定义单元格编辑器,以便满足特定的需求。对于要求带搜索功能的输入框,我们可以创建一个自定义的单元格编辑器组件。 首先,我们可以创建一个名为SearchInput的React组件,该组件包含一个带搜索功能的输入框。在这个组件,我们可以使用合适的逻辑和UI库来实现搜索功能,例如在输入框实时过滤并显示匹配的结果。 接下来,我们可以创建一个名为CustomCellEditor的React组件,并在其使用SearchInput组件作为单元格编辑器。在CustomCellEditor组件,我们需要实现ag-grid-react提供的ICellEditor接口,并重写相关方法。 在getGui方法,我们返回SearchInput组件作为单元格编辑器的UI。在getValue方法,我们可以获取SearchInput组件输入框的值并返回。在isPopup方法,我们可以定义是否显示这个单元格编辑器的弹出框。 最后,我们可以将CustomCellEditor组件应用到我们的数据网格的特定列。在ag-grid-react配置的columnDefs,我们可以为需要使用自定义单元格编辑器的列设置cellEditor属性为CustomCellEditor组件。 通过以上步骤,我们可以创建一个带有搜索功能的输入框作为ag-grid-react的自定义单元格编辑器。这样,在网格的相关列,当用户需要编辑单元格时,就会显示这个带搜索的输入框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值