10

I am using table that has expandable row feature. The row expands when the expand icon is clicked, you can check the example HERE. But, what I am trying to make is, the entire row clickable and toggle expand and collapse the row just like it works when the expand icon is clicked.

Please help.

Here is my markup:

<template lang="pug">
  el-table(:data="tableData")
    el-table-column(label="Employee Name", prop="userName")
    el-table-column(label="Company Name", prop="companyName")
    el-table-column(type="expand", align="right" )
      template(slot-scope="props")
        p User Name: {{ props.row.userName }}
        p Company Name: {{ props.row.companyName }}
</template>

2 Answers 2

20

Alright, I figured out the solution and answering my own question :)

Markup:

<template lang="pug">
  el-table(:data="tableData", @row-click="rowClicked", ref="tableData").clickable-rows
    el-table-column(label="Employee Name", prop="userName")
    el-table-column(label="Company Name", prop="companyName")
    el-table-column(type="expand", align="right" )
      template(slot-scope="props")
        p User Name: {{ props.row.userName }}
        p Company Name: {{ props.row.companyName }}
</template>

Script:

<script>
  export default {
    methods: {
      rowClicked(row) {
        this.$refs.tableData.toggleRowExpansion(row);
      }
    }
  }
</script>

Style - scss

// click-able rows
.clickable-rows {
  tbody tr td {
    cursor: pointer;
  }

  .el-table__expanded-cell {
    cursor: default;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

How would you trigger toggleRowExpansion in Vue3?
Answer to my previous comment. ``` lang-js const tableData = ref(null); rowClicked(row) { tableData.value.$refs.elTable.toggleRowExpansion(row); } ```
@MagnusPääru - <script setup> import { ref } from 'vue'; const tableData = ref(null); const rowClicked = (row) => { tableData.value.$refs.elTable.toggleRowExpansion(row); }; </script>
0

I think there is better way to do so We have row-key in el-table, and expand-row-keys So every time you click you can change current expanded row even you can create behavior that "expan-one-at-a-time" Like: :expand-row-keys="[currentExpandedRow]"

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.