personList.vue 4.0 KB

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