-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: Form node support adjusting the order #2533
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,8 @@ | |
class="border" | ||
v-if="form_data.form_field_list.length > 0" | ||
:data="form_data.form_field_list" | ||
ref="tableRef" | ||
row-key="field" | ||
> | ||
<el-table-column | ||
prop="field" | ||
|
@@ -151,9 +153,11 @@ import { ref, onMounted, computed } from 'vue' | |
import { input_type_list } from '@/components/dynamics-form/constructor/data' | ||
import { MsgError } from '@/utils/message' | ||
import { set, cloneDeep } from 'lodash' | ||
import Sortable from 'sortablejs' | ||
import { t } from '@/locales' | ||
const props = defineProps<{ nodeModel: any }>() | ||
const formNodeFormRef = ref<FormInstance>() | ||
const tableRef = ref() | ||
const editFormField = (form_field_data: any, field_index: number) => { | ||
const _value = form_data.value.form_field_list.map((item: any, index: number) => { | ||
if (field_index === index) { | ||
|
@@ -185,6 +189,7 @@ const sync_form_field_list = () => { | |
] | ||
set(props.nodeModel.properties.config, 'fields', fields) | ||
props.nodeModel.clear_next_node_field(false) | ||
onDragHandle() | ||
} | ||
const addFormCollectRef = ref<InstanceType<typeof AddFormCollect>>() | ||
const editFormCollectRef = ref<InstanceType<typeof EditFormCollect>>() | ||
|
@@ -243,6 +248,30 @@ const validate = () => { | |
function submitDialog(val: string) { | ||
set(props.nodeModel.properties.node_data, 'form_content_format', val) | ||
} | ||
|
||
// 表格排序拖拽 | ||
function onDragHandle() { | ||
if (!tableRef.value) return | ||
|
||
// 获取表格的 tbody DOM 元素 | ||
const wrapper = tableRef.value.$el as HTMLElement | ||
const tbody = wrapper.querySelector('.el-table__body-wrapper tbody') | ||
if (!tbody) return | ||
// 初始化 Sortable | ||
Sortable.create(tbody as HTMLElement, { | ||
animation: 150, | ||
ghostClass: 'ghost-row', | ||
onEnd: (evt) => { | ||
if (evt.oldIndex === undefined || evt.newIndex === undefined) return | ||
// 更新数据顺序 | ||
const items = [...form_data.value.form_field_list] | ||
const [movedItem] = items.splice(evt.oldIndex, 1) | ||
items.splice(evt.newIndex, 0, movedItem) | ||
form_data.value.form_field_list = items | ||
sync_form_field_list() | ||
} | ||
}) | ||
} | ||
onMounted(() => { | ||
set(props.nodeModel, 'validate', validate) | ||
sync_form_field_list() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code is mostly correct for a Vue.js component with an Improvements and Suggestions
Here's a slightly improved version with added checks and comments: // @ts-nocheck
/**
* This component manages and displays form fields dynamically using an el-table.
*/
<template>
<div>
<!-- el-table with sortable functionality -->
<el-table
border
v-if="form_data.form_field_list.length > 0"
:data="form_data.form_field_list"
ref="tableRef"
row-key="field"
@sort-change="handleSortChange"
>
<el-table-column prop="field" label="Field"/>
<!-- Other columns as required -->
</el-table>
<!-- Form actions -->
<button @click.prevent="submitDialog('json')">Submit JSON</button>
</div>
</template>
<script setup lang="ts">
import { useI18n } from 'vue-i18n'
import { ref, computed, onMounted, watchEffect } from 'vue'
import { ElTable, ElButton } from 'element-plus'
import { sortFieldsByIndex } from '@/components/common/utils/sort'
const { t } = useI18n()
interface FormField {
field: string;
type: string; // Add other fields as needed
}
defineProps<{
nodeModel: { properties?: { config?: { fields?: Field[] }; ... } };
}>()
const formNodeFormRef = ref<FormInstance | null>(null)
const tableRef = ref<HTMLDivElement | null>(null)
/** Initialize state management functions here */
onMounted(() => {
sync_form_fields()
})
watchEffect(
() => {
props.nodeModel?.properties?.config?.fields && updateFieldData(props.nodeModel.properties.config.fields!)
},
{ deep: true }
)
async function submitDialog(val: string): Promise<void> {
set(props.nodeModel?.properties ?? {}, "node_data", { form_content_format: val });
}
function handleSortChange(column: { property?: keyof FormField, order?: 'ascending' | 'descending' }): void {
form_data.value.form_field_list.sort(sortFieldsByIndex(column.property!));
sync_form_fields();
}
</script>
<style scoped>
/* Add custom styles if needed */
</style> Additional Notes
These changes aim to make the code more robust and maintainable. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an unexpected blank line after the closing brace of
function onDragHandle()
. Here's how you can fix it:This change ensures that there are no syntax errors in your code. The comment is also missing some spaces which may improve readability.