图书馆小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
5.9 KiB

2 weeks ago
  1. <template>
  2. <view class="item-container">
  3. <!-- 图书数量 + 右侧设置按钮 -->
  4. <view class="count-text">
  5. <text>图书数量 ({{ bookList.length }})</text>
  6. <text class="edit-btn" @click="toggleEdit">{{ editMode ? "完成" : "管理" }}</text>
  7. </view>
  8. <checkbox-group @change="checkboxChange">
  9. <view class="car-list" v-for="item in bookList" :key="item.isbn">
  10. <checkbox :value="item.isbn" :checked="item.checked" />
  11. <view class="book-item-box">
  12. <view class="item-box-left">
  13. <image class="img-item" :src="item.imgCover" mode="aspectFill" />
  14. </view>
  15. <view class="item-box-right">
  16. <view class="item-title line-clamp-2">{{ item.title || '暂无标题' }}</view>
  17. <view class="tag-box">
  18. <text class="item-author">{{ item.nickname || '佚名' }}</text>
  19. <text class="item-publish">{{ item.publish || '暂无出版社' }}</text>
  20. </view>
  21. <view class="item-desc line-clamp-2">{{ item.desc || '暂无简介' }}</view>
  22. </view>
  23. </view>
  24. </view>
  25. </checkbox-group>
  26. <view class="bottom-placeholder"></view>
  27. <view class="car-bottom">
  28. <!-- 全选 -->
  29. <view class="all-check" @click="handleAllCheck">
  30. <checkbox :checked="isAllChecked" />
  31. <text style="margin-left:6px">全选</text>
  32. </view>
  33. <!-- 根据编辑状态切换按钮续借 / 删除 -->
  34. <button
  35. class="join-btn"
  36. @click="editMode ? handleDelete() : handleRenew()"
  37. :disabled="!hasChecked"
  38. >
  39. {{ editMode ? "删除选中" : "一键续借" }}
  40. </button>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. export default {
  46. data() {
  47. return {
  48. editMode: false, // 控制编辑/删除状态
  49. bookList: [
  50. {
  51. imgCover: "https://qiniu.aiyxlib.com/1606124577077",
  52. title: "名侦探柯南",
  53. nickname: "青山刚昌",
  54. publish: "长春出版社",
  55. isbn: "1001",
  56. desc: "测试图书简介",
  57. checked: true,
  58. },
  59. {
  60. imgCover: "https://qiniu.aiyxlib.com/1606124577077",
  61. title: "测试图书",
  62. nickname: "测试作者",
  63. publish: "测试出版社",
  64. isbn: "1002",
  65. desc: "测试简介",
  66. checked: true,
  67. },
  68. {
  69. imgCover: "https://qiniu.aiyxlib.com/1606124577077",
  70. title: "测试图书",
  71. nickname: "测试作者",
  72. publish: "测试出版社",
  73. isbn: "1003",
  74. desc: "测试简介",
  75. checked: false,
  76. }
  77. ],
  78. };
  79. },
  80. computed: {
  81. // 是否全选
  82. isAllChecked() {
  83. return this.bookList.every((item) => item.checked);
  84. },
  85. // 是否有选中
  86. hasChecked() {
  87. return this.bookList.some((item) => item.checked);
  88. },
  89. },
  90. methods: {
  91. // 切换编辑模式
  92. toggleEdit() {
  93. this.editMode = !this.editMode;
  94. // 退出编辑时可以取消所有选中(可选)
  95. if (!this.editMode) {
  96. this.bookList.forEach(item => item.checked = false);
  97. }
  98. },
  99. checkboxChange(e) {
  100. console.log("checkbox选中value:", e.detail.value);
  101. const values = e.detail.value;
  102. const items = this.bookList;
  103. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  104. items[i].checked = false;
  105. for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
  106. if (items[i].isbn === values[j]) {
  107. items[i].checked = true;
  108. break;
  109. }
  110. }
  111. }
  112. this.bookList = items;
  113. },
  114. handleAllCheck() {
  115. const isAll = this.isAllChecked;
  116. this.bookList.forEach((item) => {
  117. item.checked = !isAll;
  118. });
  119. },
  120. // 一键续借
  121. handleRenew() {
  122. const checkedBooks = this.bookList.filter((item) => item.checked);
  123. uni.showToast({
  124. title: `已续借 ${checkedBooks.length}`,
  125. icon: "success",
  126. });
  127. },
  128. // 删除选中图书
  129. handleDelete() {
  130. const beforeLen = this.bookList.length;
  131. // 过滤掉选中的项
  132. this.bookList = this.bookList.filter(item => !item.checked);
  133. const deleteCount = beforeLen - this.bookList.length;
  134. uni.showToast({
  135. title: `已删除 ${deleteCount}`,
  136. icon: "success"
  137. });
  138. },
  139. },
  140. };
  141. </script>
  142. <style lang="scss" scoped>
  143. .item-container {
  144. padding: 10px;
  145. background: #f5f6f7;
  146. min-height: 100vh;
  147. }
  148. .count-text {
  149. margin-bottom: 10px;
  150. display: flex;
  151. justify-content: space-between; /* 数量左,设置右 */
  152. align-items: center;
  153. }
  154. .edit-btn {
  155. color: #01a4fe;
  156. font-size: 14px;
  157. }
  158. .car-list {
  159. display: flex;
  160. align-items: flex-start;
  161. margin-bottom: 10px;
  162. checkbox {
  163. margin-top: 15px;
  164. padding: 0 10px;
  165. }
  166. }
  167. .book-item-box {
  168. flex: 1;
  169. background: #fff;
  170. border-radius: 8px;
  171. padding: 12px;
  172. display: flex;
  173. .item-box-left {
  174. margin-right: 12px;
  175. .img-item {
  176. width: 64px;
  177. height: 90px;
  178. border-radius: 6px;
  179. }
  180. }
  181. .item-box-right {
  182. flex: 1;
  183. }
  184. .item-title {
  185. font-weight: bold;
  186. margin-bottom: 4px;
  187. }
  188. .item-author,
  189. .item-publish {
  190. font-size: 12px;
  191. background: #f4f6fc;
  192. padding: 2px 6px;
  193. border-radius: 4px;
  194. margin-right: 6px;
  195. }
  196. .item-desc {
  197. font-size: 12px;
  198. color: #999;
  199. margin-top: 6px;
  200. }
  201. }
  202. .bottom-placeholder {
  203. height: 60px;
  204. }
  205. .car-bottom {
  206. position: fixed;
  207. left: 0;
  208. bottom: 0;
  209. right: 0;
  210. background: #fff;
  211. padding: 12px 15px;
  212. display: flex;
  213. justify-content: space-between;
  214. align-items: center;
  215. box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.05);
  216. }
  217. .all-check {
  218. display: flex;
  219. align-items: center;
  220. }
  221. .join-btn {
  222. font-size: 14px;
  223. color: #fff;
  224. margin: 0 !important;
  225. background-color: #01a4fe !important;
  226. border-radius: 23px;
  227. padding: 0 30px;
  228. &::after{
  229. border: none !important;
  230. }
  231. &[disabled] {
  232. background: #ccc !important;
  233. }
  234. }
  235. </style>