当前位置:   article > 正文

uni-app:实现表格多选及数据获取_uniapp列表多选

uniapp列表多选

 效果:

 

 代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-x="true" style="overflow-x: scroll; white-space: nowrap;">
  4. <view class="table">
  5. <view class="table-tr">
  6. <view class="table-th1">
  7. <checkbox-group @tap="checkAll">
  8. <checkbox :checked="allChecked" />
  9. </checkbox-group>
  10. </view>
  11. <view class="table-th2">姓名</view>
  12. <view class="table-th2">年龄</view>
  13. <view class="table-th3">地点</view>
  14. </view>
  15. <view class="table-tr" v-for="(item, index) in list" :key="index">
  16. <view class="table-td1">
  17. <checkbox-group @change="checkClick(item)">
  18. <checkbox :checked="item.checked" />
  19. </checkbox-group>
  20. </view>
  21. <view class="table-td2">{{item.name}}</view>
  22. <view class="table-td2">{{item.age}}</view>
  23. <view class="table-td3">{{item.address}}</view>
  24. </view>
  25. </view>
  26. </scroll-view>
  27. <view>
  28. <text>数组数据</text>
  29. <view>{{selectedData}}</view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. selectedData: [],
  38. allChecked: false,
  39. inputs: "",
  40. list: [{
  41. name: "张三",
  42. age: 21,
  43. checked: false,
  44. address: '波兰斯维诺乌伊希切',
  45. },
  46. {
  47. name: "李四",
  48. age: 22,
  49. checked: false,
  50. address: '冰岛尼斯湖',
  51. },
  52. {
  53. name: "王五",
  54. age: 23,
  55. checked: false,
  56. address: '云南西双版纳',
  57. },
  58. {
  59. name: "赵六",
  60. age: 41,
  61. checked: false,
  62. address: '阿尔卑斯雪山',
  63. },
  64. ],
  65. }
  66. },
  67. methods: {
  68. // 单个的选择
  69. checkClick(item) {
  70. item.checked = !item.checked
  71. // console.log(item)
  72. if (item.checked) {
  73. this.selectedData.push(item);
  74. } else {
  75. const index = this.selectedData.findIndex(data => data === item);
  76. if (index !== -1) {
  77. this.selectedData.splice(index, 1);
  78. }
  79. }
  80. if (!item.checked) {
  81. this.allChecked = false
  82. } else {
  83. // 判断单个是否是被选择的状态
  84. const goods = this.list.every(item => {
  85. return item.checked === true
  86. })
  87. if (goods) {
  88. this.allChecked = true
  89. } else {
  90. this.allChecked = false
  91. }
  92. }
  93. console.log(this.selectedData)
  94. },
  95. //全选与全不选
  96. checkAll() {
  97. this.allChecked = !this.allChecked;
  98. if (this.allChecked) {
  99. this.list.map(item => {
  100. item.checked = true;
  101. if (!this.selectedData.includes(item)) {
  102. this.selectedData.push(item);
  103. }
  104. });
  105. } else {
  106. this.list.map(item => {
  107. item.checked = false;
  108. const index = this.selectedData.findIndex(data => data === item);
  109. if (index !== -1) {
  110. this.selectedData.splice(index, 1);
  111. }
  112. });
  113. }
  114. console.log(this.selectedData)
  115. }
  116. }
  117. }
  118. </script>
  119. <style>
  120. /* 表格样式 */
  121. .table {
  122. margin-top: 5%;
  123. font-size: 85%;
  124. display: table;
  125. width: 200%;
  126. border-collapse: collapse;
  127. box-sizing: border-box;
  128. }
  129. .table-tr {
  130. display: table-row;
  131. }
  132. .table-th1 {
  133. width: 5%;
  134. display: table-cell;
  135. font-weight: bold;
  136. border: 1rpx solid gray;
  137. background-color: #51aad6;
  138. text-align: center;
  139. vertical-align: middle;
  140. padding: 10rpx 0;
  141. overflow: hidden;
  142. text-overflow: ellipsis;
  143. word-break: break-all;
  144. }
  145. .table-th2 {
  146. width: 20%;
  147. display: table-cell;
  148. font-weight: bold;
  149. border: 1rpx solid gray;
  150. background-color: #51aad6;
  151. text-align: center;
  152. vertical-align: middle;
  153. padding: 10rpx 0;
  154. overflow: hidden;
  155. text-overflow: ellipsis;
  156. word-break: break-all;
  157. }
  158. .table-th3 {
  159. width: 50%;
  160. display: table-cell;
  161. font-weight: bold;
  162. border: 1rpx solid gray;
  163. background-color: #51aad6;
  164. text-align: center;
  165. vertical-align: middle;
  166. padding: 10rpx 0;
  167. overflow: hidden;
  168. text-overflow: ellipsis;
  169. word-break: break-all;
  170. }
  171. .table-td1 {
  172. width: 5%;
  173. display: table-cell;
  174. border: 1rpx solid gray;
  175. text-align: center;
  176. vertical-align: middle;
  177. padding: 10rpx 0;
  178. overflow: hidden;
  179. text-overflow: ellipsis;
  180. word-break: break-all;
  181. }
  182. .table-td2 {
  183. width: 20%;
  184. display: table-cell;
  185. border: 1rpx solid gray;
  186. text-align: center;
  187. vertical-align: middle;
  188. padding: 10rpx 0;
  189. overflow: hidden;
  190. text-overflow: ellipsis;
  191. word-break: break-all;
  192. }
  193. .table-td3 {
  194. width: 50%;
  195. display: table-cell;
  196. border: 1rpx solid gray;
  197. text-align: center;
  198. vertical-align: middle;
  199. overflow: hidden;
  200. text-overflow: ellipsis;
  201. word-break: break-all;
  202. /* padding: 5px 0; */
  203. }
  204. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/264052
推荐阅读
相关标签
  

闽ICP备14008679号