cuihai-combox.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <template>
  2. <view class="uni-combox">
  3. <view v-if="label" class="uni-combox__label" :style="labelStyle">
  4. <text>{{label}}</text>
  5. </view>
  6. <view class="uni-combox__input-box">
  7. <input class="uni-combox__input" type="text" :placeholder="placeholder" :disabled="isDisabled" v-model="inputVal"
  8. @input="onInput" @focus="onFocus" @blur="onBlur" />
  9. <icon type="clear" size="16" @tap="clearInputValue" v-show="!!inputVal" v-if="!isDisabled" />
  10. <image class="uni-combox__input-arrow" src="../../static/icon-arr.png" @click="toggleSelector"></image>
  11. <!-- <uni-icons class="uni-combox__input-arrow" type="arrowdown" size="14" @click="toggleSelector"></uni-icons> -->
  12. <view class="uni-combox__selector" v-if="showSelector">
  13. <scroll-view scroll-y="true" class="uni-combox__selector-scroll">
  14. <view class="uni-combox__selector-empty" v-if="filterCandidatesLength === 0">
  15. <text>{{emptyTips}}</text>
  16. </view>
  17. <view class="uni-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index" @click="onSelectorClick(index)"
  18. :style="isCheck(index)?'color:'+selectColor+';background-color:'+selectBgColor+';':'color:'+color+';'">
  19. <text>{{isJSON?item[keyName]:item}}</text>
  20. </view>
  21. </scroll-view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. /**
  28. * Combox 组合输入框
  29. * @description 组合输入框一般用于既可以输入也可以选择的场景
  30. * @property {String} label 左侧文字
  31. * @property {String} labelWidth 左侧内容宽度
  32. * @property {String} placeholder 输入框占位符
  33. * @property {Array} candidates 候选项列表
  34. * @property {String} emptyTips 筛选结果为空时显示的文字
  35. * @property {String} value 单选时组合框的初始值
  36. * @property {Array} initValue 多选时组合框的初始值(下标集合)
  37. * @property {String} keyName json数组显示的字段值
  38. * @property {Boolean} isJSON 是否是json数组,默认不是
  39. * @property {Boolean} isDisabled 是否是禁用输入,默认不禁用
  40. * @property {Boolean} isCheckBox 是否是多选,默认不是,多选时不能输入查询
  41. * @property {String} color 默认字体颜色,默认#000000
  42. * @property {String} selectColor 选中字体颜色,默认#0081ff
  43. * @property {String} selectBgColor 选中背景颜色,默认#e8e8e8
  44. */
  45. export default {
  46. name: 'comboxSearch',
  47. props: {
  48. label: {
  49. type: String,
  50. default: ''
  51. },
  52. labelWidth: {
  53. type: String,
  54. default: 'auto'
  55. },
  56. placeholder: {
  57. type: String,
  58. default: ''
  59. },
  60. candidates: {
  61. type: Array,
  62. default () {
  63. return []
  64. }
  65. },
  66. emptyTips: {
  67. type: String,
  68. default: '无匹配项'
  69. },
  70. value: {
  71. type: String,
  72. default: ''
  73. },
  74. initValue: {
  75. type: Array,
  76. default: null
  77. },
  78. keyName: {
  79. type: String,
  80. default: ''
  81. },
  82. isJSON: {
  83. type: Boolean,
  84. default: false
  85. },
  86. isDisabled: {
  87. type: Boolean,
  88. default: false
  89. },
  90. isCheckBox: {
  91. type: Boolean,
  92. default: false
  93. },
  94. color: {
  95. default: "#000000",
  96. type: String
  97. },
  98. selectColor: {
  99. default: "#0081ff",
  100. type: String
  101. },
  102. selectBgColor: {
  103. default: "#e8e8e8",
  104. type: String
  105. }
  106. },
  107. data() {
  108. return {
  109. showSelector: false,
  110. inputVal: '',
  111. arrays: [],
  112. gid: `sm-org-dropDown-${(new Date()).getTime()}${Math.random()}`
  113. }
  114. },
  115. computed: {
  116. labelStyle() {
  117. if (this.labelWidth === 'auto') {
  118. return {}
  119. }
  120. return {
  121. width: this.labelWidth
  122. }
  123. },
  124. filterCandidates() {
  125. if (!this.isDisabled) {
  126. if (this.isJSON) {
  127. let index = 0;
  128. return this.candidates.filter((item) => {
  129. item.index = index;
  130. index++;
  131. return item[this.keyName].indexOf(this.inputVal) > -1
  132. })
  133. } else {
  134. return this.candidates.filter((item) => {
  135. return item.indexOf(this.inputVal) > -1
  136. })
  137. }
  138. } else {
  139. if (this.isJSON) {
  140. let index = 0;
  141. return this.candidates.filter((item) => {
  142. item.index = index;
  143. index++;
  144. return item[this.keyName] != undefined;
  145. })
  146. } else {
  147. return this.candidates
  148. }
  149. }
  150. },
  151. filterCandidatesLength() {
  152. return this.filterCandidates.length
  153. }
  154. },
  155. created() {
  156. if (this.initValue != null) {
  157. this.arrays = this.initValue;
  158. this.inputVal = this.getInpuevals();
  159. }
  160. //在created的时候,给子组件放置一个监听,这个时候只是监听器被建立,此时这段代码不会生效
  161. //uni.$on接收一个sm-org-dropDown-show的广播
  162. uni.$on('sm-org-dropDown-show', (targetId) => {
  163. //接收广播,当该组件处于下拉框被打开的状态,并且跟下一个被点击的下拉框的gid不同时,将该组件的下拉框关闭,产生一个互斥效果。
  164. if (this.showSelector && this.gid != targetId) {
  165. this.showSelector = false
  166. }
  167. })
  168. },
  169. //当子组件销毁的时候,将建立的监听销毁,这里不会影响代码效果,但是在多次反复引用组件时,会在根节点定义越来越多的监听,长此以往影响性能,所以在不用的时候及时销毁,养成好习惯
  170. beforeDestroy() {
  171. uni.$off('sm-org-dropDown-show')
  172. },
  173. watch: {
  174. value: {
  175. handler(newVal) {
  176. this.inputVal = newVal
  177. },
  178. immediate: true
  179. }
  180. },
  181. methods: {
  182. toggleSelector() {
  183. this.showSelector = !this.showSelector
  184. if (this.showSelector) {
  185. uni.$emit('sm-org-dropDown-show', this.gid)
  186. }
  187. },
  188. onFocus() {
  189. this.showSelector = true;
  190. uni.$emit('sm-org-dropDown-show', this.gid)
  191. },
  192. onBlur() {
  193. /* setTimeout(() => {
  194. this.showSelector = false;
  195. }, 50) */
  196. },
  197. onSelectorClick(index) {
  198. if (this.isCheckBox) {
  199. let flag = this.arrays.indexOf(index);
  200. if (flag != -1) {
  201. let x = (this.arrays || []).findIndex((item) => item === index)
  202. this.arrays.splice(x, 1);
  203. } else {
  204. this.arrays.push(index);
  205. }
  206. this.inputVal = this.getInpuevals();
  207. if (this.isJSON) {
  208. this.$emit('getValue', this.arrays);
  209. } else {
  210. this.$emit('getValue', this.trimSpace(this.inputVal.split(" ")));
  211. }
  212. } else {
  213. this.showSelector = false
  214. if (this.isJSON) {
  215. this.$emit('getValue', this.filterCandidates[index].index);
  216. this.inputVal = this.filterCandidates[index][this.keyName];
  217. } else {
  218. this.$emit('getValue', this.filterCandidates[index]);
  219. this.inputVal = this.filterCandidates[index]
  220. }
  221. }
  222. },
  223. trimSpace(array) {
  224. for (var i = 0; i < array.length; i++) {
  225. if (array[i] == "") {
  226. array.splice(i, 1);
  227. i = i - 1;
  228. }
  229. }
  230. return array;
  231. },
  232. onInput() {
  233. setTimeout(() => {
  234. this.$emit('input', this.inputVal)
  235. })
  236. },
  237. clearInputValue() {
  238. this.inputVal = "";
  239. this.showSelector = false;
  240. this.$emit('getValue', -1)
  241. },
  242. getInpuevals() {
  243. if (this.arrays.length == 0) {
  244. this.inputVal = "";
  245. } else {
  246. this.arrays.sort(function(a, b) {
  247. return a - b
  248. })
  249. this.inputVal = "";
  250. if (this.isJSON) {
  251. this.arrays.forEach(v => {
  252. this.inputVal += this.candidates[v][this.keyName] + " ";
  253. });
  254. } else {
  255. this.arrays.forEach(v => {
  256. this.inputVal += this.candidates[v] + " ";
  257. });
  258. }
  259. }
  260. return this.inputVal;
  261. },
  262. isCheck(index) {
  263. return this.arrays.indexOf(index) != -1;
  264. }
  265. }
  266. }
  267. </script>
  268. <style scoped>
  269. .uni-combox {
  270. /* #ifndef APP-NVUE */
  271. display: flex;
  272. /* #endif */
  273. height: 40px;
  274. flex-direction: row;
  275. align-items: center;
  276. border-bottom: 1px solid #333;
  277. /* border-bottom: solid 1px #DDDDDD; */
  278. }
  279. .uni-combox__label {
  280. font-size: 14px;
  281. line-height: 22px;
  282. padding-right: 10px;
  283. color: #999999;
  284. }
  285. .uni-combox__input-box {
  286. position: relative;
  287. /* #ifndef APP-NVUE */
  288. display: flex;
  289. /* #endif */
  290. flex: 1;
  291. flex-direction: row;
  292. align-items: center;
  293. padding-left: 40rpx;
  294. padding-right: 20rpx;
  295. }
  296. .uni-combox__input {
  297. flex: 1;
  298. font-size: 14px;
  299. height: 22px;
  300. line-height: 22px;
  301. padding-right: 20rpx;
  302. }
  303. .uni-combox__input-arrow {
  304. width: 24rpx;
  305. height: 24rpx;
  306. opacity: 1;
  307. margin-left: 6rpx;
  308. }
  309. .uni-combox__selector {
  310. box-sizing: border-box;
  311. position: absolute;
  312. top: 42px;
  313. left: 0;
  314. width: 100%;
  315. background-color: #FFFFFF;
  316. border-radius: 6px;
  317. box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
  318. z-index: 2;
  319. }
  320. .uni-combox__selector-scroll {
  321. max-height: 200px;
  322. box-sizing: border-box;
  323. }
  324. .uni-combox__selector::before {
  325. content: '';
  326. position: absolute;
  327. width: 0;
  328. height: 0;
  329. border-bottom: solid 6px #FFFFFF;
  330. border-right: solid 6px transparent;
  331. border-left: solid 6px transparent;
  332. left: 50%;
  333. top: -6px;
  334. margin-left: -6px;
  335. }
  336. .uni-combox__selector-empty,
  337. .uni-combox__selector-item {
  338. /* #ifdef APP-NVUE */
  339. display: flex;
  340. /* #endif */
  341. line-height: 36px;
  342. font-size: 14px;
  343. text-align: center;
  344. border-bottom: solid 1px #DDDDDD;
  345. /* margin: 0px 10px; */
  346. }
  347. .uni-combox__selector-empty:last-child,
  348. .uni-combox__selector-item:last-child {
  349. border-bottom: none;
  350. }
  351. </style>