cuihai-combox.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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-if="!isDisabled" />
  10. <image class="uni-combox__input-arrow" src="../../static/cuihai-combox/arrow_down.png" style="width: 30rpx;height: 30rpx;" @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. watch:{
  108. candidates(value){
  109. console.log(value,'111')
  110. }
  111. },
  112. data() {
  113. return {
  114. showSelector: false,
  115. inputVal: '',
  116. arrays: [],
  117. gid: `sm-org-dropDown-${(new Date()).getTime()}${Math.random()}`
  118. }
  119. },
  120. computed: {
  121. labelStyle() {
  122. if (this.labelWidth === 'auto') {
  123. return {}
  124. }
  125. return {
  126. width: this.labelWidth
  127. }
  128. },
  129. filterCandidates() {
  130. console.log('filterCandidates')
  131. if (!this.isDisabled) {
  132. if (this.isJSON) {
  133. let index = 0;
  134. return this.candidates.filter((item) => {
  135. item.index = index;
  136. index++;
  137. return item[this.keyName].indexOf(this.inputVal) > -1
  138. })
  139. } else {
  140. return this.candidates.filter((item) => {
  141. return item.indexOf(this.inputVal) > -1
  142. })
  143. }
  144. } else {
  145. if (this.isJSON) {
  146. let index = 0;
  147. return this.candidates.filter((item) => {
  148. item.index = index;
  149. index++;
  150. return item[this.keyName] != undefined;
  151. })
  152. } else {
  153. return this.candidates
  154. }
  155. }
  156. },
  157. filterCandidatesLength() {
  158. return this.filterCandidates.length
  159. }
  160. },
  161. created() {
  162. if (this.initValue != null) {
  163. this.arrays = this.initValue;
  164. this.inputVal = this.getInpuevals();
  165. }
  166. //在created的时候,给子组件放置一个监听,这个时候只是监听器被建立,此时这段代码不会生效
  167. //uni.$on接收一个sm-org-dropDown-show的广播
  168. uni.$on('sm-org-dropDown-show', (targetId) => {
  169. //接收广播,当该组件处于下拉框被打开的状态,并且跟下一个被点击的下拉框的gid不同时,将该组件的下拉框关闭,产生一个互斥效果。
  170. if (this.showSelector && this.gid != targetId) {
  171. this.showSelector = false
  172. }
  173. })
  174. },
  175. //当子组件销毁的时候,将建立的监听销毁,这里不会影响代码效果,但是在多次反复引用组件时,会在根节点定义越来越多的监听,长此以往影响性能,所以在不用的时候及时销毁,养成好习惯
  176. beforeDestroy() {
  177. uni.$off('sm-org-dropDown-show')
  178. },
  179. watch: {
  180. value: {
  181. handler(newVal) {
  182. this.inputVal = newVal
  183. },
  184. immediate: true
  185. }
  186. },
  187. methods: {
  188. toggleSelector() {
  189. this.showSelector = !this.showSelector
  190. if (this.showSelector) {
  191. uni.$emit('sm-org-dropDown-show', this.gid)
  192. }
  193. },
  194. onFocus() {
  195. this.showSelector = true;
  196. uni.$emit('sm-org-dropDown-show', this.gid)
  197. },
  198. onBlur() {
  199. /* setTimeout(() => {
  200. this.showSelector = false;
  201. }, 50) */
  202. },
  203. onSelectorClick(index) {
  204. if (this.isCheckBox) {
  205. let flag = this.arrays.indexOf(index);
  206. if (flag != -1) {
  207. let x = (this.arrays || []).findIndex((item) => item === index)
  208. this.arrays.splice(x, 1);
  209. } else {
  210. this.arrays.push(index);
  211. }
  212. this.inputVal = this.getInpuevals();
  213. if (this.isJSON) {
  214. this.$emit('getValue', this.arrays);
  215. } else {
  216. this.$emit('getValue', this.trimSpace(this.inputVal.split(" ")));
  217. }
  218. } else {
  219. this.showSelector = false
  220. if (this.isJSON) {
  221. this.$emit('getValue', this.filterCandidates[index].index);
  222. this.inputVal = this.filterCandidates[index][this.keyName];
  223. } else {
  224. this.$emit('getValue', this.filterCandidates[index]);
  225. this.inputVal = this.filterCandidates[index]
  226. }
  227. }
  228. },
  229. trimSpace(array) {
  230. for (var i = 0; i < array.length; i++) {
  231. if (array[i] == "") {
  232. array.splice(i, 1);
  233. i = i - 1;
  234. }
  235. }
  236. return array;
  237. },
  238. onInput() {
  239. setTimeout(() => {
  240. this.$emit('input', this.inputVal)
  241. })
  242. },
  243. clearInputValue() {
  244. this.inputVal = "";
  245. this.showSelector = false;
  246. },
  247. getInpuevals() {
  248. if (this.arrays.length == 0) {
  249. this.inputVal = "";
  250. } else {
  251. this.arrays.sort(function(a, b) {
  252. return a - b
  253. })
  254. this.inputVal = "";
  255. if (this.isJSON) {
  256. this.arrays.forEach(v => {
  257. this.inputVal += this.candidates[v][this.keyName] + " ";
  258. });
  259. } else {
  260. this.arrays.forEach(v => {
  261. this.inputVal += this.candidates[v] + " ";
  262. });
  263. }
  264. }
  265. return this.inputVal;
  266. },
  267. isCheck(index) {
  268. return this.arrays.indexOf(index) != -1;
  269. }
  270. }
  271. }
  272. </script>
  273. <style scoped>
  274. .uni-combox {
  275. /* #ifndef APP-NVUE */
  276. display: flex;
  277. /* #endif */
  278. height: 40px;
  279. flex-direction: row;
  280. align-items: center;
  281. /* border-bottom: solid 1px #DDDDDD; */
  282. }
  283. .uni-combox__label {
  284. font-size: 16px;
  285. line-height: 22px;
  286. padding-right: 10px;
  287. color: #999999;
  288. }
  289. .uni-combox__input-box {
  290. position: relative;
  291. /* #ifndef APP-NVUE */
  292. display: flex;
  293. /* #endif */
  294. flex: 1;
  295. flex-direction: row;
  296. align-items: center;
  297. }
  298. .uni-combox__input {
  299. flex: 1;
  300. font-size: 16px;
  301. height: 22px;
  302. line-height: 22px;
  303. }
  304. .uni-combox__input-arrow {
  305. padding: 10px;
  306. }
  307. .uni-combox__selector {
  308. box-sizing: border-box;
  309. position: absolute;
  310. top: 42px;
  311. left: 0;
  312. width: 100%;
  313. background-color: #FFFFFF;
  314. border-radius: 6px;
  315. box-shadow: #DDDDDD 4px 4px 8px, #DDDDDD -4px -4px 8px;
  316. z-index: 2;
  317. }
  318. .uni-combox__selector-scroll {
  319. max-height: 200px;
  320. box-sizing: border-box;
  321. }
  322. .uni-combox__selector::before {
  323. content: '';
  324. position: absolute;
  325. width: 0;
  326. height: 0;
  327. border-bottom: solid 6px #FFFFFF;
  328. border-right: solid 6px transparent;
  329. border-left: solid 6px transparent;
  330. left: 50%;
  331. top: -6px;
  332. margin-left: -6px;
  333. }
  334. .uni-combox__selector-empty,
  335. .uni-combox__selector-item {
  336. /* #ifdef APP-NVUE */
  337. display: flex;
  338. /* #endif */
  339. line-height: 36px;
  340. font-size: 14px;
  341. text-align: center;
  342. border-bottom: solid 1px #DDDDDD;
  343. /* margin: 0px 10px; */
  344. }
  345. .uni-combox__selector-empty:last-child,
  346. .uni-combox__selector-item:last-child {
  347. border-bottom: none;
  348. }
  349. </style>