如果在框列表上单击,如何添加选定的按钮?

2023-12-06

我从这里得到参考:

https://bootstrap-vue.js.org/docs/components/card/#card-groups

https://bootstrap-vue.js.org/docs/components/button/#pressed-state-and-toggling

我的 vue 组件是这样的:

<template>
    ...
        <b-card-group deck v-for="row in formattedItems">
            <b-card :title="item.title"
                    img-src="http://placehold.it/140?text=No image"
                    img-alt="Img"
                    img-top
                    v-for="item in row">
                <p class="card-text">
                    {{item.price}}
                </p>
                <p class="card-text">
                    {{item.country}}
                </p>
                <div slot="footer">
                    <b-button-group size="sm">
                        <b-button :pressed.sync="oriPress" variant="outline-primary">Original</b-button>
                        <b-button :pressed.sync="kwPress" variant="outline-primary">Kw</b-button>
                    </b-button-group>
                    <b-btn variant="primary" block>Add</b-btn>
                </div>
            </b-card>
        </b-card-group>
    ....
</template>

<script>
    export default {
        ..
        data () {
            return{
                items: [
                     {id:1, title:'chelsea', price:900, country: 'england'},
                     {id:2, title:'liverpool', price:800, country: 'england'},
                     {id:3, title:'mu', price:700, country: 'england'},
                     {id:4, title:'city', price:600, country: 'england'},
                     {id:5, title:'arsenal', price:500, country: 'england'},
                     {id:6, title:'tottenham', price:400, country: 'england'},
                     {id:7, title:'juventus', price:300, country: 'italy'},
                     {id:8, title:'madrid', price:200, country: 'span'},
                     {id:9, title:'barcelona', price:100, country: 'span'},
                     {id:10, title:'psg', price:50, country: 'france'}
                ],
                oriPress: true,
                kwPress: false
            }
        },
        mounted: function () {
            this.getItems()
        },
        computed: {
            formattedItems() {
                return this.items.reduce((c, n, i) => {
                    if (i % 4 === 0) c.push([]);
                    c[c.length - 1].push(n);
                    return c;
                }, []);
            }
        }
    }
</script>

如果执行脚本,则所有框中的原始按钮均处于活动状态,而所有框中的按钮 kw 均处于非活动状态

这正是我所期望的。但我的问题是,当我单击 kw 按钮或原始按钮时,所有按钮都处于活动状态或不活动状态。我希望它仅在每个框中选择的按钮上激活

例如有10个盒子。当我单击第三个框中的原始按钮时,第三个框中的原始按钮将处于活动状态,而 kw 按钮将处于非活动状态。当我单击第九个框中的按钮 kw 时,第九个框中的按钮 kw 将处于活动状态,而原始按钮将处于非活动状态。其他人也一样

我该怎么做?


问题是一样的oriPress and kwPress数据属性用于所有项目。您可以将这些属性移至items[]这样它们对于每个项目都是唯一的:

// script
data() {
  return {
    items: [
      {id: 1, oriPress: true, kwPress: false, ...},
      {id: 2, oriPress: true, kwPress: false, ...},
    ]
  }
}

//template
<b-card-group v-for="row in formattedItems">
  <b-card v-for="item in row">

      <b-button :pressed.sync="item.oriPress">Original</b-button>
      <b-button :pressed.sync="item.kwPress">Kw</b-button>

  </b-card>
</b-card-group>

...但我假设的形状items[]无法更改。另一种方法是创建一个地图oriPress and kwPress属性(每项一个)。这可以通过watcher on items[]初始化oriPress and kwPress每个项目 ID 到布尔值的映射:

// script
data() {
  return {
    items: [...],
    oriPress: {},
    kwPress: {},
  }
},
watch: {
  items: {
    immediate: true,
    handler(value) {
      this.oriPress = value.reduce((p,c) => {
        p[c.id] = true;
        return p;
      }, {});

      this.kwPress = value.reduce((p,c) => {
        p[c.id] = false;
        return p;
      }, {});
    }
  }
},


