Commit cfe4cc3c by yubin

Merge remote-tracking branch 'origin/master'

parents 78ecde54 d58b4b1e
...@@ -23,15 +23,15 @@ export default { ...@@ -23,15 +23,15 @@ export default {
}, },
sm: { sm: {
type: Number, type: Number,
default: 24 default: 12
}, },
md: { md: {
type: Number, type: Number,
default: 12 default: 8
}, },
lg: { lg: {
type: Number, type: Number,
default: 8 default: 6
}, },
xl: { xl: {
type: Number, type: Number,
...@@ -45,21 +45,20 @@ export default { ...@@ -45,21 +45,20 @@ export default {
ElFormRef: null, ElFormRef: null,
max: 0, max: 0,
isAdvanced: true, isAdvanced: true,
isFold: true, // 默认折叠状态 isFold: true,
labelWidthValue: '', labelWidthValue: '',
inputEvents: [], inputEvents: [],
screenWidth: 1920 screenWidth: 1920,
visibleFormItemsCount: 0 // 新增:跟踪可见表单项数量
} }
}, },
computed: { computed: {
gutter() { gutter() {
return (24 / this.max % 24) || 0 return (24 / this.max % 24) || 0
}, },
// 计算每行最多能显示的列数
maxItemsPerRow() { maxItemsPerRow() {
return Math.floor(24 / this.currentColWidth) return Math.floor(24 / this.currentColWidth)
}, },
// 获取当前屏幕尺寸对应的列宽
currentColWidth() { currentColWidth() {
if (this.screenWidth >= XL) return this.xl if (this.screenWidth >= XL) return this.xl
if (this.screenWidth >= LG) return this.lg if (this.screenWidth >= LG) return this.lg
...@@ -67,18 +66,47 @@ export default { ...@@ -67,18 +66,47 @@ export default {
if (this.screenWidth >= SM) return this.sm if (this.screenWidth >= SM) return this.sm
return this.xs return this.xs
}, },
// 是否显示折叠按钮
shouldShowFoldButton() { shouldShowFoldButton() {
const formItems = this.extractFormItems(this.$slots.default || []) const formItems = this.extractFormItems(this.$slots.default || [])
return formItems.length > this.maxItemsPerRow - 1 return formItems.length > this.maxItemsPerRow - 1
}, },
// 折叠状态下每行最多显示的表单项数量(包括按钮列)
maxVisibleItemsWhenFold() { maxVisibleItemsWhenFold() {
return Math.max(1, this.maxItemsPerRow - 1) // 减去按钮列 return Math.max(1, this.maxItemsPerRow - 1)
},
// 修改:计算按钮列应该占用的实际列宽
buttonColWidth() {
if (this.shouldWrapButtons) {
return 24 // 按钮在单独一行时占满整行
}
// 计算剩余空间,使按钮右对齐
const visibleCount = this.visibleFormItemsCount
const itemsInRow = this.maxItemsPerRow
// 如果没有可见的表单项,按钮占整行
if (visibleCount === 0) return 24
// 计算当前行已有的列数
const occupiedCols = visibleCount * this.currentColWidth
const remainingCols = 24 - occupiedCols
// 如果剩余空间足够放下按钮列,使用剩余空间
if (remainingCols >= this.currentColWidth) {
return remainingCols
} else {
// 如果剩余空间不够,按钮换行并右对齐
return 24
}
}, },
// 是否需要将按钮放在单独一行 // 修改:是否需要换行显示按钮
shouldWrapButtons() { shouldWrapButtons() {
return !this.isFold && this.shouldShowFoldButton if (!this.isFold) {
// 展开状态下,如果可见表单项数量大于等于每行最大数量,按钮换行
return this.visibleFormItemsCount >= this.maxItemsPerRow
}
// 折叠状态下,如果有需要隐藏的表单项,按钮和表单项在同一行
return false
} }
}, },
mounted() { mounted() {
...@@ -92,6 +120,7 @@ export default { ...@@ -92,6 +120,7 @@ export default {
this.observer.observe(this.ElFormRef.$el) this.observer.observe(this.ElFormRef.$el)
} }
this.initSize() this.initSize()
this.updateVisibleCount()
window.addEventListener('resize', this.handleResize) window.addEventListener('resize', this.handleResize)
}, },
...@@ -104,15 +133,16 @@ export default { ...@@ -104,15 +133,16 @@ export default {
} }
window.removeEventListener('resize', this.handleResize) window.removeEventListener('resize', this.handleResize)
}, },
updated() {
this.updateVisibleCount()
},
methods: { methods: {
// resetFields() {
// if (this.ElFormRef && typeof this.ElFormRef.resetFields === 'function') {
// this.ElFormRef.resetFields()
// }
// },
handleResize() { handleResize() {
this.screenWidth = document.documentElement.clientWidth this.screenWidth = document.documentElement.clientWidth
this.initSize() this.initSize()
this.$nextTick(() => {
this.updateVisibleCount()
})
}, },
initSize() { initSize() {
this.max = 24 / this.currentColWidth this.max = 24 / this.currentColWidth
...@@ -134,13 +164,19 @@ export default { ...@@ -134,13 +164,19 @@ export default {
this.isFold = !this.isFold this.isFold = !this.isFold
this.labelWidthValue = '' this.labelWidthValue = ''
this.resetLabelWidth() this.resetLabelWidth()
this.$nextTick(() => {
this.updateVisibleCount()
})
}, },
// 判断是否应该隐藏表单项
shouldHideItem(index) { shouldHideItem(index) {
if (!this.isFold) return false if (!this.isFold) return false
return index >= this.maxVisibleItemsWhenFold return index >= this.maxVisibleItemsWhenFold
}, },
// 更可靠的 el-form-item 检测方法 // 新增:更新可见表单项数量
updateVisibleCount() {
const formItems = this.extractFormItems(this.$slots.default || [])
this.visibleFormItemsCount = formItems.filter((item, index) => !this.shouldHideItem(index)).length
},
isFormItemNode(node) { isFormItemNode(node) {
if (!node) return false if (!node) return false
...@@ -163,7 +199,6 @@ export default { ...@@ -163,7 +199,6 @@ export default {
return false return false
}, },
// 递归查找所有表单项
extractFormItems(nodes, result = []) { extractFormItems(nodes, result = []) {
if (!nodes || !Array.isArray(nodes)) return result if (!nodes || !Array.isArray(nodes)) return result
...@@ -183,11 +218,12 @@ export default { ...@@ -183,11 +218,12 @@ export default {
} }
}, },
render() { render() {
// 提取所有 el-form-item
const defaultSlots = this.$slots.default || [] const defaultSlots = this.$slots.default || []
const formItems = this.extractFormItems(defaultSlots) const formItems = this.extractFormItems(defaultSlots)
// 渲染表单项 // 更新可见数量
this.visibleFormItemsCount = formItems.filter((item, index) => !this.shouldHideItem(index)).length
const formItemCols = formItems.map((item, index) => { const formItemCols = formItems.map((item, index) => {
const isHidden = this.shouldHideItem(index) const isHidden = this.shouldHideItem(index)
...@@ -195,7 +231,7 @@ export default { ...@@ -195,7 +231,7 @@ export default {
<el-col <el-col
class={{ 'hidden': isHidden }} class={{ 'hidden': isHidden }}
key={`formitem-${index}`} key={`formitem-${index}`}
xs={24} xs={this.xs}
sm={this.sm} sm={this.sm}
md={this.md} md={this.md}
lg={this.lg} lg={this.lg}
...@@ -206,7 +242,6 @@ export default { ...@@ -206,7 +242,6 @@ export default {
) )
}) })
// 渲染按钮
const buttons = ( const buttons = (
<div class="buttons-space"> <div class="buttons-space">
<el-button <el-button
...@@ -237,21 +272,23 @@ export default { ...@@ -237,21 +272,23 @@ export default {
</div> </div>
) )
// 按钮列
const buttonsCol = ( const buttonsCol = (
<el-col <el-col
class="ElColButtons" class="ElColButtons"
xs={24} xs={24}
sm={this.sm} sm={this.buttonColWidth}
md={this.md} md={this.buttonColWidth}
lg={this.lg} lg={this.buttonColWidth}
xl={this.xl} xl={this.buttonColWidth}
style={this.shouldWrapButtons ? 'margin-top: 18px;' : ''} style={this.shouldWrapButtons ? 'margin-top: 18px;' : ''}
> >
{this.$slots.buttons ? this.$slots.buttons : buttons} {this.$slots.buttons ? this.$slots.buttons : buttons}
</el-col> </el-col>
) )
// 计算按钮对齐方式
const buttonJustify = this.shouldWrapButtons ? 'flex-end' : 'flex-end'
return ( return (
<el-form <el-form
{...{ attrs: this.$attrs }} {...{ attrs: this.$attrs }}
...@@ -260,16 +297,15 @@ export default { ...@@ -260,16 +297,15 @@ export default {
ref="ElFormRef" ref="ElFormRef"
class="page-wrapper-search" class="page-wrapper-search"
> >
{/* 第一行:表单项 */} {/* 第一行:表单项和按钮(非换行情况) */}
<el-row gutter={this.gutter}> <el-row gutter={this.gutter}>
{formItemCols} {formItemCols}
{/* 折叠状态:按钮列与表单项在同一行 */}
{!this.shouldWrapButtons && buttonsCol} {!this.shouldWrapButtons && buttonsCol}
</el-row> </el-row>
{/* 展开状态:按钮列在第二行 */} {/* 第二行:换行的按钮 */}
{this.shouldWrapButtons && ( {this.shouldWrapButtons && (
<el-row gutter={this.gutter} style="margin-top: 0;display: flex;justify-content: flex-end;"> <el-row gutter={this.gutter} style={`margin-top: 18px; display: flex; justify-content: ${buttonJustify};`}>
{buttonsCol} {buttonsCol}
</el-row> </el-row>
)} )}
...@@ -284,7 +320,6 @@ export default { ...@@ -284,7 +320,6 @@ export default {
position: relative; position: relative;
background: white; background: white;
padding: 16px; padding: 16px;
} }
:deep(.el-form-item) { :deep(.el-form-item) {
...@@ -303,7 +338,7 @@ export default { ...@@ -303,7 +338,7 @@ export default {
.el-input, .el-input,
.el-select, .el-select,
.el-date-editor { .el-date-editor {
width: 100%; width: 50%;
.el-input__inner { .el-input__inner {
border-radius: 4px; border-radius: 4px;
...@@ -385,6 +420,12 @@ export default { ...@@ -385,6 +420,12 @@ export default {
justify-content: flex-end; justify-content: flex-end;
align-items: center; align-items: center;
height: 40px; height: 40px;
// 确保按钮内容右对齐
.buttons-space {
justify-content: flex-end;
width: 100%;
}
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论