vue selectall复选框不确定绑定

发布于 2025-02-07 17:20:26 字数 2483 浏览 0 评论 0原文

我有这样的表,我想做的是应用indeterminate.prop,因此,如果选择了全部,则检查复选框,如果检查了1个复选框:indeterminate.prop 我想申请。我该如何写这个?我无法写下条件。

坦率地说,无法用VUE JS编写简单简单的条件使我有些紧张。

Stackoverflow要求提供很多细节,但我不知道该写什么

new Vue({
  el: "#app",
  data: {
    selected: [],
    messages: [{
        id: 1,
        text: "Learn JavaScript",
        status: 'read'
      },
      {
        id: 2,
        text: "Learn Vue",
        status: 'unread'
      },
      {
        id: 3,
        text: "Play around in JSFiddle",
        status: 'read'
      },
      {
        id: 4,
        text: "Build something awesome",
        status: 'unread'
      }
    ]
  },
  computed: {

    selectAll: {
      get: function() {
        return this.messages ? this.selected.length == this.messages.length : false;
      },
      set: function(value) {
        var selected = [];
        if (value) {
          this.messages.forEach(function(item) {
            selected.push(item.id);
          });
        }
        this.selected = selected;
      }
    }
  },
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">
  <table class="table">
    <a href="javascript:void(0);" v-model="selectAll" class="deleteMessages">All,</a>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="selectAll"></th>
        <th>Status</th>
        <th>Message</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item,index) in messages">
        <td><input type="checkbox" v-model="selected" :value="item.id"></td>
        <td>{{ item.status }}</td>
        <td>{{ item.text }}</td>
      </tr>
    </tbody>
  </table>

</div>

i have a table like this what i want to do is apply indeterminate.prop, so if all are selected, checkbox is checked,if even 1 checkbox is checked :indeterminate.prop
I want to apply. How can I write this? I couldn't write the condition.

Frankly, not being able to write a simple and easy condition with vue js made me a little nervous.

stackoverflow asks for a lot of details but I don't know what to write

new Vue({
  el: "#app",
  data: {
    selected: [],
    messages: [{
        id: 1,
        text: "Learn JavaScript",
        status: 'read'
      },
      {
        id: 2,
        text: "Learn Vue",
        status: 'unread'
      },
      {
        id: 3,
        text: "Play around in JSFiddle",
        status: 'read'
      },
      {
        id: 4,
        text: "Build something awesome",
        status: 'unread'
      }
    ]
  },
  computed: {

    selectAll: {
      get: function() {
        return this.messages ? this.selected.length == this.messages.length : false;
      },
      set: function(value) {
        var selected = [];
        if (value) {
          this.messages.forEach(function(item) {
            selected.push(item.id);
          });
        }
        this.selected = selected;
      }
    }
  },
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">
  <table class="table">
    <a href="javascript:void(0);" v-model="selectAll" class="deleteMessages">All,</a>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="selectAll"></th>
        <th>Status</th>
        <th>Message</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item,index) in messages">
        <td><input type="checkbox" v-model="selected" :value="item.id"></td>
        <td>{{ item.status }}</td>
        <td>{{ item.text }}</td>
      </tr>
    </tbody>
  </table>

</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所的.畏惧 2025-02-14 17:20:26

要选择并取消选择所有复选框,您需要正确删除该计算的该复选框,并添加单击事件,该事件所有ID,或从所选的所有ID中删除所有ID。

new Vue({
  el: "#app",
  data: {
    selected: [],
    messages: [{
        id: 1,
        text: "Learn JavaScript",
        status: 'read'
      },
      {
        id: 2,
        text: "Learn Vue",
        status: 'unread'
      },
      {
        id: 3,
        text: "Play around in JSFiddle",
        status: 'read'
      },
      {
        id: 4,
        text: "Build something awesome",
        status: 'unread'
      }
    ]
  },
  methods: {
  selectAll(){
  if(this.selected.length !== this.messages.length){
this.selected = this.messages.map(e=>e.id)
  
  }else {
   this.selected = []
  }
}

}

 
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">

<h1>Selected ids: {{ selected }}</h1>
 <button @click="selectAll">{{selected.length === messages.length ?'Unselect all' : 'Select All' }}</button>
  <table class="table">
   
    <thead>
      <tr>
        <th>Status</th>
        <th>Message</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item,index) in messages">
        <td><input type="checkbox" v-model="selected" :value="item.id"></td>
        <td>{{ item.status }}</td>
        <td>{{ item.text }}</td>
      </tr>
    </tbody>
  </table>

</div>

To select and unselect all checkboxes, you need to remove that computed properly and add a click event, which all ids, or remove all ids from selected.

new Vue({
  el: "#app",
  data: {
    selected: [],
    messages: [{
        id: 1,
        text: "Learn JavaScript",
        status: 'read'
      },
      {
        id: 2,
        text: "Learn Vue",
        status: 'unread'
      },
      {
        id: 3,
        text: "Play around in JSFiddle",
        status: 'read'
      },
      {
        id: 4,
        text: "Build something awesome",
        status: 'unread'
      }
    ]
  },
  methods: {
  selectAll(){
  if(this.selected.length !== this.messages.length){
this.selected = this.messages.map(e=>e.id)
  
  }else {
   this.selected = []
  }
}

}

 
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}
#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">

<h1>Selected ids: {{ selected }}</h1>
 <button @click="selectAll">{{selected.length === messages.length ?'Unselect all' : 'Select All' }}</button>
  <table class="table">
   
    <thead>
      <tr>
        <th>Status</th>
        <th>Message</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item,index) in messages">
        <td><input type="checkbox" v-model="selected" :value="item.id"></td>
        <td>{{ item.status }}</td>
        <td>{{ item.text }}</td>
      </tr>
    </tbody>
  </table>

</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文