index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <u-popup class="jnpf-select" :maskCloseAble="maskCloseAble" mode="bottom" v-model="showPopup"
  3. :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :height="height" :mask-close-able="false">
  4. <view class="u-select">
  5. <view class="u-select__header" @touchmove.stop.prevent="">
  6. <view class="u-select__header__cancel u-select__header__btn" :style="{ color: cancelColor }"
  7. hover-class="u-hover-class" :hover-stay-time="150" @tap="close()"
  8. style="width: 60rpx;text-align: center;">
  9. <text v-if="cancelBtn">{{cancelText}}</text>
  10. </view>
  11. <view class="u-select__header__title" style="flex: 1;text-align: center;">
  12. {{title}}
  13. </view>
  14. <view class="u-select__header__confirm u-select__header__btn" :style="{ color: confirmColor }"
  15. style="width: 60rpx;text-align: center;" hover-class="u-hover-class" :hover-stay-time="150"
  16. @touchmove.stop="" @tap.stop="handleConfirm()">
  17. <text v-if="confirmBtn">{{confirmText}}</text>
  18. </view>
  19. </view>
  20. <view class="search-box_sticky" v-if=" isFlow || filterable">
  21. <view class="search-box">
  22. <u-search :placeholder="$t('app.apply.pleaseKeyword')" height="72" :show-action="false"
  23. bg-color="#f0f2f6" shape="square" v-model="searchValue">
  24. </u-search>
  25. </view>
  26. </view>
  27. <view class="u-select__body u-select__body__multiple">
  28. <scroll-view :scroll-y="true" style="height: 100%">
  29. <u-checkbox-group v-model="innerValue" v-if="multiple">
  30. <u-checkbox v-model="item.checked" v-for="(item, index) in columnList" :key="index"
  31. :name="item[valueName]">
  32. {{item[labelName]}}
  33. </u-checkbox>
  34. </u-checkbox-group>
  35. <u-radio-group wrap v-model="checkedValue" @change="radioGroupChange" v-else>
  36. <u-radio :name="item[valueName]" v-for="(item,i) in columnList" :key="i">
  37. {{item[labelName]}}
  38. </u-radio>
  39. </u-radio-group>
  40. <JnpfEmpty v-if="!columnList.length"></JnpfEmpty>
  41. </scroll-view>
  42. </view>
  43. </view>
  44. </u-popup>
  45. </template>
  46. <script>
  47. export default {
  48. name: 'JnpfMultSelect',
  49. props: {
  50. list: {
  51. type: Array,
  52. default: () => []
  53. },
  54. height: {
  55. type: [Number, String],
  56. default: ''
  57. },
  58. multiple: {
  59. type: Boolean,
  60. default: false
  61. },
  62. filterable: {
  63. type: Boolean,
  64. default: false
  65. },
  66. cancelBtn: {
  67. type: Boolean,
  68. default: true
  69. },
  70. confirmBtn: {
  71. type: Boolean,
  72. default: true
  73. },
  74. show: {
  75. type: Boolean,
  76. default: false
  77. },
  78. cancelColor: {
  79. type: String,
  80. default: '#606266'
  81. },
  82. confirmColor: {
  83. type: String,
  84. default: '#2979ff'
  85. },
  86. safeAreaInsetBottom: {
  87. type: Boolean,
  88. default: false
  89. },
  90. maskCloseAble: {
  91. type: Boolean,
  92. default: true
  93. },
  94. defaultValue: {
  95. type: Array,
  96. default: () => []
  97. },
  98. labelName: {
  99. type: String,
  100. default: 'fullName'
  101. },
  102. valueName: {
  103. type: String,
  104. default: 'id'
  105. },
  106. title: {
  107. type: String,
  108. default: ''
  109. },
  110. cancelText: {
  111. type: String,
  112. default: '取消'
  113. },
  114. confirmText: {
  115. type: String,
  116. default: '确认'
  117. },
  118. isFlow: {
  119. type: Boolean,
  120. default: false
  121. }
  122. },
  123. data() {
  124. return {
  125. columnData: [],
  126. innerValue: [],
  127. lastSelectIndex: [],
  128. showPopup: false,
  129. checkedValue: '',
  130. searchValue: ''
  131. }
  132. },
  133. watch: {
  134. show: {
  135. handler(val) {
  136. this.showPopup = val
  137. if (val) setTimeout(() => this.init(), 10);
  138. },
  139. immediate: true,
  140. },
  141. },
  142. computed: {
  143. columnList() {
  144. return this.columnData.filter((o) => (o[this.labelName] && o[this.labelName].match(this.searchValue)))
  145. }
  146. },
  147. methods: {
  148. init() {
  149. this.setColumnData();
  150. this.setDefault();
  151. },
  152. setColumnData() {
  153. this.columnData = this.list.map((o, i) => ({
  154. ...o,
  155. checked: false
  156. }))
  157. },
  158. // 获取默认选中的值
  159. setDefault() {
  160. this.searchValue = ''
  161. this.checkedValue = ''
  162. if (this.multiple) {
  163. this.innerValue = this.defaultValue
  164. outer: for (let i = 0; i < this.innerValue.length; i++) {
  165. inner: for (let j = 0; j < this.columnData.length; j++) {
  166. if (this.innerValue[i] === this.columnData[j][this.valueName]) {
  167. this.columnData[j].checked = true
  168. break inner
  169. }
  170. }
  171. }
  172. } else {
  173. for (let j = 0; j < this.columnData.length; j++) {
  174. if (this.defaultValue[0] === this.columnData[j][this.valueName]) {
  175. this.checkedValue = this.columnData[j][this.valueName]
  176. this.innerValue = this.columnData[j]
  177. }
  178. }
  179. }
  180. },
  181. radioGroupChange(e) {
  182. const foundItem = this.columnData.find(item => item[this.valueName] === e);
  183. this.innerValue = foundItem ? [{ ...foundItem, checked: true }] : [];
  184. },
  185. handleConfirm() {
  186. if (this.multiple) {
  187. let data = {
  188. indexs: [],
  189. list: [],
  190. label: '',
  191. value: uni.$u.deepClone(this.innerValue)
  192. }
  193. if (!this.isFlow) {
  194. for (let i = 0; i < this.columnData.length; i++) {
  195. const item = this.columnData[i]
  196. if (this.columnData[i].checked) {
  197. data.list.push(uni.$u.deepClone(item))
  198. data.indexs.push(i)
  199. if (!data.label) {
  200. data.label += item[this.labelName]
  201. } else {
  202. data.label += ',' + item[this.labelName]
  203. }
  204. }
  205. }
  206. }
  207. this.$emit('confirm', data);
  208. } else {
  209. if (this.isFlow && !this.innerValue.length) return this.$u.toast("请选择流程");
  210. this.$emit('confirm', this.innerValue);
  211. }
  212. this.close()
  213. },
  214. close() {
  215. this.$emit('close');
  216. },
  217. }
  218. }
  219. </script>
  220. <style scoped lang="scss">
  221. .notData-box {
  222. width: 100%;
  223. height: 100%;
  224. justify-content: center;
  225. align-items: center;
  226. margin-top: -50px;
  227. .notData-inner {
  228. width: 286rpx;
  229. height: 222rpx;
  230. align-items: center;
  231. .iconImg {
  232. width: 100% !important;
  233. height: 100% !important;
  234. }
  235. }
  236. .notData-inner-text {
  237. color: #909399;
  238. }
  239. }
  240. .u-select {
  241. &__header {
  242. display: flex;
  243. flex-direction: row;
  244. align-items: center;
  245. justify-content: space-between;
  246. height: 40px;
  247. padding: 0 20px;
  248. position: relative;
  249. ::after {
  250. content: "";
  251. position: absolute;
  252. border-bottom: 0.5px solid #eaeef1;
  253. transform: scaleY(0.5);
  254. bottom: 0;
  255. right: 0;
  256. left: 0;
  257. }
  258. }
  259. &__body {
  260. width: 100%;
  261. height: 500rpx;
  262. overflow: hidden;
  263. background-color: #fff;
  264. &__picker-view {
  265. height: 100%;
  266. box-sizing: border-box;
  267. &__item {
  268. display: flex;
  269. align-items: center;
  270. justify-content: center;
  271. font-size: 32rpx;
  272. padding: 0 8rpx;
  273. }
  274. }
  275. .u-checkbox-group {
  276. padding: 0 30rpx;
  277. .u-checkbox {
  278. height: 25px;
  279. line-height: 25px;
  280. width: 100% !important;
  281. display: flex;
  282. margin: 6rpx 0;
  283. :deep(uni-text) {
  284. flex: 1;
  285. overflow: hidden;
  286. white-space: nowrap;
  287. text-overflow: ellipsis;
  288. }
  289. }
  290. }
  291. }
  292. }
  293. </style>