e-select.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <e-select-built-in
  3. v-model="selectData"
  4. :options="options"
  5. :props="props"
  6. :placeholder="placeholder"
  7. :width="width"
  8. :minWidth="minWidth"
  9. :maxHeight="maxHeight"
  10. :emptyTips="emptyTips"
  11. :clearable="clearable"
  12. :disabled="disabled"
  13. :search="search"
  14. :animation="animation"
  15. :position="position"
  16. :pageSize="pageSize"
  17. :pageIndex="pageIndex"
  18. @update:pageIndex="updatePageIndex"
  19. @update:total="updateTotal"
  20. @change="changeSelect">
  21. <uni-pagination-built-in
  22. v-if="pageSize"
  23. show-icon="true"
  24. :total="total"
  25. :pageSize="pageSize"
  26. :current="pageIndex"
  27. @change="changePage"></uni-pagination-built-in>
  28. </e-select-built-in>
  29. </template>
  30. <script>
  31. import eSelectBuiltIn from './e-select-built-in.vue';
  32. import uniPaginationBuiltIn from './uni-pagination-built-in/uni-pagination-built-in.vue';
  33. export default {
  34. components: {
  35. eSelectBuiltIn,
  36. uniPaginationBuiltIn,
  37. },
  38. props: {
  39. // vue2 v-model传值方式
  40. value: {
  41. type: [String, Number],
  42. default: '',
  43. },
  44. // vue3 v-model传值方式
  45. modelValue: {
  46. type: [String, Number],
  47. default: '',
  48. },
  49. // 选项列表
  50. options: {
  51. type: Array,
  52. default() {
  53. return [];
  54. },
  55. },
  56. // 选项列表自定义数据格式
  57. props: {
  58. type: Object,
  59. default: () => {
  60. return {
  61. text: 'text',
  62. value: 'value',
  63. disabled: 'disabled',
  64. };
  65. },
  66. },
  67. // 占位文本
  68. placeholder: {
  69. type: String,
  70. default: '请选择',
  71. },
  72. // 输入框宽度
  73. width: {
  74. type: String,
  75. default: '100%',
  76. },
  77. // 输入框最小宽度
  78. minWidth: {
  79. type: String,
  80. default: '120rpx',
  81. },
  82. // 选项列表悬浮框最大高度
  83. maxHeight: {
  84. type: String,
  85. default: '160px',
  86. },
  87. // 选项列表空值占位空值占位
  88. emptyTips: {
  89. type: String,
  90. default: '暂无选项',
  91. },
  92. // 是否可清空
  93. clearable: {
  94. type: Boolean,
  95. default: false,
  96. },
  97. // 是否禁用
  98. disabled: {
  99. type: Boolean,
  100. default: false,
  101. },
  102. // 是否开启搜索
  103. search: {
  104. type: Boolean,
  105. default: true,
  106. },
  107. // 是否开启搜索的滚动动画
  108. animation: {
  109. type: Boolean,
  110. default: true,
  111. },
  112. // 悬浮框位置top/bottom
  113. position: {
  114. type: String,
  115. default: 'bottom',
  116. },
  117. // 分页每页条数
  118. pageSize: {
  119. type: Number,
  120. default: 0,
  121. },
  122. },
  123. data() {
  124. return {
  125. // 前端分页,总数就是下拉列表总数,不应由用户来控制,放在组件内部
  126. total: 0,
  127. // 前端分页,当前分页不应由用户来控制,放在组件内部
  128. pageIndex: 1,
  129. selectData: '',
  130. };
  131. },
  132. watch: {
  133. options: {
  134. handler(val) {
  135. this.total = val.length;
  136. },
  137. immediate: true,
  138. deep: true,
  139. },
  140. modelValue: {
  141. handler() {
  142. this.initData();
  143. },
  144. immediate: true,
  145. },
  146. value: {
  147. handler() {
  148. this.initData();
  149. },
  150. immediate: true,
  151. },
  152. selectData: {
  153. handler(val) {
  154. this.$emit('input', val);
  155. this.$emit('update:modelValue', val);
  156. },
  157. },
  158. },
  159. methods: {
  160. /** 处理数据,此函数用于兼容vue2 vue3 */
  161. initData() {
  162. // vue2
  163. if (this.value || this.value === 0) {
  164. this.selectData = this.value;
  165. }
  166. // vue3
  167. else if (this.modelValue || this.modelValue === 0) {
  168. this.selectData = this.modelValue;
  169. }
  170. },
  171. updatePageIndex(pageIndex) {
  172. this.pageIndex = pageIndex;
  173. },
  174. updateTotal(total) {
  175. this.total = total;
  176. },
  177. changePage(data) {
  178. this.pageIndex = data.current;
  179. this.$emit('changePage', data);
  180. },
  181. changeSelect(data) {
  182. this.$emit('change', data);
  183. },
  184. },
  185. };
  186. </script>