//template
<b-card-group v-for="row in formattedItems">
  <b-card v-for="item in row">

      <b-button :pressed.sync="oriPress[item.id]">Original</b-button>
      <b-button :pressed.sync="kwPress[item.id]">Kw</b-button>

  </b-card>
</b-card-group>
new Vue({
  el: '#app',
  data() {
    return{
      items: [
        {id:1, title:'chelsea', price:900, country: 'england'},
        {id:2, title:'liverpool', price:800, country: 'england'},
        {id:3, title:'mu', price:700, country: 'england'},
        {id:4, title:'city', price:600, country: 'england'},
        {id:5, title:'arsenal', price:500, country: 'england'},
        {id:6, title:'tottenham', price:400, country: 'england'},
        {id:7, title:'juventus', price:300, country: 'italy'},
        {id:8, title:'madrid', price:200, country: 'span'},
        {id:9, title:'barcelona', price:100, country: 'span'},
        {id:10, title:'psg', price:50, country: 'france'}
      ],
      oriPress: {},
      kwPress: {}
    }
  },
  watch: {
    items: {
      immediate: true,
      handler(value) {
        this.oriPress = value.reduce((p,c) => {
          p[c.id] = true;
          return p;
        }, {});
        this.kwPress = value.reduce((p,c) => {
          p[c.id] = false;
          return p;
        }, {});
      }
    }
  },
  computed: {
    formattedItems() {
      return this.items.reduce((c, n, i) => {
        if (i % 4 === 0) c.push([]);
        c[c.length - 1].push(n);
        return c;
      }, []);
    }
  }
})
<script src="https://unpkg.com/[email protected]"></script>

<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css"/>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css"/>
<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>

