layui/example/src/components/LaySearch.vue

124 lines
2.8 KiB
Vue
Raw Normal View History

2021-10-06 15:22:30 +00:00
<template>
<select :name="name" lay-verify="required" />
2021-10-06 15:22:30 +00:00
<div
2021-10-22 01:18:40 +00:00
class="layui-unselect layui-form-select layui-search"
2021-10-06 15:22:30 +00:00
:class="[openState ? 'layui-form-selected' : '']"
@click="open"
2021-10-22 01:18:40 +00:00
style="margin-left: 20px"
2021-10-06 15:22:30 +00:00
>
<div class="layui-select-title">
<input
type="text"
2021-10-09 17:07:37 +00:00
placeholder="Search"
2021-10-06 15:22:30 +00:00
class="layui-input layui-unselect"
2021-10-09 08:15:02 +00:00
:value="name"
2021-10-06 15:22:30 +00:00
style="
background: rgba(255, 255, 255, 0.05);
border: none;
2021-10-22 01:18:40 +00:00
color: rgba(255, 255, 255, 0.7);
width: 196px;
height: 34px;
2021-10-06 15:22:30 +00:00
"
@input="change"
2021-10-22 01:18:40 +00:00
/><i class="layui-edge"></i>
2021-10-06 15:22:30 +00:00
</div>
<dl class="layui-anim layui-anim-upbit" style="">
2021-10-09 08:15:02 +00:00
<dd v-if="menus.length <= 0" class="layui-select-tips">无内容</dd>
2021-10-06 15:22:30 +00:00
<dd
2021-10-09 08:15:02 +00:00
v-for="data in menus"
v-else
2021-10-06 15:22:30 +00:00
:key="data"
:value="name"
2021-10-06 15:22:30 +00:00
class="layui-select-tips"
@click="jump(data)"
2021-10-06 15:22:30 +00:00
>
{{ data.title }}
</dd>
</dl>
</div>
</template>
<script setup name="LaySelect" lang="ts">
import { ref } from 'vue'
2021-10-06 15:22:30 +00:00
import { useRouter, useRoute } from 'vue-router'
2022-02-23 10:38:47 +00:00
import { Recordable } from '../../../src/types'
2021-10-06 15:22:30 +00:00
const props = defineProps<{
datas?: Recordable[]
}>()
2021-10-06 15:22:30 +00:00
const route = useRoute()
const router = useRouter()
const openState = ref(false)
2021-10-09 08:15:02 +00:00
const menus = ref(props.datas)
2021-10-06 15:22:30 +00:00
const name = ref('')
const open = function () {
openState.value = !openState.value
}
const jump = function (data: any) {
name.value = data.title
2021-10-09 08:15:02 +00:00
router.push(data.path)
}
const change = function (e: any) {
2021-10-09 08:15:02 +00:00
name.value = e.target.value
if (e.target.value === '') {
2021-10-09 08:15:02 +00:00
menus.value = props.datas
} else {
menus.value = searchList(e.target.value, props.datas)
2021-10-09 08:15:02 +00:00
}
}
const searchList = function (str: string, container: any) {
2021-10-09 08:15:02 +00:00
var newList = []
var startChar = str.charAt(0)
var strLen = str.length
for (var i = 0; i < container.length; i++) {
var obj = container[i]
var isMatch = false
for (var p in obj) {
if (typeof obj[p] == 'function') {
obj[p]()
} else {
var curItem = ''
if (obj[p] != null) {
curItem = obj[p]
}
for (var j = 0; j < curItem.length; j++) {
if (curItem.charAt(j) == startChar) {
if (curItem.substring(j).substring(0, strLen) == str) {
isMatch = true
break
}
}
}
}
}
if (isMatch) {
newList.push(obj)
}
}
return newList
2021-10-06 15:22:30 +00:00
}
</script>
2021-10-22 01:18:40 +00:00
<style>
.layui-search .layui-anim::-webkit-scrollbar {
width: 6px;
height: 6px;
}
.layui-search .layui-anim::-webkit-scrollbar-corner {
background: #f6f6f6;
}
.layui-search .layui-anim::-webkit-scrollbar-thumb {
background: #e6e6e6;
border-radius: 2px;
}
.layui-search .layui-anim::-webkit-scrollbar-track {
background: white;
border-radius: 2px;
}
</style>