2

I am currently working on building an app in Vue.js using Element-UI and I am fairly new to using Element-UI. I have created a table using el-table. I have managed to add contents in the table. Now, I have a delete button in each row of the table. After I click the delete button, it should delete the row successfully (ie, make a DELETE axios or http request to the server and delete that user from the database). In order to do that, I need to access the contents or just the username in that specific row. How do I do this?

Find my HTML (Delete) code below:

HTML part of Vue.js:

      <el-table>
             <el-table-column
                  width="75"
                  prop="name">
                  <template slot-scope="scope">
<!--Adding the delete button -->
                    <el-button
                      size="mini"
                      type="danger" 
                      icon="el-icon-delete" circle
                      @click="users_delete_visible = true"
                      >
                    </el-button>
                  </template>
             </el-table-column>

        </el-table>

There is a column for username as well in the table created using the el-table. How do I modify the above html code in order to access the name in the row when pressed on the delete button?

Any help would really be appreciated. I am stuck on this for quite sometime.

Thank you

1 Answer 1

2

When binding to the click event, you can pass the current row as an argument to your function and then access your data property.

<el-button
    size="mini"
    type="danger" 
    icon="el-icon-delete" circle
    @click="deleteUser(scope.row)"
>
</el-button>

Then in your function,

methods: {
    deleteUser(row) {
      let username = row.username; // Assuming your object has a username property 
      console.log('Deleting user ' + username);
    }
  }

Working Example

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. This worked really well. Appreciate it. :)
You're welcome. Please mark the question as solved.
I have another issue, I want to display a dialog box instead of an alert when pressed on the delete button. The Dialog box displays a message "Deleting user <username>" and then after clicking on the "Confrim" button on the dialog box, it should delete that user. So when I try doing that with calling the function delete_user(scope.row) after clicking the "Confrim" button, then it deletes the username from the last row in the table and not the one in the row that was meant to be deleted. It takes the username from the last row. Please could help in this? I would really appreciate it.

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.