如何在React js中从另一个组件刷新一个组件的数据
How to refresh data of one component from another component in react js
我不熟悉 React js 并使用一些基本的 CRUD 操作创建一个示例应用程序。
我可以显示一个列表,但我不知道如果我删除任何记录然后如何刷新数据网格。
这是我的代码:
var DeleteRow = React.createClass({
rowDelete: function (e) {
console.log(this.props);
var isConfirm = confirm("Are you sure want to delete this record?")
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'Students/DeleteStudent',
data: { id: this.props.data },
success: function (data) {
// refresh the list data
},
error: function (error) {
}
});
}
},
render: function () {
return(
<button data={this.props.id} onClick={this.rowDelete} className="btn btn-danger">Delete</button>
);
},
});
var SudentList = React.createClass({
render: function () {
var RowData = this.props.data.map(function(item){
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstMidName}</td>
<td>{item.lastName}</td>
<td>{item.enrollmentDate}</td>
<td>Edit</td>
<td><DeleteRow data={item.id}/></td>
</tr>
);
});
return (
<table className="table table-bordered table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Fist Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{RowData}
</tbody>
</table>
);
}
});
var Gird = React.createClass({
loadStudentsFromServer: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
getInitialState: function () {
return { items: [] };
},
componentDidMount: function () {
this.loadStudentsFromServer();
},
render: function () {
var addBtnStyle = {
margin:'10px',
}
return (
<div>
<button className="btn btn-primary pull-right" style={addBtnStyle}>Add New</button>
<SudentList data={this.state.items} />
</div>
);
}
});
ReactDOM.render(
<Gird dataUrl='/Students/GetAllStudent' />,
document.getElementById('content')
);
我想在 ajax 成功时从 DeleteRow
组件刷新 Gird
组件的数据,我该怎么做?。
我已经在这个网站上回答了几个问题,但到目前为止运气不好。
你需要做的是从Grid
组件传递一个函数给DeleteRow
组件,这样当删除成功时,你会更新[=12]中的项目列表=].尝试这样的事情
var DeleteRow = React.createClass({
rowDelete: function (e) {
console.log(this.props);
var self = this;
var isConfirm = confirm("Are you sure want to delete this record?")
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'Students/DeleteStudent',
data: { id: this.props.data },
success: function (data) {
// refresh the list data
self.props.onRowDelete(self.props.data)
},
error: function (error) {
}
});
}
},
render: function () {
return(
<button data={this.props.id} onClick={this.rowDelete} className="btn btn-danger">Delete</button>
);
},
});
var SudentList = React.createClass({
render: function () {
var self = this;
var RowData = this.props.data.map(function(item){
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstMidName}</td>
<td>{item.lastName}</td>
<td>{item.enrollmentDate}</td>
<td>Edit</td>
<td><DeleteRow data={item.id} onRowDelete={self.props.onRowDelete}/></td>
</tr>
);
});
return (
<table className="table table-bordered table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Fist Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{RowData}
</tbody>
</table>
);
}
});
var Gird = React.createClass({
loadStudentsFromServer: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
getInitialState: function () {
return { items: [] };
},
componentDidMount: function () {
this.loadStudentsFromServer();
},
onRowDelete: function(id) {
var items = this.state.items.filter(function(item, i) {
return item.id !== id;
})
this.setState({items: items})
},
render: function () {
var addBtnStyle = {
margin:'10px',
}
return (
<div>
<button className="btn btn-primary pull-right" style={addBtnStyle}>Add New</button>
<SudentList data={this.state.items} onRowDelete={this.onRowDelete}/>
</div>
);
}
});
This 也可能有帮助
我有一个你的代码的解决方案,你可以将函数 loadStudentsFromServer()
传递给组件 DeleteRow
就像你将 this.state.items
传递给组件 SudentList
您可以更改您的代码,第一个在这里:
<SudentList data={this.state.items} loadStudentsFromServer={this.loadStudentsFromServer}/>
然后在您的 SudentList
组件中您可以添加此功能:
loadStudentsFromServer: function(){
this.props.loadStudentsFromServer()
}.bind(this),
同时将此更改为将您的函数传递给 DeleteRow
组件:
<DeleteRow data={item.id} loadStudentsFromServer={this.props.loadStudentsFromServer} />
如果成功删除数据,只需调用该函数即可:
success: function (data) {
// refresh the list data
this.props.loadStudentsFromServer()
},
这是完整代码:
var DeleteRow = React.createClass({
rowDelete: function (e) {
console.log(this.props);
var isConfirm = confirm("Are you sure want to delete this record?")
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'Students/DeleteStudent',
data: { id: this.props.data },
success: function (data) {
// refresh the list data
this.props.loadStudentsFromServer()
},
error: function (error) {
}
});
}
},
render: function () {
return(
<button data={this.props.id} onClick={this.rowDelete} className="btn btn-danger">Delete</button>
);
},
});
var SudentList = React.createClass({
loadStudentsFromServer: function(){
this.props.loadStudentsFromServer()
}.bind(this),
render: function () {
var RowData = this.props.data.map(function(item){
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstMidName}</td>
<td>{item.lastName}</td>
<td>{item.enrollmentDate}</td>
<td>Edit</td>
<td><DeleteRow data={item.id} loadStudentsFromServer={this.props.loadStudentsFromServer} /></td>
</tr>
);
});
return (
<table className="table table-bordered table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Fist Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{RowData}
</tbody>
</table>
);
}
});
var Gird = React.createClass({
loadStudentsFromServer: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
getInitialState: function () {
return { items: [] };
},
componentDidMount: function () {
this.loadStudentsFromServer();
},
render: function () {
var addBtnStyle = {
margin:'10px',
}
return (
<div>
<button className="btn btn-primary pull-right" style={addBtnStyle}>Add New</button>
<SudentList data={this.state.items} loadStudentsFromServer={this.loadStudentsFromServer}/>
</div>
);
}
});
ReactDOM.render(
<Gird dataUrl='/Students/GetAllStudent' />,
document.getElementById('content')
);
希望对您有所帮助,如有其他错误可以评论,谢谢:)
注意:这段代码是在删除一些数据后从服务器重新获取你的数据
我不熟悉 React js 并使用一些基本的 CRUD 操作创建一个示例应用程序。
我可以显示一个列表,但我不知道如果我删除任何记录然后如何刷新数据网格。
这是我的代码:
var DeleteRow = React.createClass({
rowDelete: function (e) {
console.log(this.props);
var isConfirm = confirm("Are you sure want to delete this record?")
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'Students/DeleteStudent',
data: { id: this.props.data },
success: function (data) {
// refresh the list data
},
error: function (error) {
}
});
}
},
render: function () {
return(
<button data={this.props.id} onClick={this.rowDelete} className="btn btn-danger">Delete</button>
);
},
});
var SudentList = React.createClass({
render: function () {
var RowData = this.props.data.map(function(item){
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstMidName}</td>
<td>{item.lastName}</td>
<td>{item.enrollmentDate}</td>
<td>Edit</td>
<td><DeleteRow data={item.id}/></td>
</tr>
);
});
return (
<table className="table table-bordered table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Fist Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{RowData}
</tbody>
</table>
);
}
});
var Gird = React.createClass({
loadStudentsFromServer: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
getInitialState: function () {
return { items: [] };
},
componentDidMount: function () {
this.loadStudentsFromServer();
},
render: function () {
var addBtnStyle = {
margin:'10px',
}
return (
<div>
<button className="btn btn-primary pull-right" style={addBtnStyle}>Add New</button>
<SudentList data={this.state.items} />
</div>
);
}
});
ReactDOM.render(
<Gird dataUrl='/Students/GetAllStudent' />,
document.getElementById('content')
);
我想在 ajax 成功时从 DeleteRow
组件刷新 Gird
组件的数据,我该怎么做?。
我已经在这个网站上回答了几个问题,但到目前为止运气不好。
你需要做的是从Grid
组件传递一个函数给DeleteRow
组件,这样当删除成功时,你会更新[=12]中的项目列表=].尝试这样的事情
var DeleteRow = React.createClass({
rowDelete: function (e) {
console.log(this.props);
var self = this;
var isConfirm = confirm("Are you sure want to delete this record?")
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'Students/DeleteStudent',
data: { id: this.props.data },
success: function (data) {
// refresh the list data
self.props.onRowDelete(self.props.data)
},
error: function (error) {
}
});
}
},
render: function () {
return(
<button data={this.props.id} onClick={this.rowDelete} className="btn btn-danger">Delete</button>
);
},
});
var SudentList = React.createClass({
render: function () {
var self = this;
var RowData = this.props.data.map(function(item){
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstMidName}</td>
<td>{item.lastName}</td>
<td>{item.enrollmentDate}</td>
<td>Edit</td>
<td><DeleteRow data={item.id} onRowDelete={self.props.onRowDelete}/></td>
</tr>
);
});
return (
<table className="table table-bordered table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Fist Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{RowData}
</tbody>
</table>
);
}
});
var Gird = React.createClass({
loadStudentsFromServer: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
getInitialState: function () {
return { items: [] };
},
componentDidMount: function () {
this.loadStudentsFromServer();
},
onRowDelete: function(id) {
var items = this.state.items.filter(function(item, i) {
return item.id !== id;
})
this.setState({items: items})
},
render: function () {
var addBtnStyle = {
margin:'10px',
}
return (
<div>
<button className="btn btn-primary pull-right" style={addBtnStyle}>Add New</button>
<SudentList data={this.state.items} onRowDelete={this.onRowDelete}/>
</div>
);
}
});
This 也可能有帮助
我有一个你的代码的解决方案,你可以将函数 loadStudentsFromServer()
传递给组件 DeleteRow
就像你将 this.state.items
传递给组件 SudentList
您可以更改您的代码,第一个在这里:
<SudentList data={this.state.items} loadStudentsFromServer={this.loadStudentsFromServer}/>
然后在您的 SudentList
组件中您可以添加此功能:
loadStudentsFromServer: function(){
this.props.loadStudentsFromServer()
}.bind(this),
同时将此更改为将您的函数传递给 DeleteRow
组件:
<DeleteRow data={item.id} loadStudentsFromServer={this.props.loadStudentsFromServer} />
如果成功删除数据,只需调用该函数即可:
success: function (data) {
// refresh the list data
this.props.loadStudentsFromServer()
},
这是完整代码:
var DeleteRow = React.createClass({
rowDelete: function (e) {
console.log(this.props);
var isConfirm = confirm("Are you sure want to delete this record?")
if (isConfirm) {
$.ajax({
type: 'POST',
url: 'Students/DeleteStudent',
data: { id: this.props.data },
success: function (data) {
// refresh the list data
this.props.loadStudentsFromServer()
},
error: function (error) {
}
});
}
},
render: function () {
return(
<button data={this.props.id} onClick={this.rowDelete} className="btn btn-danger">Delete</button>
);
},
});
var SudentList = React.createClass({
loadStudentsFromServer: function(){
this.props.loadStudentsFromServer()
}.bind(this),
render: function () {
var RowData = this.props.data.map(function(item){
return (
<tr key={item.id}>
<td>{item.id}</td>
<td>{item.firstMidName}</td>
<td>{item.lastName}</td>
<td>{item.enrollmentDate}</td>
<td>Edit</td>
<td><DeleteRow data={item.id} loadStudentsFromServer={this.props.loadStudentsFromServer} /></td>
</tr>
);
});
return (
<table className="table table-bordered table-responsive">
<thead>
<tr>
<th>Id</th>
<th>Fist Name</th>
<th>Last Name</th>
<th>Enrollment Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{RowData}
</tbody>
</table>
);
}
});
var Gird = React.createClass({
loadStudentsFromServer: function () {
$.get(this.props.dataUrl, function (data) {
if (this.isMounted()) {
this.setState({
items: data
});
}
}.bind(this));
},
getInitialState: function () {
return { items: [] };
},
componentDidMount: function () {
this.loadStudentsFromServer();
},
render: function () {
var addBtnStyle = {
margin:'10px',
}
return (
<div>
<button className="btn btn-primary pull-right" style={addBtnStyle}>Add New</button>
<SudentList data={this.state.items} loadStudentsFromServer={this.loadStudentsFromServer}/>
</div>
);
}
});
ReactDOM.render(
<Gird dataUrl='/Students/GetAllStudent' />,
document.getElementById('content')
);
希望对您有所帮助,如有其他错误可以评论,谢谢:)
注意:这段代码是在删除一些数据后从服务器重新获取你的数据