inputSearch.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <!--
  3. <inputSearch :dataSource="dataSource" @select="handleChange" placeholder="请输入商品名称" />
  4. //数据源
  5. dataSource: [{
  6. id: 1,
  7. name: '耐克1'
  8. },
  9. {
  10. id: 2,
  11. name: '耐克2'
  12. }
  13. ],
  14. //用户点击获取的数据
  15. handleChange(data) {
  16. console.log(data)
  17. }
  18. -->
  19. <view class="input-group">
  20. <input :placeholder="placeholder" @input="search" @blur="hideList" v-model="name" />
  21. <view class="ul">
  22. <view class="li" v-for="(item,index) in list" :key="index" @tap="select(item)">{{item.name}}</view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. props: {
  29. placeholder: String, //默认提示
  30. searchKey: String, //模糊搜索的key值
  31. dataSource: {
  32. type: Array,
  33. default: function() { //数据源
  34. return [];
  35. }
  36. }
  37. },
  38. data() {
  39. return {
  40. list: [],
  41. name: '',
  42. backName: ''
  43. };
  44. },
  45. destroyed() {
  46. clearTimeout(this.t);
  47. },
  48. methods: {
  49. search(e) {
  50. let val = e.detail.value;
  51. let {
  52. dataSource
  53. } = this;
  54. let arr = [];
  55. for (let i = 0; i < dataSource.length; i++) {
  56. if (dataSource[i].name.indexOf(val) !== -1) {
  57. arr.push(dataSource[i]);
  58. }
  59. }
  60. // console.log(arr)
  61. if (!val) {
  62. this.list = [];
  63. } else {
  64. this.list = arr;
  65. }
  66. },
  67. select(item) {
  68. this.backName = item.name;
  69. this.list = [];
  70. this.$emit('select', item);
  71. },
  72. hideList() {
  73. this.list = [];
  74. this.t = setTimeout(() => {
  75. this.name = this.backName;
  76. }, 0);
  77. }
  78. }
  79. }
  80. </script>
  81. <style lang="scss">
  82. .input-group {
  83. position: relative;
  84. input {
  85. border: 1upx solid gray;
  86. height: 90upx;
  87. padding-left: 10upx;
  88. font-size: 30upx;
  89. box-sizing: border-box;
  90. }
  91. .ul {
  92. position: absolute;
  93. left: 0;
  94. top: 100%;
  95. width: 100%;
  96. background: #eaeaea;
  97. .li {
  98. border-bottom: 1upx solid gray;
  99. line-height: 60upx;
  100. }
  101. }
  102. }
  103. </style>