score:1
What you need to do is pass a function to the DeleteRow
component from the Grid
component, so that when the delete is successful, you will update the list of items in Grid
. Try something like this
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 might be helpful as well
score:0
i have one solution of your code, you can passing function loadStudentsFromServer()
to component DeleteRow
same like you passing this.state.items
to component SudentList
you can change your code, the first one is here :
<SudentList data={this.state.items} loadStudentsFromServer={this.loadStudentsFromServer}/>
and then in your SudentList
component you can add this function :
loadStudentsFromServer: function(){
this.props.loadStudentsFromServer()
}.bind(this),
and also change this one to passing your function to DeleteRow
component :
<DeleteRow data={item.id} loadStudentsFromServer={this.props.loadStudentsFromServer} />
and just call the function if you success delete the data :
success: function (data) {
// refresh the list data
this.props.loadStudentsFromServer()
},
and this is the full code :
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')
);
Hope it can help you, you can comment if have another error, thanks :)
Note : this code is re fetching your data from server after delete some data
Source: stackoverflow.com
Related Query
- How to refresh data of one component from another component in react js
- How to pass data from one component to another in React or React-Redux?
- how to pass the data from one component to another in react js
- How to pass data from one component to another component in onchange using React js
- How to transfer data from one parent component to another parent component in react using router / router params
- React - How can I pass API data from one component to another in a different js file?
- How to send data from one react component to another react component?
- How to pass data from one component to another component/parent in React
- How to pass json data from one component to another using functions in react
- React JS: How to send data from one Component to another Component onClick event
- How to send specific API data from one component to another called within React Component in react
- How to from one component set state another component in react native?
- How to pass data from one component to another ReactJS
- How to pass data from one component to another while using API in reactjs
- How to parse data through a button from one component to another
- React - how to populate one select menu based on a value of another select menu using data from a local .json file
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- Passing data from one component to another - React & Redux
- React | pass form data from one component to another
- How to set one component's state from another component in React
- How to navigate from one component to another by clicking a button using React router
- How to fetch data from API and then pass it to another component in react after it is fully loaded?
- React sending data from one component to another one
- Passing data from one component to another in React using formik
- How to pass a Json data from one folder to another folder in react
- How to send result data from one page to another page in React
- How to pass a prop from one component to another when using react router link
- How to pass data from one variable in props to another in React app?
- How to pass data from one component to another via a function in ReactJS?
- How can I pass props from one component to another using Link and router in react
More Query from same tag
- Error: Nothing was returned from render... in MERN Stack Application
- React component is making infinite API calls
- How to center tooltips for block elements with CSS or JS?
- React router nested routes doesn't work
- Is there a way to get the Node/Element/HTMLElement from JSX before it's mounted?
- Possible to dismiss the browser compile error and show the react errorboundary?
- ReactJS: Mutating child state from parent via props
- CSS selector attributes does not work when adding style to component
- changing from redux to redux toolkit
- React prevState uses this particular parenthesis syntax. What is it?
- Best approach to wait on material-ui ripples to complete before taking snapshots
- React.js How do I use a component in another component?
- React: passing JSX as a function argument
- Best way to sharing function in react
- How to get the text to be in alignment after a line break in react js
- React components values are not updating
- Show only the 1st line of text in React Native
- I'm losing focus on a dynamically created jsx input when the state is updated
- Aligning icon to text inside a div
- How do I login in a App page and direct the page to Home?
- How to mock useSWRConfig implementation?
- How to assign data to a variable from axios get() response
- How do I deploy a React Webpack web app to Azure App Service?
- Types of property 'children' are incompatible -MockedProvider
- Passing a NavLink to a Material UI component via the containerElement prop gives “Failed prop type” warning
- Typescript optional chaining combined with ternary operator parsing error
- React - IE11 - visited links not recognized as visited on refresh
- Why can I not use a variable as parameter in the require() function of node.js (browserify)?
- How to command+click to open url in new tab and switch to it in JavaScript?
- How to create a Redux store using combineReducers without initial state