0

i have created delete functionality,in which when user clicks delete[X] it has to get deleted from the respected row w.r.t datagrid view used in react

passing id as an parameter used _find index(loadlash)

problem:

1) selected Rows are not getting deleted.

code:

onclick event

      <div>
        <button onClick={() => this.deleteHandler(params.value)}>X</button>
      </div>

Delete code:

 deleteHandler = (id) => {
 const arrayPerson = this.props.rowData;
 const index = _.findIndex(this.props.rowData, { id: id });
 if (arrayPerson.indexOf(id) > -1) {
   arrayPerson.splice(index, 1);
   this.setState({ rows: arrayPerson });
 }

can any one help me on this issue.

14
  • What issue you have got? Commented Feb 1, 2018 at 14:02
  • if i click on delete[x] its not getting deleted and there are few empty rows if i click on that index =0.... after clicking on empty row and again if i click on data it shows index as -1 Commented Feb 1, 2018 at 14:05
  • Take a look at this link Commented Feb 1, 2018 at 14:07
  • Possible duplicate of Deleting an item in ReactJS Commented Feb 1, 2018 at 14:08
  • @TheReason i have changed the code ,still its not working Commented Feb 1, 2018 at 14:47

1 Answer 1

1

This is a working example for this what if different is that you are passing the data from parent to child for that u can do multiple thing.

The Parent Component.

class TestComp extends React.Component {
constructor() {
    super();
    this.state = { listItems: [{ id: 1, text: "abc" }, { id: 2, text: "bcd" }, { id: 3, text: "dec" }] }
    this.handleRowClick = this.handleRowClick.bind(this);
}
handleRowClick(id) {

    const listItems = this.state.listItems;
    let copyListItems = [];
    listItems.forEach(function (item) {
        let objCopy = Object.assign({}, item);
        copyListItems.push(objCopy);
    });
    let updatedArray = _.remove(copyListItems, function (item) { return item.id == id; });

    this.setState({ listItems: copyListItems });
}
render() {

    return (
        <div>
            <ChildComp
                list={this.state.listItems}
                deleteHandler={this.handleRowClick}
            />
        </div>
    )
}
}

The Child Component that is receiving List items as props and when clicked the parent handler is being called which will update the List items and setState of Parent which will update the list being provided to child component hence it will be rendered.

export class ChildComp extends React.Component {

render() {
    let list = this.props.list.map((obj) =>
        <div key={obj.id} id={obj.id} style={{ padding: "10px", backgroundColor: "grey", border: "1px solid black" }} onClick={() => { this.props.deleteHandler(obj.id) }}> {obj.text} </div>
    );
    return (
        <div >
           {list}
        </div>
    )
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

no i have not used this.state.rows......rows={this.props.rowData} these are only properties
you are setting the updated Rows in this.setState to rows.. now your this.state.rows contains the updated rows. u need to render them in order for the view to change otherwise , the render method will keep showing the output of original rows. also u should not mutate state or props
if we set "this.state .rows "in dataGrid it throws an error(TypeError: Cannot read property 'rowData' of null);
because you are receiving props from the parent. you need to set them in your state of your component because you are going to change them ..
sure i will give u a simple example ryt now
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.