Vue.js - element-ui el-table v-for lost the last item

Viewed 3579

I want to generate grouping table head by a title list , but the v-for loop lost the last one item.

The html template code:

<div id="app">
  <el-table :data="tableData" stripe highlight-current-row height="100%">
    <template v-for="title in titles">
      <el-table-column v-if="typeof title === 'object'" :key="title" fixed :prop="Object.entries(title)[0][0]"
                       :label="Object.entries(title)[0][0]">
        <el-table-column v-for="subtitle in Object.entries(title)[0][1]" :key="subtitle" fixed :prop="subtitle"
                         :label="subtitle">
        </el-table-column>

      </el-table-column>
      <el-table-column v-else :key="title" fixed :prop="title" :label="title">
      </el-table-column>
    </template>
  </el-table>
</div>

The JavaScript code:

new Vue({
  el: "#app",
  data() {
    return {
      titles: [
        "title 1",
        { "title 2": ["title 2-1", "title 2-2"] },
        "title 3",
        "title 4"
      ]
    };
  }
});

The render result as following picture:

enter image description here

My problem is that the last item "title 4" is not displayed, only a blank is left, hope someone can help me.

The table doc of element-ui is here, and the demo code is here.

1 Answers

After some debugging i found that fixed prop adds is-hidden class to th element which hide the title4 and doesn't hide the other ones (i don't understand why this is happening !!), to solve this i tried to remove the fixed attribute and reverse the conditional rendering :

  <el-table :data="tableData" stripe highlight-current-row height="100%">
    <template v-for="(title,index) in titles">
         <el-table-column  v-if="typeof title !== 'object'" :key="title"  :prop="title" :label="title">
      </el-table-column>
      <template v-else>
      <el-table-column  :prop="Object.entries(title)[0][0]" :label="Object.entries(title)[0][0]">
        <el-table-column v-for="subtitle in Object.entries(title)[0][1]" :key="subtitle" fixed :prop="subtitle" :label="subtitle">
        </el-table-column>

      </el-table-column>
       </template>
    </template>
  </el-table>

Demo

Related