如何使 element in vuejs that contains with html

问题描述 投票:0回答:1

我想提出一个表行组件,它可以包含有HTML中有一个插槽。问题是,浏览器吊文出表的vuejs有机会呈现它。

例如,我想提出一个表像这样https://codepen.io/mankowitz/pen/LqLRWr

td, th {
  border: 1px solid red;
}

.main-table {
  border: 2px solid blue;
}
<table class="main-table">
  <thead>
    <tr>
      <th>Col1</th>
      <th>Col2</th>
      <th>Col3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="3">Collapsed row</td>
    </tr>
    <tr>
      <td>
        <p>text</p>
        <table>
            <tr>
              <th>embedded table header</th>
            </tr>
            <tr>
              <td>embedded table text</td>
            </tr>            
        </table>
      </td>
      <td> Col2 </td>
      <td> Col3 </td>
    </tr>
    <tr>
      <td>
        regular row col1</td>
      <td>col2</td>
      <td>col3</td>
    </tr>
</table>

然而,当我把它放在一个VUE模板,格式是完全错误的。见https://codepen.io/mankowitz/pen/PVmBxV

Vue.component("table-row", {
  props: ["collapsed"],
  template: `
<tr>
  <td v-if="collapsed" colspan="3">collapsed row</td>
  <td v-if="!collapsed"> <slot /> </td>
  <td v-if="!collapsed">  col2 </td>
  <td v-if="!collapsed">  col3 </td>
</tr>
`
});

const example = {};
const app = new Vue(example);
app.$mount("#app");
th, td {
  border: 1px solid red;
  padding: 1px;
}

.main-table {
  border: 2px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <table class="main-table">
    <thead>
      <tr>
        <th>Main Col 1</th>
        <th>Main Col 2</th>
        <th>Main Col 3</th>
      </tr>
    </thead>

    <tbody>

      <!-- This row has table inside table -->
      <table-row :collapsed="1">
        <p>title text</p>
        <table>
          <tr>
            <th>embedded table header</th>
          </tr>
          <tr>
            <td>embedded table text</td>
          </tr>
        </table>
      </table-row>

      <!-- This row is normal  -->
      <table-row :collapsed="0">
        This row is not collapsed
      </table-row>

      <!-- This row is collapsed (hidden)  -->
      <table-row :collapsed="1">
        This row is collapsed
      </table-row>

    </tbody>

  </table>
</div>
javascript vue.js vuejs2 vue-component
1个回答
0
投票

我做到了与VUE工作渲染功能,你也许可以与模板做到这一点,但它会更长:

Vue.component("table-row", {
  props: ["collapsed"],
  render: function(h) {
    if (this.collapsed == 1) {
      return h('tr', [h('td', {attrs: {colspan: 3}}, 'collapsed')]);
    }
    return h('tr', this.$slots.default);
  }
});


const app = new Vue({el: '#app'});
th, td {
  border: 1px solid red;
  padding: 1px;
}

.main-table {
  border: 2px solid blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <table class="main-table">
    <thead>
      <tr>
        <th>Main Col 1</th>
        <th>Main Col 2</th>
        <th>Main Col 3</th>
      </tr>
    </thead>

    <tbody>

      <!-- This row has table inside table -->
      <tr is="table-row" :collapsed="0">
        <td>
        <p>title text</p>
        <table>
          <tr>
            <th>embedded table header</th>
          </tr>
          <tr>
            <td>embedded table text</td>
          </tr>
        </table>
        </td>
        <td></td>
        <td></td>
      </tr>

      <!-- This row is normal  -->
      <tr is="table-row" :collapsed="0">
        <td>This row is not collapsed</td>
        <td></td>
        <td></td>
      </tr>

      <!-- This row is collapsed (hidden)  -->
      <tr is="table-row" :collapsed="1">
        <td>This row is collapsed</td>
      </tr>

    </tbody>

  </table>
</div>

请注意,如果您使用的插槽按我的例子中,你仍然必须包括<td>节点。

© www.soinside.com 2019 - 2024. All rights reserved.