Just one question about: is there some API to delete one row in data grid? thanks.
hey @eric379990 there's no built-in API right now, but we have a couple of grids internally that have the ability to delete their rows.
the gist is basically you clone your this.state.rows object, remove the rowIdx you want, and then call this.setState({ rows: modifiedRows });
there are a couple of ways to do get the right rows:-
onClick functionenableRowSelect for the grid and find rows where isSelected === truerow.RowState === RowStates.Deletedping me back if you need more help or want a proper example :smile:
Thanks so much!
Hello jpdriver,
Can you please provide an example of adding delete button.
Thanks!
In case someone need...
import {Button} from 'react-bootstrap';
_rows.push({
key:location.id,
delete:<Button type="button" bsStyle="danger" bsSize="xsmall" title="Delete" onClick={this.handleDeleteRow.bind(this,location.id)}>x</Button>
})
handleDeleteRow=(id)=>{
...
};
Hey @jpdriver and @ahazut !
I'm trying to figure this out on my own right now and it's been a bit troubling. Would someone mind posting a more extensive example?
Thank you again
@jpdriver could you give a more extensive example please?
any update on this?
Another even better and more simple way is
// define a getColumns function in your class that renders the grid
getColumns = () => {
return [{
name: 'ID',
key: 'id',
}, {
name: 'Name',
key: 'name',
}, {
name: 'Actions',
key: '$delete',
getRowMetaData: (row) => row,
formatter: ({ dependentValues }) => (
<span>
<a href="javascript:;" onClick={() => this.deleteRows(dependentValues.id)}>Delete</a>
</span>
),
}];
}
I just implemented my own delete row button as well. Here is my delete function. I'm using the selectable row example, hence I'm using the this.state.selectedIds
my columns
columns : [
{
key: 'service',
name: 'Services',
draggable: true
},
{
key: 'detail',
name: 'Details',
editable: true,
draggable: true
}
]
my rowKey is 'service'
_delete() {
let rows = this.state.rows.slice();
this.state.selectedIds.map( function (value) {
rows.forEach(function(result, index) {
if(result['service'] === value) {
rows.splice(index, 1);
}
});
});
this.setState({ rows });
}
@bkniffler Thanks for advice, and I will give a little more example based on this.
Here's the deleteRows function:
deleteRows = (id) => {
let rows = this.state.rows.slice()
rows = rows.filter(row => row.id !== id)
this.setState({ rows })
}
You can simply call Array.prototype.filter() to remove it from the current rows and update the state
Actually in my real application, I pass the whole obj to do more things :)
Most helpful comment
Another even better and more simple way is