优化用户、动物搜索功能,实现模糊搜索
This commit is contained in:
parent
efbfd6848e
commit
4d493f1f2e
@ -84,7 +84,7 @@ export default {
|
|||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="饲养员ID">
|
<el-form-item label="饲养员ID">
|
||||||
<el-input v-model="form.roleId" placeholder="饲养员ID"/>
|
<el-input v-model="form.roleId" type="number" placeholder="饲养员ID"/>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="动物颜色">
|
<el-form-item label="动物颜色">
|
||||||
<el-color-picker v-model="form.color" />
|
<el-color-picker v-model="form.color" />
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import {copy} from "@/utils/common.js";
|
import {copy, fuzzyMatching, splitKeyWords} from "@/utils/common.js";
|
||||||
import {Delete, DocumentAdd, Edit} from "@element-plus/icons-vue";
|
import {Delete, DocumentAdd, Edit, Search} from "@element-plus/icons-vue";
|
||||||
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
||||||
import ZooAnimalFormDialog from "@/components/ZooAnimalFormDialog.vue";
|
import ZooAnimalFormDialog from "@/components/ZooAnimalFormDialog.vue";
|
||||||
|
|
||||||
@ -9,11 +9,11 @@ export default {
|
|||||||
components: {ZooAnimalFormDialog, ZooUserFormDialog},
|
components: {ZooAnimalFormDialog, ZooUserFormDialog},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
animals:[],
|
animals: [],
|
||||||
tableData:[],
|
tableData: [],
|
||||||
searchInput: {
|
searchInput: {
|
||||||
// auth: '',
|
state: '',//状态(0正常 1异常)
|
||||||
// keyWord: ''
|
keyword: ''
|
||||||
},
|
},
|
||||||
dialog: {
|
dialog: {
|
||||||
addDialogVisible: false,//添加动物对话框
|
addDialogVisible: false,//添加动物对话框
|
||||||
@ -23,6 +23,11 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
//图标
|
||||||
|
//region
|
||||||
|
Search() {
|
||||||
|
return Search
|
||||||
|
},
|
||||||
DocumentAdd() {
|
DocumentAdd() {
|
||||||
return DocumentAdd
|
return DocumentAdd
|
||||||
},
|
},
|
||||||
@ -32,6 +37,7 @@ export default {
|
|||||||
Delete() {
|
Delete() {
|
||||||
return Delete
|
return Delete
|
||||||
},
|
},
|
||||||
|
//endregion
|
||||||
// dataFilters() {
|
// dataFilters() {
|
||||||
// // 计算格式化后的数组
|
// // 计算格式化后的数组
|
||||||
// let data = copy(this.tableData)
|
// let data = copy(this.tableData)
|
||||||
@ -45,6 +51,14 @@ export default {
|
|||||||
// return data
|
// return data
|
||||||
// },
|
// },
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
searchInput: {
|
||||||
|
deep: true,
|
||||||
|
handler(val) {
|
||||||
|
this.searchAnimal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
showEditDialog(animal) {
|
showEditDialog(animal) {
|
||||||
//这里使用animal的id查询后端的特定动物再赋值,这里用假数据代替一下
|
//这里使用animal的id查询后端的特定动物再赋值,这里用假数据代替一下
|
||||||
@ -88,7 +102,7 @@ export default {
|
|||||||
console.log('删除动物', animal)
|
console.log('删除动物', animal)
|
||||||
return ElMessageBox.confirm(
|
return ElMessageBox.confirm(
|
||||||
'该操作不可撤销,是否继续?',
|
'该操作不可撤销,是否继续?',
|
||||||
'删除动物:'+animal.name,
|
'删除动物:' + animal.name,
|
||||||
{
|
{
|
||||||
confirmButtonText: '确定',
|
confirmButtonText: '确定',
|
||||||
cancelButtonText: '取消',
|
cancelButtonText: '取消',
|
||||||
@ -130,27 +144,38 @@ export default {
|
|||||||
this.dialog.addDialogVisible = false//添加成功关闭窗口
|
this.dialog.addDialogVisible = false//添加成功关闭窗口
|
||||||
|
|
||||||
},
|
},
|
||||||
// searchUser() {
|
searchAnimal() {
|
||||||
// console.log('搜索用户', this.searchInput)
|
console.log('动物搜索',this.searchInput)
|
||||||
// let searchResult = []
|
let searchResult = []//搜索结果数组
|
||||||
// this.users.forEach(e => {
|
//第一步匹配搜索选项,如果选择了特定用户身份
|
||||||
// if (e.username.includes(this.searchInput.keyWord)) {
|
if (typeof this.searchInput.state ==='number') {
|
||||||
// searchResult.push(this.copy(e))
|
this.animals.forEach(e=>{
|
||||||
// }
|
if (e.state === this.searchInput.state) {
|
||||||
// })
|
searchResult.push(copy(e))//如果符合特定权限,则添加到搜索结果
|
||||||
// let searchResult2 = []
|
}
|
||||||
// console.log('this.searchInput.auth', this.searchInput.auth, typeof this.searchInput.auth == 'number')
|
})
|
||||||
// if (typeof this.searchInput.auth === 'number') {
|
}
|
||||||
// searchResult.forEach(e => {
|
else { //如果没有选择特定用户身份,代表所有数据都需要参加到下一轮的模糊搜索
|
||||||
// if (e.auth === this.searchInput.auth) {
|
searchResult = copy(this.animals)
|
||||||
// searchResult2.push(this.copy(e))
|
}
|
||||||
// }
|
|
||||||
// })
|
|
||||||
// searchResult = searchResult2
|
|
||||||
// }
|
|
||||||
// this.tableData = searchResult;
|
|
||||||
// },
|
|
||||||
|
|
||||||
|
let searchResult2 = []
|
||||||
|
let keyset = splitKeyWords(this.searchInput.keyword)//切割关键字
|
||||||
|
|
||||||
|
//第二步进行模糊搜索
|
||||||
|
searchResult.forEach(e=>{
|
||||||
|
//如果该对象能匹配上所有关键字,则添加到结果
|
||||||
|
// console.log('开始进行模糊匹配,对象、关键字集合分别为',e,keyset)
|
||||||
|
let is = fuzzyMatching(e,keyset);
|
||||||
|
if (is) {
|
||||||
|
searchResult2.push(e)
|
||||||
|
}
|
||||||
|
// console.log('结束模糊匹配,结果为',e,keyset,is)
|
||||||
|
})
|
||||||
|
searchResult = searchResult2
|
||||||
|
|
||||||
|
this.tableData = searchResult
|
||||||
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
//数据生成器
|
//数据生成器
|
||||||
@ -182,33 +207,32 @@ export default {
|
|||||||
<el-button type="primary" :icon="DocumentAdd" @click="dialog.addDialogVisible = true">添加动物</el-button>
|
<el-button type="primary" :icon="DocumentAdd" @click="dialog.addDialogVisible = true">添加动物</el-button>
|
||||||
</div>
|
</div>
|
||||||
<div class="right">
|
<div class="right">
|
||||||
<!-- <el-input-->
|
<el-input
|
||||||
<!-- v-model="searchInput.keyWord"-->
|
v-model="searchInput.keyword"
|
||||||
<!-- style="max-width: 300px"-->
|
style="max-width: 300px"
|
||||||
<!-- placeholder="搜索用户"-->
|
placeholder="搜索动物 空格隔开关键字"
|
||||||
<!-- >-->
|
>
|
||||||
<!-- <template #prepend>-->
|
<template #prepend>
|
||||||
<!-- <el-select v-model="searchInput.auth" placeholder="身份" style="width: 90px">-->
|
<el-select v-model="searchInput.state" placeholder="状态" style="width: 80px">
|
||||||
<!-- <el-option label="不选择" value=""/>-->
|
<el-option label="不选择" value=""/>
|
||||||
<!-- <el-option label="管理员" :value="0"/>-->
|
<el-option label="正常" :value="0"/>
|
||||||
<!-- <el-option label="饲养员" :value="1"/>-->
|
<el-option label="异常" :value="1"/>
|
||||||
<!-- <el-option label="兽医" :value="2"/>-->
|
</el-select>
|
||||||
<!-- </el-select>-->
|
</template>
|
||||||
<!-- </template>-->
|
<template #append>
|
||||||
<!-- <template #append>-->
|
<el-button :icon="Search" @click="searchAnimal()"/>
|
||||||
<!-- <el-button :icon="Search" @click="searchUser()"/>-->
|
</template>
|
||||||
<!-- </template>-->
|
</el-input>
|
||||||
<!-- </el-input>-->
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- <hr/>-->
|
<!-- <hr/>-->
|
||||||
<div class="table">
|
<div class="table">
|
||||||
<el-table :data="tableData" style="width: 100%;height: 100%" empty-text="暂无数据" border>
|
<el-table :data="tableData" style="width: 100%;height: 100%" empty-text="暂无数据" border>
|
||||||
<el-table-column fixed prop="id" label="动物ID" />
|
<el-table-column fixed prop="id" label="动物ID"/>
|
||||||
<el-table-column fixed prop="name" label="动物名称" width="100" />
|
<el-table-column fixed prop="name" label="动物名称" width="100"/>
|
||||||
<el-table-column prop="sex" label="性别"/>
|
<el-table-column prop="sex" label="性别"/>
|
||||||
<el-table-column prop="species" label="动物种类" width="90" />
|
<el-table-column prop="species" label="动物种类" width="90"/>
|
||||||
<el-table-column prop="weight" label="动物体重(KG)" width="130"/>
|
<el-table-column prop="weight" label="动物体重(KG)" width="130"/>
|
||||||
<el-table-column prop="height" label="动物身高(M)" width="130"/>
|
<el-table-column prop="height" label="动物身高(M)" width="130"/>
|
||||||
<el-table-column prop="state" label="动物状态" width="90">
|
<el-table-column prop="state" label="动物状态" width="90">
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import {Delete, Edit, Search, User} from "@element-plus/icons-vue";
|
import {Delete, Edit, Search, User} from "@element-plus/icons-vue";
|
||||||
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
import ZooUserFormDialog from "@/components/ZooUserFormDialog.vue";
|
||||||
import {copy} from "@/utils/common.js";
|
import {copy, fuzzyMatching, splitKeyWords} from "@/utils/common.js";
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -57,7 +57,7 @@ export default {
|
|||||||
tableData: [],//用来显示在界面的数据
|
tableData: [],//用来显示在界面的数据
|
||||||
searchInput: {
|
searchInput: {
|
||||||
auth: '',
|
auth: '',
|
||||||
keyWord: ''
|
keyword: ''
|
||||||
},
|
},
|
||||||
dialog: {
|
dialog: {
|
||||||
addDialogVisible: false,//添加用户对话框
|
addDialogVisible: false,//添加用户对话框
|
||||||
@ -154,24 +154,36 @@ export default {
|
|||||||
|
|
||||||
},
|
},
|
||||||
searchUser() {
|
searchUser() {
|
||||||
console.log('搜索用户', this.searchInput)
|
console.log('用户搜索',this.searchInput)
|
||||||
let searchResult = []
|
let searchResult = []//搜索结果数组
|
||||||
this.users.forEach(e => {
|
//第一步匹配搜索选项,如果选择了特定用户身份
|
||||||
if (e.username.includes(this.searchInput.keyWord)) {
|
if (typeof this.searchInput.auth ==='number') {
|
||||||
searchResult.push(copy(e))
|
this.users.forEach(e=>{
|
||||||
}
|
|
||||||
})
|
|
||||||
let searchResult2 = []
|
|
||||||
console.log('this.searchInput.auth', this.searchInput.auth, typeof this.searchInput.auth == 'number')
|
|
||||||
if (typeof this.searchInput.auth === 'number') {
|
|
||||||
searchResult.forEach(e => {
|
|
||||||
if (e.auth === this.searchInput.auth) {
|
if (e.auth === this.searchInput.auth) {
|
||||||
searchResult2.push(copy(e))
|
searchResult.push(copy(e))//如果符合特定权限,则添加到搜索结果
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
searchResult = searchResult2
|
|
||||||
}
|
}
|
||||||
this.tableData = searchResult;
|
else { //如果没有选择特定用户身份,代表所有数据都需要参加到下一轮的模糊搜索
|
||||||
|
searchResult = copy(this.users)
|
||||||
|
}
|
||||||
|
|
||||||
|
let searchResult2 = []
|
||||||
|
let keyset = splitKeyWords(this.searchInput.keyword)//切割关键字
|
||||||
|
|
||||||
|
//第二步进行模糊搜索
|
||||||
|
searchResult.forEach(e=>{
|
||||||
|
//如果该对象能匹配上所有关键字,则添加到结果
|
||||||
|
// console.log('开始进行模糊匹配,对象、关键字集合分别为',e,keyset)
|
||||||
|
let is = fuzzyMatching(e,keyset);
|
||||||
|
if (is) {
|
||||||
|
searchResult2.push(e)
|
||||||
|
}
|
||||||
|
// console.log('结束模糊匹配,结果为',e,keyset,is)
|
||||||
|
})
|
||||||
|
searchResult = searchResult2
|
||||||
|
|
||||||
|
this.tableData = searchResult
|
||||||
},
|
},
|
||||||
//鼠标悬浮显示密码
|
//鼠标悬浮显示密码
|
||||||
showPassword(event,password){
|
showPassword(event,password){
|
||||||
@ -206,7 +218,7 @@ export default {
|
|||||||
</div>
|
</div>
|
||||||
<div class="right">
|
<div class="right">
|
||||||
<el-input
|
<el-input
|
||||||
v-model="searchInput.keyWord"
|
v-model="searchInput.keyword"
|
||||||
style="max-width: 300px"
|
style="max-width: 300px"
|
||||||
placeholder="搜索用户"
|
placeholder="搜索用户"
|
||||||
>
|
>
|
||||||
|
@ -1,6 +1,35 @@
|
|||||||
|
|
||||||
//深拷贝任何值
|
//深拷贝任何值
|
||||||
export function copy(value) {
|
export function copy(value) {
|
||||||
let result = JSON.stringify(value)
|
let result = JSON.stringify(value)
|
||||||
return JSON.parse(result)
|
return JSON.parse(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
//分割字符串
|
||||||
|
export function splitKeyWords(keyword) {
|
||||||
|
let keyset = keyword.split(' ')//提取关键字
|
||||||
|
keyset = Array.from(new Set(keyset))//关键字去重
|
||||||
|
console.log('切割去重好的关键字', keyset)
|
||||||
|
|
||||||
|
return keyset;
|
||||||
|
}
|
||||||
|
|
||||||
|
//模糊匹配,传入一个包含多个关键字的数组、一个对象,若匹配则true
|
||||||
|
export function fuzzyMatching(object, keyset) {
|
||||||
|
//一个对象的所有属性能匹配所有关键字就返回true,every():遇到false就返回false,所有true就返回true
|
||||||
|
return keyset.every(key => {
|
||||||
|
return keywordMatching(object, key)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//传入一个关键字,一个对象,只有关键字能匹配对象的某个属性,则true
|
||||||
|
function keywordMatching(object, keyword) {
|
||||||
|
//该关键字为空,肯定能匹配上
|
||||||
|
if (keyword === '') {
|
||||||
|
console.log('keywordMatching关键字为空', object, keyword, true)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
//只要对象的任意属性能匹配一次关键字就返回true,some():遇到true就返回true,所有false就返回false
|
||||||
|
return Object.keys(object).some(key => {
|
||||||
|
return (object[key] + '').includes(keyword)
|
||||||
|
})
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user