SearchInput.vue 8.14 KB
Newer Older
潘际乾 committed
1 2
<template>
  <div class="search-wrapper" :class="{ 'customer-bor': resultDisplay }">
3
    <el-input
潘际乾 committed
4 5 6
      prefix-icon="el-icon-search"
      placeholder="全局搜索"
      v-model="searchContent"
7 8
      :clearable="true"

潘际乾 committed
9 10 11 12 13 14 15
      @keyup.enter.native="searchEvent"
      @keydown.up.native="preDownEvent"
      @keyup.up.native="preUpEvent"
      @keydown.down.native="nextDownEvent"
      @keyup.down.native="nextUpEvent"
      @focus="focusInput"
      @blur="blurInput"
16
    ></el-input>
潘际乾 committed
17 18 19 20 21 22 23 24 25
    <div
      class="search-sug"
      v-show="resultDisplay"
      @mouseenter="isChoosing = true"
      @mouseleave="isChoosing = false"
    >
      <ul>
        <li
          :class="{ 'sug-selected': item.selected }"
26
          v-for="(item, idx) in searchResult"
潘际乾 committed
27 28
          :key="idx"
          @mouseover="liOverEvent(idx)"
29 30 31 32 33 34 35
          @mouseup.left="searchEvent(item)"
        >
        
        <span style="width:8rem;display:inline-block">{{item.OWNREF}}</span>
        <el-divider direction="vertical"></el-divider>
        <span>{{item.OBJNAM}}</span>
        </li>
潘际乾 committed
36 37 38 39 40 41 42
      </ul>
    </div>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
43
import commonReport from "~/mixin/commonReport"
潘际乾 committed
44 45
export default {
  name: "SearchInput",
46
  mixins:[commonReport],
潘际乾 committed
47 48 49 50
  data() {
    return {
      searchContent: "",
      searchResult: [],
潘际乾 committed
51
      searchResultForLis: [],
潘际乾 committed
52 53 54 55 56 57 58 59 60 61 62
      selectedIndex: 0,
      resultDisplay: false,
      // 鼠标选择结果集的状态
      isChoosing: false,
      debounceWrapper: debounce(this.queryData, 500),
    };
  },
  watch: {
    searchContent: function(val, oldVal) {
      // console.log(val, oldVal);
      if (val.trim() === "") {
63
        this.searchResult = [];
潘际乾 committed
64
        this.searchResultForLis = [];
65
        this.resultDisplay = false;
潘际乾 committed
66 67
        return;
      }
68
      if (this.searchResult.map((r) => r.OWNREF).includes(val)) {
潘际乾 committed
69 70 71 72 73 74
        return;
      }
      this.debounceWrapper();
    },
  },
  methods: {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    async queryData() {
      // const testData = [
      //   { text: "DD3500210260AA01", url: "" },
      //   { text: "OC3500190001AA", url: "" },
      //   { text: "KZ3500210313AA", url: "" },
      //   { text: "KZ3500210312AA", url: "" },
      //   { text: "DD3500210311AA01", url: "" },
      //   { text: "DD3500210311AA01", url: "" },
      //   { text: "KZ3500210311AA", url: "" },
      //   { text: "CH3500200001AA", url: "" },
      //   { text: "CH3323200001AB", url: "" },
      //   { text: "DV453500210260AA01", url: "" },
      //   { text: "KZ3500210313AA", url: "" },
      //   { text: "DD45350029760AA01", url: "" },
      //   { text: "CH3500343657001AA", url: "" },
      //   { text: "CH35987567001AA", url: "" },
      //   { text: "CH243534200001AA", url: "" },
      //   { text: "CH350086786001AA", url: "" },
      //   { text: "CH53454001AA", url: "" },
      //   { text: "CH65645FG4545AA", url: "" },
      // ];

潘际乾 committed
97
      console.log("query data ..." + new Date().toLocaleString());
98 99 100 101 102 103 104
      let rtnmsg = await this.globalSearch(this.searchContent);
      if(rtnmsg.respCode == SUCCESS && rtnmsg.data && rtnmsg.data.length >0){
        let srcData = rtnmsg.data;
        let searchResult = srcData.map(item=>{
          return item;
        })
        this.searchResult = searchResult;
潘际乾 committed
105
        this.resultDisplay = true;
106 107
      }
      else{
潘际乾 committed
108 109 110
        this.searchResult = [];
        this.resultDisplay = false;
      }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
                
      // this.shuffle(testData);
      // const res = testData;
      // const res = testData.filter(
      //   (d) => d.text.indexOf(this.searchContent) > -1
      // );
      // if (res && res.length > 0) {
      //   this.searchResultForLis = res.map((d) => {
      //     const o = Object.assign({}, d);
      //     o.text = d.text.replace(
      //       this.searchContent,
      //       "<em>" + this.searchContent + "</em>"
      //     );
      //     return o;
      //   });
      //   this.searchResult = res;
      //   this.resultDisplay = true;
      // } else {
      //   this.searchResult = [];
      //   this.searchResultForLis = [];
      //   this.resultDisplay = false;
      // }
潘际乾 committed
133 134 135 136 137 138 139 140 141
    },
    shuffle(arr) {
      let i = arr.length - 1;
      while (i > 0) {
        const n = (Math.random() * i) >>> 0;
        [arr[n], arr[i]] = [arr[i], arr[n]];
        i--;
      }
    },
142 143 144
    searchEvent(row) {
      if (typeof row.OWNREF === "string") {
        this.searchContent = row.OWNREF;
潘际乾 committed
145 146
      }
      this.resultDisplay = false;
147
      // TODO
148
      
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
      
      let trns = {
        did:"ditsel",
        lid:"litsel",
        cpd:"cptsel",
        ded:"detsel",
        led:"letsel",
        bpd:"bptsel",
        ccd:"cctsel",
        ged:"getsel",
        gid:"gitsel",
        mcd:"mctsel",
        trd:"trtsel"
        }
      let objtyp = row.OBJTYP.trim().toLowerCase()
      let link = trns[objtyp];
165 166 167 168 169
      console.log("go to ..."+link);
      if(!link){
        this.$notify.error({title: '错误',message: '未知的objtyp:'+row.OBJTYP+"!"})
        return
      }
170
      this.$router.push({name:link.toUpperCase().substring(0,1)+link.substring(1),query:{ownref:row.OWNREF.trim(),objtyp}})
潘际乾 committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    },
    focusInput() {
      if (this.searchResult.length > 0) {
        this.resultDisplay = true;
      }
    },
    blurInput() {
      if (!this.isChoosing) {
        this.resultDisplay = false;
      }
      this.selectedIndex = -1;
      this.clearHighlightItem();
    },
    goNext() {
      if (this.searchResult.length === 0) {
        return;
      }
      if (this.selectedIndex >= this.searchResult.length - 1) {
        this.selectedIndex = 0;
      } else {
        this.selectedIndex++;
      }
      this.highlightItem(this.selectedIndex, true);
    },
    goPre() {
      if (this.searchResult.length === 0) {
        return;
      }
      if (this.selectedIndex <= 0) {
        this.selectedIndex = this.searchResult.length - 1;
      } else {
        this.selectedIndex--;
      }
      this.highlightItem(this.selectedIndex, true);
    },
    highlightItem(idx, changeVal) {
      // console.log(idx);
      const lis = this.$el.querySelectorAll(".search-sug ul li");
      lis.forEach((li) => (li.className = ""));
      if (lis[idx] && this.searchResult[idx]) {
        lis[idx].className = "sug-selected";
        if (changeVal) {
          this.searchContent = this.searchResult[idx].text;
        }
      }
    },
    liOverEvent(idx) {
      this.selectedIndex = idx;
      this.highlightItem(idx, false);
    },
    clearHighlightItem() {
      this.$el
        .querySelectorAll(".search-sug ul li")
        .forEach((li) => (li.className = ""));
    },
    /**
     * up按下
     */
    preDownEvent() {
      // console.log("up key: down");
      this.goPre();
    },
    /**
     * up弹起
     */
    preUpEvent() {
      // console.log("up key: up");
    },
    /**
     * down按下
     */
    nextDownEvent() {
      // console.log("down key: down");
      this.goNext();
    },
    /**
     * down弹起
     */
    nextUpEvent() {
      // console.log("down key: up");
    },
  },
};
</script>

