在 Bootstrap 表中中斷行:在 Bootstrap 表中實現行中斷的指南
P粉957661544
P粉957661544 2024-03-27 21:54:13
0
2
394

我正在嘗試在表內的陣列上建立 .join() 。預期結果是每輛車(在下面的範例中)位於一排。

我嘗試過使用 .join("\r\n").join("<br />"),但它不起作用。我錯過了什麼?

new Vue({
  el: "#table",
  data() {
    return {
      items: [
        { firstname: "John", lastname: "Doe", cars: ["Ferrari", "Tesla"] },
        { firstname: "Jane", lastname: "Doe", cars: ["BMW", "Civic"] },
        { firstname: "Jack", lastname: "Doo", cars: ["Corsa", "Golf"] },
        { firstname: "Julie", lastname: "Doo", cars: ["Fiesta", "Brasilia"] }
      ],
      fields: [
        { key: "firstname", label: "First name" },
        { key: "lastname", label: "Last name" },
        { key: "cars", label: "Cars" }
      ]
    };
  },
  methods: {
    join() {
      for (let item of this.items) {
        item.cars = item.cars.join("<br />")
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>

<div id="table">
  <b-button @click="join">Join Array</b-button>
  <b-table :fields="fields" :items="items" />
</div>

與上面相同的程式碼片段,但在 CodePen 上。

P粉957661544
P粉957661544

全部回覆(2)
P粉587780103

假設您希望它始終位於單獨的行上(並且不僅僅是在單擊範例中的按鈕之後),那麼您可以使用<b-table> 提供的插槽來自訂該列的內容,並使用一個簡單的v-for 來渲染它自己的<div> 中的每個元素。

new Vue({
  el: "#table",
  data() {
    return {
      items: [
        { firstname: "John", lastname: "Doe", cars: ["Ferrari", "Tesla"] },
        { firstname: "Jane", lastname: "Doe", cars: ["BMW", "Civic"] },
        { firstname: "Jack", lastname: "Doo", cars: ["Corsa", "Golf"] },
        { firstname: "Julie", lastname: "Doo", cars: ["Fiesta", "Brasilia"] }
      ],
      fields: [
        { key: "firstname", label: "First name" },
        { key: "lastname", label: "Last name" },
        { key: "cars", label: "Cars" }
      ]
    };
  }
});
sssccc
sssccc

P粉111627787

您看到的問題是內容未被解釋,因此它顯示為文字。若要對此進行調整,請使用 cell(key) 插槽。然後在槽中,使用 v-html 來解釋字串作為 html。

new Vue({
  el: "#table",
  data() {
    return {
      items: [
        { firstname: "John", lastname: "Doe", cars: ["Ferrari", "Tesla"] },
        { firstname: "Jane", lastname: "Doe", cars: ["BMW", "Civic"] },
        { firstname: "Jack", lastname: "Doo", cars: ["Corsa", "Golf"] },
        { firstname: "Julie", lastname: "Doo", cars: ["Fiesta", "Brasilia"] }
      ],
      fields: [
        { key: "firstname", label: "First name" },
        { key: "lastname", label: "Last name" },
        { key: "cars", label: "Cars" }
      ]
    };
  },
  methods: {
    join() {
      for (let item of this.items) {
        item.cars = item.cars.join("
") } } } });
sssccc
sssccc

Join Array
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板