Yashank
Yashank

Reputation: 813

Using Buttons in React Material UI Table

I have made a table using Material UI where I have two buttons in the first column of every row. I wish to edit/delete rows on clicking these but Im stuck on logic. Is it even possible with my implementation ? If not then what's the preferred way of doing so?

render() {
  var deleteIcon =
  (<IconButton onClick={console.log("delete")}>
    <DeleteIcon color="secondary" />
  </IconButton>
  );

  const editIcon = (
    <IconButton onClick={console.log("edited")}>
      <EditIcon color="primary" />
    </IconButton>
  );

return(
  <TableBody>
   {this.state.serviceData.map(n => {
    return (
     <TableRow key={n.id}>
      <TableCell style={styles.editor} component="th" scope="row">
        {deleteIcon}
        {editIcon}
      </TableCell>
     <TableCell>{n.domain}</TableCell>
   <TableCell>{n.service_name}</TableCell>
  </TableCell>
 </TableRow>
)};

And my result is : Image showing my table

Upvotes: 2

Views: 34772

Answers (3)

shiva
shiva

Reputation: 3941

I have recreated your problem and solved the problem with my logic.

I passed the index of each element as a parameter to the handler functions.

Eg:

const editIcon = index => (
      <IconButton onClick={() => this.editComponent(index)}>
        <EditIcon color="primary" />
      </IconButton>
    );

DELETION

For deletion, pass the index as params to the handler function and delete the element at specified index using splice operator.

deleteComponent(index) {
    const serviceData = this.state.serviceData.slice();
    serviceData.splice(index, 1);
    this.setState({ serviceData });
  }

EDITING

I have used a state called index to keep track of the index the user is currently editing. Initially the index is -1

So whenever the user clicks edit button the editedIndex is updated.

editComponent(index) {
    this.setState({ editedIndex: index });
  }

I created two TextField Component which is shown at the specified cell (the cell where editbutton is clicked)

const editDomain = (
      <TextField
        id="domain"
        label="Domain"
        className={classes.textField}
        value={this.state.editedDomain}
        margin="normal"
        onChange={this.handleChange('editedDomain')}
      />
    );
    

So Whenever the rendering component Index is equal to editedIndex the editing Compomemts are shown at corresponding Tablecell

<TableCell>
  {serviceData.indexOf(n) === editedIndex
    ? editDomain
    : n.domain}
</TableCell>

Upvotes: 2

Adnan
Adnan

Reputation: 1707

I suppose you want to do this enter image description here

I have done same using React-Table here is the link for my project repo you can consider this as an example:

https://github.com/AdnanShah/ReactJS-KretaHub-/blob/Thank_You_Init/src/app/routes/dashboard/routes/Default/rows.js

Upvotes: 1

Yashank
Yashank

Reputation: 813

Building on @st4rl00rd's comment, I was able to tie the buttons using :

const editIcon = (
      <IconButton onClick={this.editItem}>
        <EditIcon color="primary" />
      </IconButton>
    );

and binding them in the constructor. I was able to get the selected row data by doing :

       <TableBody>
            {this.state.serviceData.map(n => {
              return (
                <TableRow key={n.id} onClick={this.getData.bind(this,n)}>

Upvotes: 7

Related Questions