| ¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <div class="app-container"> |
| | | <div class="search-form"> |
| | | <el-form :inline="true" :model="searchForm"> |
| | | <el-form-item label="é¨é¨"> |
| | | <el-input |
| | | v-model="searchForm.department" |
| | | placeholder="请è¾å
¥é¨é¨åç§°" |
| | | clearable |
| | | style="width: 200px" |
| | | /> |
| | | </el-form-item> |
| | | <el-form-item label="å§å"> |
| | | <el-input |
| | | v-model="searchForm.name" |
| | | placeholder="请è¾å
¥å§å" |
| | | clearable |
| | | style="width: 200px" |
| | | /> |
| | | </el-form-item> |
| | | <el-form-item> |
| | | <el-button type="primary" @click="handleSearch">æç´¢</el-button> |
| | | <el-button @click="handleReset">éç½®</el-button> |
| | | <el-button type="success" @click="handleAdd">æ°å¢</el-button> |
| | | </el-form-item> |
| | | </el-form> |
| | | </div> |
| | | |
| | | <div class="table-container"> |
| | | <DynamicTable |
| | | ref="dynamicTableRef" |
| | | :data="tableData" |
| | | :dict-types="dictTypes" |
| | | :loading="loading" |
| | | :show-selection="true" |
| | | :show-actions="true" |
| | | :show-pagination="true" |
| | | :pagination="pagination" |
| | | height="calc(100vh - 280px)" |
| | | @selection-change="handleSelectionChange" |
| | | @edit="handleEdit" |
| | | @delete="handleDelete" |
| | | @select-change="handleSelectChange" |
| | | @input-change="handleInputChange" |
| | | @size-change="handleSizeChange" |
| | | @current-change="handleCurrentChange" |
| | | /> |
| | | </div> |
| | | |
| | | <!-- æ°å¢/ç¼è¾å¯¹è¯æ¡ --> |
| | | <el-dialog |
| | | v-model="dialogVisible" |
| | | :title="dialogTitle" |
| | | width="600px" |
| | | append-to-body |
| | | > |
| | | <el-form |
| | | ref="formRef" |
| | | :model="form" |
| | | :rules="rules" |
| | | label-width="100px" |
| | | > |
| | | <el-form-item label="é¨é¨" prop="department"> |
| | | <el-input v-model="form.department" placeholder="请è¾å
¥é¨é¨" /> |
| | | </el-form-item> |
| | | <el-form-item label="å§å" prop="name"> |
| | | <el-input v-model="form.name" placeholder="请è¾å
¥å§å" /> |
| | | </el-form-item> |
| | | <el-form-item label="å·¥å·" prop="employeeId"> |
| | | <el-input v-model="form.employeeId" placeholder="请è¾å
¥å·¥å·" /> |
| | | </el-form-item> |
| | | |
| | | <!-- å¨æè¡¨åé¡¹ï¼æ ¹æ®åå
¸çæ --> |
| | | <el-form-item |
| | | v-for="dictItem in dynamicFormItems" |
| | | :key="dictItem.value" |
| | | :label="dictItem.label" |
| | | :prop="dictItem.value" |
| | | > |
| | | <el-select |
| | | v-model="form[dictItem.value]" |
| | | placeholder="è¯·éæ©" |
| | | style="width: 100%" |
| | | > |
| | | <el-option |
| | | v-for="option in dictItem.options" |
| | | :key="option.value" |
| | | :label="option.label" |
| | | :value="option.value" |
| | | /> |
| | | </el-select> |
| | | </el-form-item> |
| | | </el-form> |
| | | |
| | | <template #footer> |
| | | <div class="dialog-footer"> |
| | | <el-button type="primary" @click="handleSubmit">ç¡®å®</el-button> |
| | | <el-button @click="dialogVisible = false">åæ¶</el-button> |
| | | </div> |
| | | </template> |
| | | </el-dialog> |
| | | </div> |
| | | </template> |
| | | |
| | | <script setup> |
| | | import { ref, reactive, computed, onMounted } from 'vue' |
| | | import { ElMessage, ElMessageBox } from 'element-plus' |
| | | import DynamicTable from '@/components/DynamicTable/index.vue' |
| | | |
| | | // ååºå¼æ°æ® |
| | | const loading = ref(false) |
| | | const dialogVisible = ref(false) |
| | | const dialogTitle = ref('') |
| | | const editIndex = ref(-1) |
| | | const selectedRows = ref([]) |
| | | |
| | | // æç´¢è¡¨å |
| | | const searchForm = reactive({ |
| | | department: '', |
| | | name: '' |
| | | }) |
| | | |
| | | // è¡¨æ ¼æ°æ® |
| | | const tableData = ref([ |
| | | { |
| | | id: 1, |
| | | department: 'ææ¯é¨', |
| | | name: 'å¼ ä¸', |
| | | employeeId: 'EMP001', |
| | | status: '1', |
| | | level: '2', |
| | | position: '1' |
| | | }, |
| | | { |
| | | id: 2, |
| | | department: '人äºé¨', |
| | | name: 'æå', |
| | | employeeId: 'EMP002', |
| | | status: '0', |
| | | level: '1', |
| | | position: '2' |
| | | }, |
| | | { |
| | | id: 3, |
| | | department: 'è´¢å¡é¨', |
| | | name: 'çäº', |
| | | employeeId: 'EMP003', |
| | | status: '1', |
| | | level: '3', |
| | | position: '1' |
| | | } |
| | | ]) |
| | | |
| | | // åå
¸ç±»åé
ç½® |
| | | const dictTypes = ref([ |
| | | 'sys_normal_disable', // ç¶æåå
¸ |
| | | 'sys_user_level', // 级å«åå
¸ |
| | | 'sys_user_position' // èä½åå
¸ |
| | | ]) |
| | | |
| | | // å页é
ç½® |
| | | const pagination = reactive({ |
| | | current: 1, |
| | | size: 10, |
| | | total: 0 |
| | | }) |
| | | |
| | | // è¡¨åæ°æ® |
| | | const form = reactive({ |
| | | department: '', |
| | | name: '', |
| | | employeeId: '', |
| | | status: '', |
| | | level: '', |
| | | position: '' |
| | | }) |
| | | |
| | | // 表åéªè¯è§å |
| | | const rules = { |
| | | department: [ |
| | | { required: true, message: '请è¾å
¥é¨é¨', trigger: 'blur' } |
| | | ], |
| | | name: [ |
| | | { required: true, message: '请è¾å
¥å§å', trigger: 'blur' } |
| | | ], |
| | | employeeId: [ |
| | | { required: true, message: '请è¾å
¥å·¥å·', trigger: 'blur' } |
| | | ] |
| | | } |
| | | |
| | | // å¨æè¡¨å项 |
| | | const dynamicFormItems = computed(() => { |
| | | // è¿éå¯ä»¥æ ¹æ®åå
¸æ°æ®å¨æçæè¡¨å项 |
| | | return [ |
| | | { |
| | | label: 'ç¶æ', |
| | | value: 'status', |
| | | options: [ |
| | | { label: 'å¯ç¨', value: '1' }, |
| | | { label: 'ç¦ç¨', value: '0' } |
| | | ] |
| | | }, |
| | | { |
| | | label: '级å«', |
| | | value: 'level', |
| | | options: [ |
| | | { label: 'å级', value: '1' }, |
| | | { label: 'ä¸çº§', value: '2' }, |
| | | { label: 'é«çº§', value: '3' } |
| | | ] |
| | | }, |
| | | { |
| | | label: 'èä½', |
| | | value: 'position', |
| | | options: [ |
| | | { label: 'åå·¥', value: '1' }, |
| | | { label: '主管', value: '2' }, |
| | | { label: 'ç»ç', value: '3' } |
| | | ] |
| | | } |
| | | ] |
| | | }) |
| | | |
| | | // ç»ä»¶å¼ç¨ |
| | | const dynamicTableRef = ref(null) |
| | | const formRef = ref(null) |
| | | |
| | | // äºä»¶å¤ç彿° |
| | | const handleSearch = () => { |
| | | // å®ç°æç´¢é»è¾ |
| | | console.log('æç´¢æ¡ä»¶:', searchForm) |
| | | ElMessage.success('æç´¢åè½å¾
å®ç°') |
| | | } |
| | | |
| | | const handleReset = () => { |
| | | searchForm.department = '' |
| | | searchForm.name = '' |
| | | } |
| | | |
| | | const handleAdd = () => { |
| | | dialogTitle.value = 'æ°å¢åå·¥' |
| | | editIndex.value = -1 |
| | | resetForm() |
| | | dialogVisible.value = true |
| | | } |
| | | |
| | | const handleEdit = (row, index) => { |
| | | dialogTitle.value = 'ç¼è¾åå·¥' |
| | | editIndex.value = index |
| | | Object.assign(form, row) |
| | | dialogVisible.value = true |
| | | } |
| | | |
| | | const handleDelete = async (row, index) => { |
| | | try { |
| | | await ElMessageBox.confirm('ç¡®å®è¦å é¤è¿æ¡è®°å½åï¼', 'æç¤º', { |
| | | type: 'warning' |
| | | }) |
| | | |
| | | tableData.value.splice(index, 1) |
| | | ElMessage.success('å 餿å') |
| | | } catch (error) { |
| | | // ç¨æ·åæ¶å é¤ |
| | | } |
| | | } |
| | | |
| | | const handleSelectionChange = (selection) => { |
| | | selectedRows.value = selection |
| | | } |
| | | |
| | | const handleSelectChange = (row, prop, value) => { |
| | | console.log('éæ©åå:', row, prop, value) |
| | | // å¯ä»¥å¨è¿éå¤çæ°æ®æ´æ°é»è¾ |
| | | } |
| | | |
| | | const handleInputChange = (row, prop, value) => { |
| | | console.log('è¾å
¥åå:', row, prop, value) |
| | | // å¯ä»¥å¨è¿éå¤çæ°æ®æ´æ°é»è¾ |
| | | } |
| | | |
| | | const handleSizeChange = (size) => { |
| | | pagination.size = size |
| | | // éæ°å è½½æ°æ® |
| | | } |
| | | |
| | | const handleCurrentChange = (current) => { |
| | | pagination.current = current |
| | | // éæ°å è½½æ°æ® |
| | | } |
| | | |
| | | const handleSubmit = async () => { |
| | | try { |
| | | await formRef.value.validate() |
| | | |
| | | if (editIndex.value === -1) { |
| | | // æ°å¢ |
| | | const newRow = { |
| | | id: Date.now(), |
| | | ...form |
| | | } |
| | | tableData.value.push(newRow) |
| | | ElMessage.success('æ°å¢æå') |
| | | } else { |
| | | // ç¼è¾ |
| | | Object.assign(tableData.value[editIndex.value], form) |
| | | ElMessage.success('ç¼è¾æå') |
| | | } |
| | | |
| | | dialogVisible.value = false |
| | | } catch (error) { |
| | | console.error('表åéªè¯å¤±è´¥:', error) |
| | | } |
| | | } |
| | | |
| | | const resetForm = () => { |
| | | Object.assign(form, { |
| | | department: '', |
| | | name: '', |
| | | employeeId: '', |
| | | status: '', |
| | | level: '', |
| | | position: '' |
| | | }) |
| | | formRef.value?.resetFields() |
| | | } |
| | | |
| | | // ç»ä»¶æè½½æ¶åå§åæ°æ® |
| | | onMounted(() => { |
| | | pagination.total = tableData.value.length |
| | | }) |
| | | </script> |
| | | |
| | | <style scoped> |
| | | .app-container { |
| | | padding: 20px; |
| | | } |
| | | |
| | | .search-form { |
| | | margin-bottom: 20px; |
| | | padding: 20px; |
| | | background-color: #f5f5f5; |
| | | border-radius: 4px; |
| | | } |
| | | |
| | | .table-container { |
| | | background-color: #fff; |
| | | border-radius: 4px; |
| | | padding: 20px; |
| | | } |
| | | |
| | | .dialog-footer { |
| | | text-align: right; |
| | | } |
| | | </style> |