• 全选反选案例:


    1. <template>
    2. <div id="app">
    3. <button v-for="(item, index) in list" @click="checkFn(item)" :class="isActiveFn(item)?'active':''">{{ item.value }}</button>
    4. <button @click="selectAll">全选</button>
    5. <button @click="reverseSelect">反选</button>
    6. </div>
    7. </template>
    8. <script>
    9. export default {
    10. name: "app",
    11. data:function () {
    12. return {
    13. list: [
    14. {
    15. id: 0,
    16. value: "1"
    17. },
    18. {
    19. id: 1,
    20. value: "2"
    21. },
    22. {
    23. id: 2,
    24. value: "3"
    25. },
    26. {
    27. id: 3,
    28. value: "4"
    29. },
    30. ],
    31. checkedList: []
    32. }
    33. },
    34. methods: {
    35. isActiveFn:function (item) {
    36. let index = this.checkedList.findIndex(el => {
    37. return item.value === el.value;
    38. })
    39. if (index !== -1) {
    40. return true
    41. } else {
    42. return false
    43. }
    44. },
    45. checkFn:function (item) {
    46. let index = this.checkedList.findIndex(el => {
    47. return el.value === item.value
    48. })
    49. if (index === -1) {
    50. this.checkedList.push(item)
    51. } else {
    52. this.checkedList.splice(index, 1)
    53. }
    54. },
    55. selectAll:function () {
    56. // 这里使用深拷贝 , 切忌使用 this.checkedList = this.list , 这样会发生引用的问题
    57. this.checkedList = JSON.parse(JSON.stringify(this.list))
    58. },
    59. reverseSelect:function () {
    60. let parseChecked = []
    61. this.list.forEach(item => {
    62. let index = this.checkedList.findIndex(el => {
    63. return el.value === item.value
    64. })
    65. if (index === -1) {
    66. parseChecked.push(item)
    67. }
    68. })
    69. this.checkedList = parseChecked
    70. }
    71. }
    72. }
    73. </script>
    74. <style lang="less" scoped>
    75. button {
    76. margin: 10px;
    77. }
    78. .active {
    79. background-color: chartreuse;
    80. }
    81. </style>

  • 相关阅读:
    Typora 窗口总数跳动问题
    Lint-staged自动修复格式错误及小结
    Flux、Atomic、Proxy 不同心智模型状态管理库的比较和原理
    LeetCode01
    #ArrayList 和 LinkedList 的区别(超容易理解)
    云IDE开发产品
    GET和POST请求的区别
    SpringBoot-配置优先级
    【Leetcode】1825. Finding MK Average
    Mybatis基础
  • 原文地址:https://blog.csdn.net/qq_46344419/article/details/127787663