qianziyu-select.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view>
  3. <u-popup :show="show" @close="cancel">
  4. <view class="title">{{popupTitle}}</view>
  5. <view style="padding: 20rpx;">
  6. <u-search v-if="showSearch" @custom="search" @search="search" :placeholder="placeholder"
  7. v-model="keyword"></u-search>
  8. <u-gap v-if="showSearch" height="15"></u-gap>
  9. <scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="$emit('lower')">
  10. <!--单选-->
  11. <u-radio-group v-if="type == 'radio'" :borderBottom="true" iconPlacement="right" placement="column"
  12. @change="groupChange" v-model="radioValue">
  13. <u-radio :customStyle="{marginBottom: '12px'}" v-for="(item, index) in dataLists" :key="index"
  14. :label="item[name]" :name="index">
  15. </u-radio>
  16. </u-radio-group>
  17. <!--多选-->
  18. <u-checkbox-group v-if="type == 'checkbox'" :borderBottom="true" placement="column"
  19. iconPlacement="right" @change="checkboxChange" v-model="checkboxValue">
  20. <u-checkbox :customStyle="{marginBottom: '12px',paddingBottom:'12px'}"
  21. v-for="(item, index) in dataLists" :key="index" :label="item[name]" :name="index">
  22. </u-checkbox>
  23. </u-checkbox-group>
  24. </scroll-view>
  25. <u-gap height="45"></u-gap>
  26. <view class="bottons">
  27. <u-row>
  28. <u-col customStyle="padding:0 10rpx 20rpx 20rpx" span="6">
  29. <u-button @click="cancel">取消</u-button>
  30. </u-col>
  31. <u-col customStyle="padding:0 20rpx 20rpx 10rpx" span="6">
  32. <u-button @click="submit" type="primary" throttleTime="1000" :disabled="(JSON.stringify(radioData) === '{}') && (checkboxData.length === 0)">确认</u-button>
  33. </u-col>
  34. </u-row>
  35. </view>
  36. </view>
  37. </u-popup>
  38. </view>
  39. </template>
  40. <script>
  41. /**
  42. * 公共选择下拉框,基于uview。支持下拉加载、列表搜索、单选|多选
  43. * @author qianziyu
  44. * @description 弹出层选择器,基于uview中u-popup实现
  45. * @property {Array} dataLists 数据列表
  46. * @property {String} name 列表显示的字段名
  47. * @property {Boolean} show 是否展示弹窗 (默认 false )
  48. * @property {String} type 选择类型 单选|多选 (默认 单选 )
  49. * @property {Boolean} showSearch 是否显示搜索框 (默认 true )
  50. * @property {String} popupTitle 列表标题
  51. * @property {String} placeholder 搜索框placeholder
  52. * @event {Function} search 搜索事件,返回keyword
  53. * @event {Function} lower 滑动到底部触发,用于下拉加载新数据
  54. * @event {Function} cancel 组件关闭事件
  55. * @event {Function} submit 提交按钮,返回选中的列表数据
  56. * @example <common-select :show="show" :popupTitle="popupTitle" @cancel="show=false" @search="selectSearch" name="cworkStationName" @submit="onsubmit"
  57. :dataLists="dataLists" placeholder="输入工站名称搜索"></common-select>
  58. */
  59. export default {
  60. name: "qianziyu-select",
  61. props: {
  62. dataLists: {
  63. default: {},
  64. type: Array
  65. },
  66. name: {
  67. default: 'name',
  68. },
  69. show: {
  70. default: false,
  71. type: Boolean
  72. },
  73. type: {
  74. default: 'radio',
  75. type: String
  76. },
  77. showSearch: {
  78. default: true,
  79. type: Boolean
  80. },
  81. popupTitle: {
  82. default: '列表选择',
  83. type: String
  84. },
  85. placeholder: {
  86. default: '请输入搜索内容'
  87. }
  88. },
  89. data() {
  90. return {
  91. keyword: '',
  92. scrollTop: 0,
  93. checkboxData: [],
  94. checkboxValue:[],
  95. radioData: {},
  96. radioValue: ''
  97. };
  98. },
  99. methods: {
  100. checkboxChange(n) {
  101. this.checkboxData=[]
  102. n.forEach(key=>{
  103. this.checkboxData.push(this.dataLists[key])
  104. })
  105. },
  106. //选择列表项触发
  107. groupChange(n) {
  108. this.radioData = this.dataLists[n]
  109. },
  110. //点击搜索触发
  111. search() {
  112. this.$emit('search', this.keyword)
  113. },
  114. //点击取消按钮触发
  115. cancel() {
  116. this.$emit('cancel')
  117. },
  118. //提交触发
  119. submit() {
  120. if (this.type == 'radio') {
  121. if (JSON.stringify(this.radioData) == '{}') {
  122. uni.$u.toast('请选择数据')
  123. return;
  124. }
  125. this.$emit('submit', this.radioData)
  126. } else if (this.type == 'checkbox') {
  127. if (this.checkboxData.length == 0) {
  128. uni.$u.toast('请选择数据')
  129. return;
  130. }
  131. this.$emit('submit', this.checkboxData)
  132. }
  133. }
  134. }
  135. }
  136. </script>
  137. <style lang="scss" scoped>
  138. .u-popup {
  139. .title {
  140. border-bottom: 1px solid #f7f7f7;
  141. padding: 20rpx;
  142. text-align: center;
  143. font-weight: bold;
  144. }
  145. }
  146. .scroll-Y {
  147. height: 650rpx;
  148. }
  149. .bottons {
  150. background-color: white;
  151. position: fixed;
  152. left: 0;
  153. bottom: 0;
  154. right: 0;
  155. bottom: constant(safe-area-inset-bottom);
  156. bottom: env(safe-area-inset-bottom);
  157. }
  158. </style>