<div id="app">
  <b-card-group deck v-for="row in formattedItems">
    <b-card :title="item.title"
            img-src="http://placehold.it/140?text=No image"
            img-alt="Img"
            img-top
            v-for="item in row">
      <p class="card-text">
        {{item.price}}
      </p>
      <p class="card-text">
        {{item.country}}
      </p>
      <div slot="footer">
        <b-button-group size="sm">
          <b-button :pressed.sync="oriPress[item.id]" variant="outline-primary">Original</b-button>
          <b-button :pressed.sync="kwPress[item.id]" variant="outline-primary">Kw</b-button>
        </b-button-group>
        <b-btn variant="primary" block>Add</b-btn>
      </div>
    </b-card>
  </b-card-group>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果在框列表上单击,如何添加选定的按钮? 的相关文章

  • 如何以马赛克风格对齐图像? html/css

    我想包含一组马赛克风格的图像 它们全部通过 HTML5 CSS 组合在一起 我还使用 Bootstrap 和各种行 列和 div 来构建和定位内容 但是我无法将图像分组在一起 我已经能够将它们对齐到一定程度 但它们仍然不匹配等 请参阅附图了
  • d3-sankey 链接在拖动事件时未更新

    问题一 我无法更新拖动事件上的 d3 sankey 链接 我正在尝试复制与此类似的 d3 垂直和水平拖动事件 https bl ocks org d3noob 5028304 https bl ocks org d3noob 5028304
  • 使用 bootstrap-vue 组件和 bootstrap 3 动态显示/隐藏列

    我目前正在尝试使用 bootstrap vue 表动态显示 隐藏 https bootstrap vue js org docs components table https bootstrap vue js org docs compon
  • 谷歌地图上的自定义标记

    问题详情 显示默认图标 我期待标记的自定义图标 我在下面的代码中遗漏了什么吗 我正在使用 Laravel 5 6 12 和 vue js 并尝试在谷歌地图中显示自定义图标 我的代码在模板下面
  • Bootstrap Typeahead:删除第一项的强制选择

    您好 我正在 Twitter Bootstrap 中使用 typeahead 我在这里发现 在自动完成下拉列表中 它默认选择第一个选项 我的要求是 最初它不应该选择任何内容 只有当我按下导航键 向上或向下 或将鼠标悬停在它上面时 它才会进入
  • 使用 ESC 关闭引导模式

    我正在使用 2 个模态 第一个模态包含一个表单 第二个模态在表单中发生错误时显示 第二个模式仅包含带有错误消息的文本 My problem is that when 2nd modal show up and I press Esc the
  • 将 Bootstrap config.json 文件与 Bower 结合使用

    我使用了 Bootstrap 定制器工具 http getbootstrap com customize http getbootstrap com customize 它生成一个config json每当我需要进行更改时我都会重复使用的文
  • 取消选择单选选项

    我正在使用引导单选按钮 http getbootstrap com javascript buttons并希望允许取消选择无线电组 这可以使用额外的按钮来完成 Fiddle http jsfiddle net norlihazmeyGhaz
  • 关闭表单多选按钮单击 vuejs

    我有 vue 材质多选的 html 设计 我希望单击按钮即可关闭多选选项 Here is my design 我试过这个 methods selectAgents this refs selectAgent el children 0 cl
  • VueJS vue-spinner 属性或方法“加载”未定义

    你好 我正在尝试设置view spinner https www npmjs com package vue spinner 但是出现错误 vue esm js 591 Vue warn 属性或方法 loading 未在实例上定义 但在渲染
  • 两列 Jekyll 布局,用标签分隔?

    我目前正在 Jekyll 博客上工作 我想将我的 Markdown 文件采用以下格式 div class row div class col md 6 div div class col md 6 div div 我希望我的代码块在一列中
  • Vue 和 Axios + AWS API 网关和 Lambda - CORS + POST 不起作用

    我正在尝试通过 AWS API Gateway 创建一个 API 测试函数 以及一个通过 Axios 的 Vue 应用程序调用的 Lambda 函数 它应该从输入元素发送姓名和电子邮件 每次我收到此错误时 Access to XMLHttp
  • Vuex 2.0 调度与提交

    有人可以解释一下什么时候使用调度和提交吗 我理解提交会触发突变 调度会触发操作 然而 派遣不也是一种行动吗 正如你所说 dispatch触发一个动作 并且commit触发突变 以下是如何使用这些概念 你总是用 dispatch来自路线 组件
  • 从 Axios 响应中解析 XML,推送到 Vue 数据数组

    在我的 Vue 应用程序中 我使用 Axios 获取 XML 文件并使用parseString将 XML 解析为 JSON 然后我需要通过result到 Vue 数据 this events My console log将解析后的 XML
  • Bootstrap轮播不滑动

    我一直在尝试使用 Bootstrap Carousel 并在一定程度上取得了成功 我也可以单击并更改图像 但我有一个问题 只是不滑动而已 我哪里做错了 html div class carousel slide ol class carou
  • vuejs 2.0.0 中选择的占位符

    我正在使用创建一个网络应用程序vuejs 2 0 https vuejs org guide forms html 我使用以下代码创建了简单的选择输入
  • Twitter Bootstrap Modal 无法在 Rails 中工作

    我还很新Rails and to twitter bootstrap 我正在研究模态 但我收到了 NoMethodError 未定义的方法 渲染 当我删除 代码来自my release js erb页面 它消失但没有窗口出现 另外 还有错误
  • Vue 多个组件位于一个包/文件中

    假设我想创建一个 UI 包 如何将多个组件放入一个 JS 文件中 通常我会将不同的组件放在不同的文件中 import ButtonText from ButtonText vue import ButtonIcon from ButtonI
  • 下拉菜单导致滚动条

    我用过这个W3C 的示例 http www w3schools com bootstrap bootstrap dropdowns asp div class dropdown div
  • 如何检查我的 create-nuxt-app 版本并升级?

    背景 以前 运行yarn create nuxt app myApp 会安装Nuxt v2 4 0 但今天我注意到您降级到Nuxt v2 0 0 我没有改变开发环境 所以我无法理解这种行为 当我发现这个问题时 我做了一些搜索并在其他地方抱怨

随机推荐