如何为Bootstrap Vue工具提示

时间:2019-04-12 19:33:19

标签: twitter-bootstrap vue.js bootstrap-vue

我有一些Bootstrap Vue工具提示,我想通过在对象数组中查找对象的v-for并填充<b-tooltip>属性的值来减少标记的占用空间。

我当前的工具提示之一

<b-tooltip v-if="clientInContext && taxId && hideTaxId"
  target="titlebar-taxId-eye-show"
  :disabled.sync="disableTaxIdButtonTooltip"
  :show.sync="showTaxIdButtonTooltip"
  triggers="hover"
  placement="bottom">
    Click to temporarily display this value
</b-tooltip>

我的V-For尝试:

<b-tooltip v-for='tooltip in tooltipContents'
  v-if="tooltip.vif"
  target="tooltip.target"
  :disabled.sync="tooltip.disable"
  :show.sync="tooltip.show"
  triggers="hover"
  placement="bottom">
    {{tooltip.text}}
</b-tooltip>
public tooltipContents: object[] = [
  {
    vif: 'clientInContext && ssn && hideSsn',
    target: 'titlebar-ssn-eye-show',
    disable: this.disableSsnButtonTooltip,
    show: this.showSsnButtonTooltip,
    text: 'Click to temporarily display this value'
  },
  {
    vif: 'clientInContext && ssn && !hideSsn',
    target: 'titlebar-ssn-eye-hide',
    disable: this.disableSsnButtonTooltip,
    show: this.showSsnButtonTooltip,
    text: 'Click to mask this value'
  },
  {
    vif: 'clientInContext && ssn && !hideSsn',
    target: 'titlebar-ssn',
    disable: this.disableSsnTextTooltip,
    show: this.showSsnTextTooltip,
    text: '{{ssn}}' 
  }
]

我不确定我还能做些什么来使它正常工作。并且尚未找到与此问题相关的在线信息。

1 个答案:

答案 0 :(得分:1)

我会将v-for放在div中,然后将v-if放在v-tooltip上。这不是与“一对一”的比较您的示例,但它显示了如何对每个工具提示进行检查。

CodePen镜像:https://codepen.io/oze4/pen/gyxrBY?editors=1010

new Vue({
  el: "#app",
  data: {
    tooltips: [{
        name: "tooltip1",
        target: "button-1",
        placement: "bottom",
        text: "Live"
      },
      {
        name: "tooltip2",
        target: "button-2",
        placement: "top",
        text: "Html"
      },
      {
        name: "tooltip3",
        target: "button-3",
        placement: "left",
        text: "Alternative"
      }
    ]
  },
  methods: {
    tooltipCheck(tooltip) {
      switch (tooltip) {
        case "tooltip1":
          {
            // do check for tooltip 1
            return true;
          }
        case "tooltip2":
          {
            // do check fo tooltip2
            return false;
          }
        case "tooltip3":
          {
            // do check for tooltip3
            return true;
          }
      }
    }
  }
});
<script src="https://unpkg.com/vue@2.5.17/dist/vue.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@2.0.0-rc.11/dist/bootstrap-vue.min.js"></script>

<link href="https://unpkg.com/bootstrap@4.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://unpkg.com/bootstrap-vue@2.0.0-rc.11/dist/bootstrap-vue.css" rel="stylesheet" />


<div id="app">
  <div style="margin-top: 40px;">
    <b-container fluid>
      <b-row style="margin-top: 10px;">
        <b-button id="button-1" variant="outline-success">One (check evaluates to true)</b-button>
      </b-row>
      <b-row style="margin-top: 10px;">
        <b-button id="button-2" variant="outline-success">Two (check evaluates to false)</b-button>
      </b-row>
      <b-row style="margin-top: 10px;">
        <b-button id="button-3" variant="outline-success">Three (check evaluates to true)</b-button>
      </b-row>
      <div v-for="(tt, index) in tooltips">
        <b-tooltip v-if="tooltipCheck(tt.name)" :target="tt.target" :placement="tt.placement">
          {{ tt.text }}
        </b-tooltip>
      </div>
    </b-container>
  </div>
</div>