【笔记~】使用js实现搜索排序(el-table)

直接看代码 ↓

 <div id="messages">
        <div class="serchBox">
          <label id="lbltxtSearchService">搜索
            <input type="text" class="el-input__inner" id="txtSearchService" placeholder="支持编码、名称" v-model="serchContent" @keyup.enter="serchEvent()" />
          </label>
        </div>

        <div class="selContentBox">
          <template>
            <el-table :header-cell-style="{background:'#f5f7fa'}" ref="multipleTable" @fit="true" border
              highlight-current-row :data="model" @selection-change="handleSelectionChange">
              <el-table-column prop="name" label="名称" sortable width="200px"
                :show-overflow-tooltip="true">
              </el-table-column>
              <el-table-column prop="code" label="编码" sortable width="265px"
                :show-overflow-tooltip="true">
              </el-table-column>
              <el-table-column prop="type" label="类型" sortable width="150px"
                :show-overflow-tooltip="true">
              </el-table-column>
            </el-table>
          </template>
        </div>
      </div>
 var vue = new Vue({
			el: "#messages",
			data: {
				model,
				serchContent: ''
			},
			methods: {
				// 搜索
				serchEvent() {
					if (event.keyCode == 13) {
						var input = this.serchContent.trim().toLowerCase();
						if (model) {
							var ServiceList = model;
							// console.log(model);
							// 给ServiceList添加_index_
							ServiceList.forEach((service, index) => {
								service._index_ = index;
							});
                            // 判断搜索框中有无内容
							if (input) {
								var matches = [];
								var unmatches = [];
                                // 取出要要根据搜索的内容
								ServiceList.forEach(service => {
									var search = (service.interface_service_code + "#" + service.interface_service_func_name + "#" + service.interface_service_name).toLowerCase();
									// console.log(search);
                                    // 判断搜索框中的内容与支持搜索的内容
									if (search.indexOf(input) > -1)
										matches.push(service);
									else
										unmatches.push(service);
								});
                                // 合并数组,符合的内容就在新数组的前面啦
								var datas = matches.concat(unmatches);
								// console.log(datas);
								this.model = datas
							}
							else {
								ServiceList = ServiceList.sort((l, r) => {
									if (r._index_ < l._index_)
										return 1
									else
										return -1;
								});
								this.model = ServiceList
							}
						}
					}
				},
			}
		});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值