feat: refs #6891 create composable & update searchbar
This commit is contained in:
parent
f6abcc6693
commit
d444a8721d
|
@ -1,9 +1,9 @@
|
|||
<script setup>
|
||||
import { onMounted, ref } from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { useQuasar } from 'quasar';
|
||||
import { useArrayData } from 'composables/useArrayData';
|
||||
import VnInput from 'src/components/common/VnInput.vue';
|
||||
import useRedirect from 'src/composables/useRedirect';
|
||||
|
||||
const quasar = useQuasar();
|
||||
|
||||
|
@ -65,10 +65,10 @@ const props = defineProps({
|
|||
},
|
||||
});
|
||||
|
||||
const router = useRouter();
|
||||
const arrayData = useArrayData(props.dataKey, { ...props });
|
||||
const { store } = arrayData;
|
||||
const searchText = ref('');
|
||||
const { navigate } = useRedirect();
|
||||
|
||||
onMounted(() => {
|
||||
const params = store.userParams;
|
||||
|
@ -81,37 +81,18 @@ async function search() {
|
|||
const staticParams = Object.entries(store.userParams).filter(
|
||||
([key, value]) => value && (props.staticParams || []).includes(key)
|
||||
);
|
||||
// const filter =props?.where? { where: JSON.parse(props.where) }: {}
|
||||
await arrayData.applyFilter({
|
||||
params: {
|
||||
// filter ,
|
||||
...Object.fromEntries(staticParams),
|
||||
search: searchText.value,
|
||||
},
|
||||
});
|
||||
if (!props.redirect) return;
|
||||
|
||||
if (props.customRouteRedirectName)
|
||||
return router.push({
|
||||
name: props.customRouteRedirectName,
|
||||
params: { id: searchText.value },
|
||||
navigate(store.data, {
|
||||
customRouteRedirectName: props.customRouteRedirectName,
|
||||
searchText: searchText.value,
|
||||
});
|
||||
|
||||
const { matched: matches } = router.currentRoute.value;
|
||||
const { path } = matches.at(-1);
|
||||
const [, moduleName] = path.split('/');
|
||||
|
||||
if (!store.data.length || store.data.length > 1)
|
||||
return router.push({ path: `/${moduleName}/list` });
|
||||
|
||||
const targetId = store.data[0].id;
|
||||
let targetUrl;
|
||||
|
||||
if (path.endsWith('/list')) targetUrl = path.replace('/list', `/${targetId}/summary`);
|
||||
if (path.endsWith('-list')) targetUrl = path.replace('-list', `/${targetId}/summary`);
|
||||
else if (path.includes(':id')) targetUrl = path.replace(':id', targetId);
|
||||
|
||||
await router.push({ path: targetUrl });
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
import { useRouter } from 'vue-router';
|
||||
|
||||
export default function useRedirect() {
|
||||
const router = useRouter();
|
||||
|
||||
const navigate = (data, { customRouteRedirectName, searchText }) => {
|
||||
if (customRouteRedirectName)
|
||||
return router.push({
|
||||
name: customRouteRedirectName,
|
||||
params: { id: searchText },
|
||||
});
|
||||
|
||||
const { matched: matches } = router.currentRoute.value;
|
||||
const { path } = matches.at(-1);
|
||||
const [, moduleName] = path.split('/');
|
||||
|
||||
if (!data.length || data.length > 1)
|
||||
return router.push({ path: `/${moduleName}/list` });
|
||||
|
||||
const targetId = data[0].id;
|
||||
|
||||
if (/\/list|-list/.test(path))
|
||||
router.push({ path: `/${moduleName}/${targetId}/summary` });
|
||||
else router.push({ path: path.replace(/:id/, targetId) });
|
||||
};
|
||||
|
||||
return { navigate };
|
||||
}
|
Loading…
Reference in New Issue