siteList.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="siteList">
  3. <van-search
  4. v-model="value"
  5. show-action
  6. placeholder="请输入搜索关键词"
  7. @search="onSearch"
  8. @cancel="onCancel"
  9. />
  10. <van-list
  11. v-model:loading="loading"
  12. :finished="finished"
  13. finished-text="没有更多了"
  14. @load="onLoad"
  15. >
  16. <van-checkbox-group
  17. v-if="type == 1"
  18. v-model="checked"
  19. ref="checkboxGroup"
  20. >
  21. <van-button type="primary" size="mini" @click="checkAll"
  22. >全选</van-button
  23. >
  24. <van-cell-group inset>
  25. <van-cell
  26. v-for="(item, index) in list"
  27. clickable
  28. :key="item"
  29. :title="item.name"
  30. @click="toggle(index)"
  31. >
  32. <template #right-icon>
  33. <van-checkbox
  34. shape="square"
  35. :name="item"
  36. :ref="(el) => (checkboxRefs[index] = el)"
  37. />
  38. </template>
  39. </van-cell>
  40. </van-cell-group>
  41. </van-checkbox-group>
  42. <van-radio-group v-if="type == 0" v-model="checked">
  43. <van-cell-group inset>
  44. <van-cell
  45. v-for="item in list"
  46. clickable
  47. :key="item"
  48. :title="item.name"
  49. @click="selectes(item)"
  50. >
  51. <template #right-icon>
  52. <van-radio :name="item" />
  53. </template>
  54. </van-cell>
  55. </van-cell-group>
  56. </van-radio-group>
  57. </van-list>
  58. </div>
  59. </template>
  60. <script>
  61. import { ref } from "vue";
  62. import tools from "@/api/sys/tools";
  63. export default {
  64. name: "siteList",
  65. emits: ["selected"],
  66. props: ["type"],
  67. setup(props, { emit }) {
  68. const list = ref([]);
  69. const loading = ref(false);
  70. const finished = ref(false);
  71. let index = 0;
  72. const onLoad = () => {
  73. // 异步更新数据
  74. new tools()
  75. .placeList({
  76. current: index + 1,
  77. size: 10,
  78. })
  79. .then(({ records }) => {
  80. list.value.push(...records);
  81. // 加载状态结束
  82. loading.value = false;
  83. index++;
  84. // 数据全部加载完成
  85. if (records.length < 10) {
  86. finished.value = true;
  87. }
  88. });
  89. };
  90. // 搜索
  91. const value = ref("");
  92. const onSearch = (val) => showToast(val);
  93. const onCancel = () => showToast("取消");
  94. // 选择场所
  95. const checked = ref([]);
  96. const checkboxRefs = ref([]);
  97. const checkboxGroup = ref(null);
  98. const toggle = (index) => {
  99. checkboxRefs.value[index].toggle();
  100. emit("selected", checked.value, 1);
  101. };
  102. // 全选
  103. const checkAll = () => {
  104. checkboxGroup.value.toggleAll(true);
  105. emit("selected", checked.value, 1);
  106. };
  107. const selectes = (val) => {
  108. checked.value = val;
  109. emit("selected", checked.value, 0);
  110. };
  111. return {
  112. // 场所
  113. list,
  114. onLoad,
  115. loading,
  116. finished,
  117. // 搜索
  118. value,
  119. onSearch,
  120. onCancel,
  121. // 全选
  122. checkAll,
  123. toggle,
  124. checked,
  125. checkboxRefs,
  126. checkboxGroup,
  127. // 单选
  128. selectes,
  129. };
  130. },
  131. };
  132. </script>
  133. <style scoped>
  134. .siteList {
  135. height: 65vh;
  136. overflow: auto;
  137. margin: 10px;
  138. }
  139. .van-button {
  140. top: -5px;
  141. }
  142. .search {
  143. height: 40px;
  144. line-height: 40px;
  145. }
  146. .van-list {
  147. height: 80%;
  148. margin-top: 5px;
  149. }
  150. .keyword {
  151. width: 70%;
  152. height: 25px;
  153. border-radius: 25px;
  154. border: 1px solid;
  155. padding-left: 15px;
  156. }
  157. </style>