<style scoped>
.search-wrapper {
  position: relative;
}
.search-wrapper >>> .el-input {
261
  z-index: 101;
潘际乾 committed
262 263 264 265 266 267 268 269
}
.search-wrapper >>> .el-input .el-input__inner {
  height: 30px;
}
.search-sug {
  position: absolute;
  width: 590px;
  top: 28px;
270 271
  height: 40rem;
  overflow: auto;
潘际乾 committed
272 273 274 275
  border-radius: 0 0 10px 10px;
  border: 2px solid #4e71f2;
  box-shadow: none;
  font-family: "Microsoft YaHei", Arial, sans-serif;
276
  z-index: 100;
潘际乾 committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  background: #fff;
}
.search-sug ul {
  margin: 7px 14px 0;
  padding: 8px 0 7px;
  background: 0 0;
  border-top: 2px solid #f5f5f6;
}
.search-sug ul li {
  list-style: none;
  width: auto;
  padding: 0 8px;
  padding-left: 14px;
  margin-left: -14px;
  margin-right: -14px;
  color: #626675;
  font: 14px arial;
  line-height: 28px;
  background: 0 0;
  font-family: "Microsoft YaHei", Arial, sans-serif;
  position: relative;
  cursor: pointer;
}
.search-sug ul li.sug-selected {
  background-color: #f5f5f6;
  color: #315efb;
}
.search-wrapper >>> .el-input__inner:focus {
  border: 2px solid #4e71f2;
  outline: 0;
}
.customer-bor >>> .el-input__inner {
  border-bottom: 0 !important;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}
313 314 315 316 317

.search-sug ul li >>> em {
  color: #f73131;
  font-style: normal;
}
潘际乾 committed
318
</style>