【问题标题】:Change text on dynamically created buttons with vue使用 vue 更改动态创建的按钮上的文本
【发布时间】:2020-10-28 11:51:10
【问题描述】:

我正在使用一组在 vue 中动态创建的按钮。 我的代码为数组中的每个对象创建一个按钮,并将按钮 id 设置为对象的 id。 现在我有另一个对象数组,其中包含每个按钮根据其 ID 应具有的文本。 我不知道如何将名称与 id 匹配。

感谢您的帮助!

            <b-row class="main-buttongroup-row1">
              <b-col
                lg="4"
                class="btn"
                v-for="Sub in Main[0].subs"
                :key="Sub.id"
              >
                <b-button
                  v-model="optionsButton"
                  :id="Sub.id"
                  @click="submit(Sub.id)"
                  >{{ optionsButton.text }}</b-button
                >
              </b-col>
            </b-row>

这就是我的数组“Main”的样子:

var Main = [
{
   id:3
   num: 3
   scale: 100
   subs: [
       { id: 5, count:2 }
       { id: 1, count:1 }
       { id: 2, count:2 }]
}]

这是需要匹配文本的数组:

  data() {
    return {
      showAlert: false,
      optionsButton: [
        { text: "text1", value: 1 },
        { text: "text2", value: 2 },
        { text: "text3", value: 3 },
        { text: "text4", value: 4 },
        { text: "text5", value: 5 },
        { text: "text6", value: 6 }
      ],
    };
  }

【问题讨论】:

    标签: vue.js bootstrap-vue


    【解决方案1】:

    您可以使用一种方法为您执行此操作,该方法将获取 id 并返回文本。

    模板

    <b-row class="main-buttongroup-row1">
       <b-col lg="4" class="btn" v-for="Sub in Main[0].subs" :key="Sub.id">
          <b-button v-model="optionsButton" :id="Sub.id" @click="submit(Sub.id)">{{ getButtonText(Sub.id) }}</b-button>
       </b-col>
    </b-row>
    

    .Vue

    data() {
      return {
        showAlert: false,
        optionsButton: [
          { text: "text1", value: 1 },
          { text: "text2", value: 2 },
          { text: "text3", value: 3 },
          { text: "text4", value: 4 },
          { text: "text5", value: 5 },
          { text: "text6", value: 6 }
        ],
        methods: {
          getButtonText(id) {
            return this.optionsButton.filter(opts => opts.value === id)[0].text
          }
        }
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 2016-09-07
      • 2013-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      相关资源
      最近更新 更多