Vue Bootstrap表格:列中的按钮未呈现

huangapple go评论64阅读模式
英文:

Vue bootstrap table: button in column not rendering

问题

我有一个使用Vue Bootstrap的表格,其中有一个名为"cosimo"的列,其中包含一个按钮,用于启动一个模态框。然而,按钮未正确呈现并且在点击时不起作用。以下是该列的代码:

columns: [
  {
    title: "Cosimo",
    field: "component.name",
    sortable: true,
    formatter: (value, row, index) => {
      return `
        <b-button v-b-modal.modal-1 @click="showModal">
          Launch demo modal
        </b-button>
      `;
    }
  }, ...]

我已经查看了Vue Bootstrap文档,并相信我已经遵循了正确的语法来创建一个启动模态框的按钮。然而,按钮不起作用,HTML未正确呈现。有谁能帮助我找出问题所在吗?提前感谢!在模板部分以外的地方可以正常工作。

编辑
我使用以下代码呈现该列:

<bootstrap-table
  ref="table"
  :columns="columns"
  :data="data"
  :options="options"
  @on-load-success="tableLoaded"
>
  <template v-slot:cell(component.name)>
    <span><b-btn @click="showModal(item)">Launch Demo Modal</b-btn></span>
  </template>
</bootstrap-table>

然后是列的定义:

{
  title: "Cosimo Recommendation",
  field: "component.name",
  sortable: true
},
英文:

I have a Vue Bootstrap table with a "cosimo" column that includes a button to launch a modal. However, the button is not rendering properly and does not work when clicked. Here is the code for the column:

columns: [
{
  title: &quot;Cosimo&quot;,
  field: &quot;component.name&quot;,
  sortable: true,
  formatter: (value, row, index) =&gt; {
    return `
      &lt;b-button v-b-modal.modal-1 @click=&quot;showModal&quot;
        &gt;Launch demo modal&lt;/b-button
      &gt;
  `;
  }
}, ...]

I've checked the Vue Bootstrap documentation and I believe I've followed the correct syntax for creating a button that launches a modal. However, the button is not working and the HTML is not rendering properly. Can anyone help me identify the issue? Thanks in advance! It works outside the table in the template part.

EDIT
I am rendering the column with this code:

&lt;bootstrap-table
  ref=&quot;table&quot;
  :columns=&quot;columns&quot;
  :data=&quot;data&quot;
  :options=&quot;options&quot;
  @on-load-success=&quot;tableLoaded&quot;
&gt;
  &lt;template v-slot:cell(component.name)&gt;
    &lt;span&gt;&lt;b-btn @click=&quot;showModal(item)&quot;&gt;Launch Demo Modal&lt;/b-btn&gt;&lt;/span&gt;
  &lt;/template&gt;
&lt;/bootstrap-table&gt;

And then the columns entry:

        {
      title: &quot;Cosimo Recommendation&quot;,
      field: &quot;component.name&quot;,
      sortable: true
    },

答案1

得分: 2

我不确定您正在使用的bootstrap-vue版本,但如果您使用的是2.23.0版本,那么您可以使用插槽来添加按钮和其他HTML元素,可以查看文档中关于信息模型按钮的示例:

https://bootstrap-vue.org/docs/components/table#complete-example

英文:

I'm not sure which version of bootstrap-vue you're using, but if you're on version 2.23.0, then you can use slots for adding buttons and other HTML elements, see below example of info model button in documentation:

https://bootstrap-vue.org/docs/components/table#complete-example

答案2

得分: 1

你可以通过在模板本身中使用 slot 来实现特定列的目标。

<b-table :items="items" :fields="fields">
    <template v-slot:cell(<component.name>)="{ item }">
        <span><b-btn @click="showModal(item)">启动演示模态窗口</b-btn></span>
    </template>
</b-table>

你可以在这里看到 cell({key}) 插槽的使用示例。

英文:

You can achieve this by using slot in the template itself for that particular column.

&lt;b-table :items=&quot;items&quot; :fields=&quot;fields&quot;&gt;
    &lt;template v-slot:cell(&lt;component.name&gt;)=&quot;{ item }&quot;&gt;
      &lt;span&gt;&lt;b-btn @click=&quot;showModal(item)&quot;&gt;Launch Demo Modal&lt;/b-btn&gt;&lt;/span&gt;
    &lt;/template&gt;
&lt;/b-table&gt;

You can see cell({key}) slot here

huangapple
  • 本文由 发表于 2023年2月19日 14:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定