美文网首页
Vue+Element-ui 输入框可模糊查询

Vue+Element-ui 输入框可模糊查询

作者: 残_忆 | 来源:发表于2019-10-21 17:43 被阅读0次
    <el-autocomplete
      popper-class="my-autocomplete"
      v-model="state"
      :fetch-suggestions="querySearch"
      placeholder="请输入内容"
      @select="handleSelect">
      <i
        class="el-icon-edit el-input__icon"
        slot="suffix"
        @click="handleIconClick">
      </i>
      <template slot-scope="{ item }">
        <div class="name">{{ item.value }}</div>
        <span class="addr">{{ item.address }}</span>
      </template>
    </el-autocomplete>
    
    <style>
    .my-autocomplete {
      li {
        line-height: normal;
        padding: 7px;
    
        .name {
          text-overflow: ellipsis;
          overflow: hidden;
        }
        .addr {
          font-size: 12px;
          color: #b4b4b4;
        }
    
        .highlighted .addr {
          color: #ddd;
        }
      }
    }
    </style>
    
    <script>
      export default {
        data() {
          return {
            restaurants: [],
            state: ''
          };
        },
        methods: {
          querySearch(queryString, cb) {
            var restaurants = this.restaurants;
            var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
            // 调用 callback 返回建议列表的数据
            cb(results);
          },
          createFilter(queryString) {
            return (restaurant) => {
              return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);//字符串头查询
              //return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) != -1);模糊查询
            };
          },
          loadAll() {
            return [
              { "value": "三全鲜食(北新泾店)", "address": "长宁区新渔路144号" },
              { "value": "Hot honey 首尔炸鸡(仙霞路)", "address": "上海市长宁区淞虹路661号" },
              { "value": "新旺角茶餐厅", "address": "上海市普陀区真北路988号创邑金沙谷6号楼113" },
              { "value": "泷千家(天山西路店)", "address": "天山西路438号" },
            ];
          },
          handleSelect(item) {
            console.log(item);
          },
          handleIconClick(ev) {
            console.log(ev);
          }
        },
        mounted() {
          this.restaurants = this.loadAll();
        }
      }
    </script>
    

    相关文章

      网友评论

          本文标题:Vue+Element-ui 输入框可模糊查询

          本文链接:https://www.haomeiwen.com/subject/mqqcvctx.html