feat(support): 添加分类筛选的功能 #39
@ -1,10 +1,12 @@
|
|||||||
<!-- eslint-disable vue/no-v-html -->
|
<!-- eslint-disable vue/no-v-html -->
|
||||||
<template>
|
<template>
|
||||||
<!-- 用 v-html 渲染解析后的 HTML -->
|
<!-- 用 v-html 渲染解析后的 HTML -->
|
||||||
<div class="markdown-body" v-html="safeHtml" />
|
<div ref="container" class="markdown-body" v-html="safeHtml" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { createApp } from 'vue';
|
||||||
|
import MarkdownTable from './MarkdownTable.vue';
|
||||||
interface Props {
|
interface Props {
|
||||||
content: string;
|
content: string;
|
||||||
}
|
}
|
||||||
@ -17,7 +19,44 @@
|
|||||||
const safeHtml = computed(() => renderMarkdown(contentWithAbsoluteUrls));
|
const safeHtml = computed(() => renderMarkdown(contentWithAbsoluteUrls));
|
||||||
// const safeHtml = computed(() => renderMarkdown(props.content))
|
// const safeHtml = computed(() => renderMarkdown(props.content))
|
||||||
|
|
||||||
console.log('Rendered HTML:', safeHtml.value);
|
const container = ref<HTMLElement | null>(null);
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
await nextTick();
|
||||||
|
if (!safeHtml.value) return;
|
||||||
|
console.log(safeHtml.value);
|
||||||
|
|
||||||
|
// 查找所有 table
|
||||||
|
const tables = container.value.querySelectorAll('table');
|
||||||
|
console.log(tables);
|
||||||
|
tables.forEach((table) => {
|
||||||
|
// 1. 提取表头
|
||||||
|
const headers = Array.from(table.querySelectorAll('thead th')).map(
|
||||||
|
(th) => th.textContent?.trim() ?? ''
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. 提取行数据
|
||||||
|
const rows = Array.from(table.querySelectorAll('tbody tr')).map((tr) => {
|
||||||
|
const cells = Array.from(tr.querySelectorAll('td')).map(
|
||||||
|
(td) => td.textContent?.trim() ?? ''
|
||||||
|
);
|
||||||
|
const obj: Record<string, string> = {};
|
||||||
|
headers.forEach((h, i) => {
|
||||||
|
obj[h] = cells[i];
|
||||||
|
});
|
||||||
|
return obj;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 3. 创建 Vue 子应用,把原生 table 替换成 <md-table>
|
||||||
|
const mountPoint = document.createElement('div');
|
||||||
|
table.replaceWith(mountPoint);
|
||||||
|
|
||||||
|
const app = createApp(MarkdownTable, { headers, rows });
|
||||||
|
app.mount(mountPoint);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// console.log('Rendered HTML:', safeHtml.value);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|||||||
12
app/components/MarkdownTable.vue
Normal file
12
app/components/MarkdownTable.vue
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<template>
|
||||||
|
<el-table :data="rows" border>
|
||||||
|
<el-table-column v-for="h in headers" :key="h" :prop="h" :label="h" />
|
||||||
|
</el-table>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
defineProps<{
|
||||||
|
headers: string[];
|
||||||
|
rows: Record<string, string>[];
|
||||||
|
}>();
|
||||||
|
</script>
|
||||||
@ -2,10 +2,10 @@
|
|||||||
<div class="question-list">
|
<div class="question-list">
|
||||||
<el-collapse class="question-collapse" accordion>
|
<el-collapse class="question-collapse" accordion>
|
||||||
<el-collapse-item
|
<el-collapse-item
|
||||||
v-for="(question, index) in questions"
|
v-for="question in questions"
|
||||||
:key="index"
|
:key="question.documentId"
|
||||||
:title="question.title"
|
:title="question.title"
|
||||||
:name="String(index)"
|
:name="question.documentId"
|
||||||
>
|
>
|
||||||
<markdown-renderer :content="question.content || ''" />
|
<markdown-renderer :content="question.content || ''" />
|
||||||
</el-collapse-item>
|
</el-collapse-item>
|
||||||
@ -16,7 +16,11 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
defineProps({
|
defineProps({
|
||||||
questions: {
|
questions: {
|
||||||
type: Array as () => Array<{ title: string; content: string }>,
|
type: Array as () => Array<{
|
||||||
|
title: string;
|
||||||
|
content: string;
|
||||||
|
documentId: string;
|
||||||
|
}>,
|
||||||
default: () => [],
|
default: () => [],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@ -25,32 +25,133 @@
|
|||||||
</el-breadcrumb-item>
|
</el-breadcrumb-item>
|
||||||
</el-breadcrumb>
|
</el-breadcrumb>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="document-category">
|
||||||
|
<el-select v-model="selectedType" placeholder="选择产品类型" clearable>
|
||||||
|
<el-option
|
||||||
|
v-for="type in productionTypeOptions"
|
||||||
|
:key="type.documentId"
|
||||||
|
:label="type.type"
|
||||||
|
:value="type.documentId"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
<el-select
|
||||||
|
v-model="selectedProduction"
|
||||||
|
placeholder="选择系列产品"
|
||||||
|
clearable
|
||||||
|
>
|
||||||
|
<el-option
|
||||||
|
v-for="production in productionOptions"
|
||||||
|
:key="production.documentId"
|
||||||
|
:label="production.title"
|
||||||
|
:value="production.documentId"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
<el-input
|
||||||
|
v-model="keyword"
|
||||||
|
placeholder="输入关键词..."
|
||||||
|
clearable
|
||||||
|
:prefix-icon="Search"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div class="page-content">
|
<div class="page-content">
|
||||||
<document-list :documents="documents" />
|
<document-list :documents="filteredDocuments" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { Search } from '@element-plus/icons-vue';
|
||||||
|
|
||||||
const { find } = useStrapi();
|
const { find } = useStrapi();
|
||||||
const { getStrapiLocale } = useLocalizations();
|
const { getStrapiLocale } = useLocalizations();
|
||||||
const strapiLocale = getStrapiLocale();
|
const strapiLocale = getStrapiLocale();
|
||||||
|
|
||||||
const { data, pending, error } = useAsyncData('documents', () =>
|
const { data, pending, error } = useAsyncData('documents', () =>
|
||||||
find<ProductionDocument>('production-documents', {
|
find<ProductionDocument>('production-documents', {
|
||||||
populate: 'document',
|
populate: ['document', 'related_productions.production_type'],
|
||||||
locale: strapiLocale,
|
locale: strapiLocale,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const documents = computed(
|
// const documents = computed(
|
||||||
() =>
|
// () =>
|
||||||
data.value?.data.map((item) => ({
|
// data.value?.data.map((item) => ({
|
||||||
|
// ...item.document,
|
||||||
|
// })) || []
|
||||||
|
// );
|
||||||
|
const documents = computed(() => data.value?.data ?? []);
|
||||||
|
|
||||||
|
const keyword = ref('');
|
||||||
|
|
||||||
|
const selectedType = ref<string | null>(null);
|
||||||
|
const selectedProduction = ref<string | null>(null);
|
||||||
|
|
||||||
|
const productionTypeOptions = computed(() => {
|
||||||
|
const types: ProductionType[] = [];
|
||||||
|
documents.value.forEach((document: ProductionDocument) => {
|
||||||
|
document.related_productions?.forEach((production: Production) => {
|
||||||
|
const productionType = production?.production_type;
|
||||||
|
if (!types.some((p) => p.documentId === productionType.documentId)) {
|
||||||
|
types.push(productionType);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return types;
|
||||||
|
});
|
||||||
|
|
||||||
|
const productionOptions = computed(() => {
|
||||||
|
if (!selectedType.value) return [];
|
||||||
|
const productions: Production[] = [];
|
||||||
|
documents.value.forEach((document: ProductionDocument) => {
|
||||||
|
document.related_productions.forEach((production: Production) => {
|
||||||
|
if (
|
||||||
|
production.production_type?.documentId === selectedType.value &&
|
||||||
|
!productions.some((p) => p.documentId === production.documentId)
|
||||||
|
) {
|
||||||
|
productions.push(production);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return productions;
|
||||||
|
});
|
||||||
|
|
||||||
|
const filteredDocuments = computed(() =>
|
||||||
|
documents.value
|
||||||
|
.filter((document: ProductionDocument) => {
|
||||||
|
const matchProduction = selectedProduction.value
|
||||||
|
? document.related_productions?.some(
|
||||||
|
(production: Production) =>
|
||||||
|
production.documentId === selectedProduction.value
|
||||||
|
)
|
||||||
|
: selectedType.value
|
||||||
|
? document.related_productions?.some(
|
||||||
|
(production: Production) =>
|
||||||
|
production.production_type?.documentId === selectedType.value
|
||||||
|
)
|
||||||
|
: true;
|
||||||
|
|
||||||
|
const matchKeyword = keyword.value
|
||||||
|
? document.document.caption &&
|
||||||
|
document.document.caption.includes(keyword.value)
|
||||||
|
: true;
|
||||||
|
|
||||||
|
return matchProduction && matchKeyword;
|
||||||
|
})
|
||||||
|
.map((item) => ({
|
||||||
...item.document,
|
...item.document,
|
||||||
})) || []
|
}))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
watch(selectedType, () => {
|
||||||
|
selectedProduction.value = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(documents, (value) => {
|
||||||
|
console.log(value);
|
||||||
|
});
|
||||||
|
|
||||||
watch(error, (value) => {
|
watch(error, (value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
console.error('数据获取失败: ', value);
|
console.error('数据获取失败: ', value);
|
||||||
@ -80,6 +181,12 @@
|
|||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.document-category {
|
||||||
|
display: flex;
|
||||||
|
padding: 0rem 2rem;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
.page-content {
|
.page-content {
|
||||||
padding: 1rem 2rem 2rem;
|
padding: 1rem 2rem 2rem;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,26 +25,125 @@
|
|||||||
</el-breadcrumb-item>
|
</el-breadcrumb-item>
|
||||||
</el-breadcrumb>
|
</el-breadcrumb>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="question-category">
|
||||||
|
<el-select v-model="selectedType" placeholder="选择产品类型" clearable>
|
||||||
|
<el-option
|
||||||
|
v-for="type in productionTypeOptions"
|
||||||
|
:key="type.documentId"
|
||||||
|
:label="type.type"
|
||||||
|
:value="type.documentId"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
<el-select
|
||||||
|
v-model="selectedProduction"
|
||||||
|
placeholder="选择系列产品"
|
||||||
|
clearable
|
||||||
|
>
|
||||||
|
<el-option
|
||||||
|
v-for="production in productionOptions"
|
||||||
|
:key="production.documentId"
|
||||||
|
:label="production.title"
|
||||||
|
:value="production.documentId"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
|
<el-input
|
||||||
|
v-model="keyword"
|
||||||
|
placeholder="输入关键词..."
|
||||||
|
clearable
|
||||||
|
:prefix-icon="Search"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div class="page-content">
|
<div class="page-content">
|
||||||
<question-list :questions="questions" />
|
<question-list :questions="filteredQuestions" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { Search } from '@element-plus/icons-vue';
|
||||||
const { find } = useStrapi();
|
const { find } = useStrapi();
|
||||||
const { getStrapiLocale } = useLocalizations();
|
const { getStrapiLocale } = useLocalizations();
|
||||||
const strapiLocale = getStrapiLocale();
|
const strapiLocale = getStrapiLocale();
|
||||||
|
|
||||||
const { data, pending, error } = useAsyncData('questions', () =>
|
const { data, pending, error } = useAsyncData('questions', () =>
|
||||||
find<Question>('questions', {
|
find<Question>('questions', {
|
||||||
|
populate: {
|
||||||
|
related_productions: {
|
||||||
|
populate: ['production_type'],
|
||||||
|
},
|
||||||
|
},
|
||||||
locale: strapiLocale,
|
locale: strapiLocale,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const questions = computed(() => data.value?.data ?? null);
|
const questions = computed(() => data.value?.data ?? null);
|
||||||
|
|
||||||
|
const keyword = ref('');
|
||||||
|
|
||||||
|
const selectedType = ref<string | null>(null);
|
||||||
|
const selectedProduction = ref<string | null>(null);
|
||||||
|
|
||||||
|
const productionTypeOptions = computed(() => {
|
||||||
|
const types: ProductionType[] = [];
|
||||||
|
questions.value.forEach((q: Question) => {
|
||||||
|
q.related_productions?.forEach((production: Production) => {
|
||||||
|
const productionType = production?.production_type;
|
||||||
|
if (!types.some((p) => p.documentId === productionType.documentId)) {
|
||||||
|
types.push(productionType);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return types;
|
||||||
|
});
|
||||||
|
|
||||||
|
const productionOptions = computed(() => {
|
||||||
|
if (!selectedType.value) return [];
|
||||||
|
const productions: Production[] = [];
|
||||||
|
questions.value.forEach((question: Question) => {
|
||||||
|
question.related_productions.forEach((production: Production) => {
|
||||||
|
if (
|
||||||
|
production.production_type?.documentId === selectedType.value &&
|
||||||
|
!productions.some((p) => p.documentId === production.documentId)
|
||||||
|
) {
|
||||||
|
productions.push(production);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return productions;
|
||||||
|
});
|
||||||
|
|
||||||
|
const filteredQuestions = computed(() => {
|
||||||
|
return questions.value.filter((question: Question) => {
|
||||||
|
const matchProduction = selectedProduction.value
|
||||||
|
? question.related_productions?.some(
|
||||||
|
(production: Production) =>
|
||||||
|
production.documentId === selectedProduction.value
|
||||||
|
)
|
||||||
|
: selectedType.value
|
||||||
|
? question.related_productions?.some(
|
||||||
|
(production: Production) =>
|
||||||
|
production.production_type?.documentId === selectedType.value
|
||||||
|
)
|
||||||
|
: true;
|
||||||
|
|
||||||
|
const matchKeyword = keyword.value
|
||||||
|
? (question.title && question.title.includes(keyword.value)) ||
|
||||||
|
(question.content && question.content.includes(keyword.value))
|
||||||
|
: true;
|
||||||
|
|
||||||
|
return matchProduction && matchKeyword;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(selectedType, () => {
|
||||||
|
selectedProduction.value = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(data, (newVal) => {
|
||||||
|
console.log('useAsyncData updated:', newVal);
|
||||||
|
});
|
||||||
|
|
||||||
watch(error, (value) => {
|
watch(error, (value) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
console.error('数据获取失败: ', value);
|
console.error('数据获取失败: ', value);
|
||||||
@ -74,6 +173,12 @@
|
|||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.question-category {
|
||||||
|
display: flex;
|
||||||
|
padding: 0rem 2rem;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
.page-content {
|
.page-content {
|
||||||
padding: 1rem 2rem 2rem;
|
padding: 1rem 2rem 2rem;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user