英文:
How to refresh dataTable in ReactJS after the modal save button is clicked?
问题
在我学习React的第二周。
我需要一些关于如何在我从模态框中点击保存按钮时刷新ReactJs中的dataTable的帮助。
这是我的结构。
我有一个ParameterMaintenance.jsx
,它导入了DataTable
组件。
import React from 'react';
import { ParameterDatable } from '../../components/ParameterDatatable';
export const ParameterMaintenance = () => {
return (
<div className='container mt-5'>
<div></div>
<ParameterDatatable/>
</div>
)
}
然后,我有一个ParameterDataTable.jsx
文件,其中包含dataTable。请注意,在这里,我从DataTable的actions属性中调用了AddParameter.jsx
,如下所示actions={<AddParameter/>}
,以添加一个按钮,显示处理新参数保存的模态框表单。
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import DataTable from 'react-data-table-component';
import DataTableExtensions from 'react-data-table-component-extensions';
import 'react-data-table-component-extensions/dist/index.css';
import "bootstrap/dist/js/bootstrap.bundle.js";
import "bootstrap/dist/css/bootstrap.css";
import { Link } from "react-router-dom";
import { AddParameter } from './AddParameter';
export const ParameterDatatable= () => {
const [parameters, setParameters] = useState([]);
useEffect(() => {
loadParameters();
}, []);
const loadParameters = async () => {
const result = await axios.get("http://localhost:8080/parameters");
setParameters(result.data);
};
const deleteParam = async (id) => {
await axios.delete(`http://localhost:8080/parameter/${id}`);
loadParameters();
};
const column = [
// 列的定义
];
return (
<div className="datatable">
<DataTableExtensions
columns={column}
data={parameters}
print={false}
export={false}
>
<DataTable
title="Parameters"
columns={column}
data={parameters}
pagination
fixedHeader
fixedHeaderScrollHeight="450px"
highlightOnHover
actions={<AddParameter/>}
subHeader
/>
</DataTableExtensions>
</div>
)
}
以下是我的AddParameter.jsx
,这是保存新参数的模态框表单。
import React, { useState } from 'react';
import axios from "axios";
import "bootstrap/dist/js/bootstrap.bundle.js";
import "bootstrap/dist/css/bootstrap.css";
import { Link, useNavigate } from "react-router-dom";
export const AddParameter = () => {
let navigate = useNavigate();
const [parameter, setParameter] = useState({
param_name: "",
param_sequence_number: "",
param_value: "",
});
const { param_name, param_sequence_number, param_value } = parameter;
const onInputChange = (e) => {
setParameter({ ...parameter, [e.target.name]: e.target.value });
};
const onSubmit = async (e) => {
e.preventDefault();
console.log("Submitting");
await axios.post("http://localhost:8080/parameter", parameter);
navigate('admin/parameter1');
};
return (
<div>
<button className="btn btn-sm btn-info"
data-bs-toggle="modal"
data-bs-target="#exampleModal">Create New</button>
<div className="modal fade" id="exampleModal" tabIndex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
{/* 模态框的定义 */}
</div>
</div>
)
}
当我点击保存按钮时,我想关闭模态框并刷新DataTable,或者调用ParameterDataTable.jsx
中从API获取数据的loadParameters
函数。
尝试使用ReactJS中的navigate
来尝试重新加载页面,但地址会像这样追加到当前地址。
英文:
On my second week of learning React.
I need some help on how to refresh the dataTable in reactJs when I clicked the save button from the modal.
Here is my structure.
I have a parameterMaintenance.jsx below which imports the DataTable Component
ParameterMaintenance.jsx
import React from 'react'
import { ParameterDatable } from '../../components/ParameterDatatable'
export const ParameterMaintenance = () => {
return (
<div className='container mt-5'>
<div></div>
<ParameterDatatable/>
</div>
)
}
and then, I have a ParameterDataTable.jsx file that holds the dataTable.
notice that here, I called the AddParameter.jsx from the
DataTable actions property like this actions={<AddParameter/>}
to add a button that displays the modal form that handles the Saving of a new Parameter.
ParameterDatatable.jsx
import React, {useState, useEffect} from 'react'
import axios from 'axios';
import DataTable from 'react-data-table-component';
import DataTableExtensions from 'react-data-table-component-extensions';
import 'react-data-table-component-extensions/dist/index.css';
import "bootstrap/dist/js/bootstrap.bundle.js";
import "bootstrap/dist/css/bootstrap.css";
import { Link } from "react-router-dom";
import { AddParameter } from './AddParameter';
export const ParameterDatatable= () => {
const [parameters, setParameters] = useState([]);
useEffect(() => {
loadParameters();
}, []);
const loadParameters = async () => {
const result = await axios.get("http://localhost:8080/parameters");
setParameters(result.data);
};
const deleteParam = async (id) => {
await axios.delete(`http://localhost:8080/parameter/${id}`);
loadParameters();
};
const column = [
{
name: "Param Name",
selector: (row) => row.param_name,
width: "180px" ,
sortable: true,
alignItems: 'center',
display: 'flex',
},
{
name: "Sequence Number",
selector: (row) => row.param_sequence_number,
width: "150px" ,
sortable: true,
},
{
name: "Parameter Value",
selector: (row) => row.param_value,
width: "150px" ,
sortable: true,
},
{
name: "",
width: "110px" ,
cell: row =>
<div className="App">
<div className="openbtn text-center">
<button
type="button"
class="btn btn-danger"
data-bs-toggle="modal"
data-bs-target="#myModal"
>
Delete
</button>
<div className="modal" tabindex="-1" id="myModal">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title">Danger</h5>
<button
type="button"
className="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div className="modal-body">
<p>Do you want to remove this record premanently?</p>
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-secondary"
data-bs-dismiss="modal"
>
Close
</button>
<button type="button" className="btn btn-primary" onClick={() => deleteParam(row.param_id)}
data-bs-dismiss="modal">
Yes
</button>
</div>
</div>
</div>
</div>
</div>
</div>
,
},
{
name: "",
width: "110px" ,
cell: row =>
<button
className="btn btn-primary mx-2"
onClick={() => deleteParam(row.param_id)}>
Edit
</button>,
},
]
return (
<div className="datatable">
<DataTableExtensions
columns={column}
data={parameters}
print={false}
export={false}
>
<DataTable
title="Parameters"
columns={column}
data={parameters}
pagination
fixedHeader
fixedHeaderScrollHeight="450px"
highlightOnHover
actions={<AddParameter/>}
subHeader
/>
</DataTableExtensions>
</div>
)
}
And below is my AddParameter.jsx which is the modal form for saving new parameter
AddParameter.jsx
import React, {useState} from 'react'
import axios from "axios";
import "bootstrap/dist/js/bootstrap.bundle.js";
import "bootstrap/dist/css/bootstrap.css";
import { Link, useNavigate } from "react-router-dom";
export const AddParameter = () => {
let navigate = useNavigate();
const [parameter, setParameter] = useState({
param_name: "",
param_sequence_number: "",
param_value: "",
});
const {param_name, param_sequence_number, param_value} = parameter;
const onInputChange = (e) => {
setParameter({ ...parameter, [e.target.name]: e.target.value });
};
const onSubmit = async (e) => {
e.preventDefault();
console.log("Submitting");
await axios.post("http://localhost:8080/parameter", parameter);
navigate('admin/parameter1');
};
return (
<div>
<button className="btn btn-sm btn-info"
data-bs-toggle="modal"
data-bs-target="#exampleModal">Create New</button>
<div className="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h5 className="modal-title" id="exampleModalLabel">New Parameter</h5>
<button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div className="modal-body">
<form onSubmit={(e) => onSubmit(e)}>
<div className="mb-3">
<label for="recipient-name" className="col-form-label">Parameter Name</label>
<input type={"text"} className="form-control" placeholder="Param Name" name="param_name" value={param_name}
onChange={(e) => onInputChange(e)}/>
</div>
<div className="mb-3">
<label for="message-text" className="col-form-label">Sequence Number</label>
<input type={"text"} className="form-control" placeholder="Sequence Number" name="param_sequence_number" value={param_sequence_number}
onChange={(e) => onInputChange(e)}/>
</div>
<div className="mb-3">
<label for="message-text" className="col-form-label">Parameter Value</label>
<input type={"text"} className="form-control" placeholder="Value" name="param_value" value={param_value}
onChange={(e) => onInputChange(e)}/>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" className="btn btn-primary" data-bs-dismiss="modal">Save Parameter</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
)
}
what I wanted to do is when I clicked the Save button, it will close the modal and refresh the DataTable, or call the loadParameters function which gets data from API in ParameterDataTable.jsx
Tried to use the navigate in reactjs to try to reload the page but the address will just add to the current address like this.
答案1
得分: 1
我理解你的问题,你想在删除用户值后刷新表格数据。你学到了在React中父子组件、子父组件的概念,并在项目中实现,从而解决了你的问题。
// 例子:父组件
function ParentA(){
const [state,setstate] = useState(false);
useEffect(() => {
// 调用获取数据的方法;
},[state]);
return(
<ChildA state={state} setstate={setstate} />
)
}
// 子组件
function ChildA({state, setstate}){
const renderData = () => {
setstate(!state);
}
return (
<button onClick={renderData}>点击这里</button>
)
}
英文:
I understand your problem, you want to refresh table data after removing of user value. you learn about parent to child, child to parent concept in react and implement in project then solved your problem.
//example: Parent Component;
function ParentA(){
const [state,setstate] = useState(false);
useEffect(() => {
//call fetch data method;
},[state]);
return(
<ChildA state={state} setstate={setstate} />
)
}
//child component.
function ChildA({state, setstate}){
const renderData = () => {
setstate(!state);
}
return (
<button onClick={renderData}>Click here</button>
)
}
答案2
得分: 1
这是一个可能的方法:
将loadParameter
函数作为AddParameter
组件的prop传递,并在提交表单或点击按钮时调用它。
export const AddParameter = (props) => {
....
const onSubmit = async (e) => {
e.preventDefault();
console.log("Submitting");
await axios.post("http://localhost:8080/parameter", parameter);
props.loadParameter()
};
...// or call it onClick
然后从父组件传递它
<DataTable
title="Parameters"
columns={column}
data={parameters}
pagination
fixedHeader
fixedHeaderScrollHeight="450px"
highlightOnHover
actions={<AddParameter loadParameter={loadParameter} />}
subHeader
/>
最后,正如你在评论中所说想要清空文本框,你只需在关闭模态时调用一个函数。
const resetTextAfterClosing = () => {
setParameter({
param_name: "",
param_sequence_number: "",
param_value: "",
});
}
英文:
This one possible approach:
Pass loadParameter
function as a prop of AddParameter
component and call it when sending the form or when clicking the button.
export const AddParameter = (props) => {
....
const onSubmit = async (e) => {
e.preventDefault();
console.log("Submitting");
await axios.post("http://localhost:8080/parameter", parameter);
props.loadParameter()
};
...// or call it onClick
Then pass it from the parent component
<DataTable
title="Parameters"
columns={column}
data={parameters}
pagination
fixedHeader
fixedHeaderScrollHeight="450px"
highlightOnHover
actions={<AddParameter loadParameter = {loadParameter} />}
subHeader
/>
Finally, as you said in the comments that you want to clear the text boxes, you just have to call a function when closing the modal.
const resetTextAfterClosing = () => {
setParameter({
param_name: "",
param_sequence_number: "",
param_value: "",
});
}
答案3
得分: 0
在 "onSubmit" 函数中,设置 axios 响应,其中包含在 "setParameters" 中的所有数据,以及
useEffect(() => {
loadParameters();
}, [parameters]);
希望这能解决你的问题。但是请了解一下 props drilling 和 parent-child components。
英文:
In "onSubmit" function, set the axios response which contains all data in "setParameters" and
useEffect(() => {
loadParameters();
}, [parameters]);
Hope, this will solve your problem. But have some studies about props drilling and parent-child components
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论