下面我将详细讲解“el-form每行显示两列底部按钮居中效果的实现”的完整攻略。
1. 使用 element-ui 中的 el-form 搭建表单
在使用 element-ui 的 el-form 前,需要先引入 element-ui 库和样式文件,并在 Vue 中进行配置。在添加表单元素时,可以利用 el-col、el-row 以及 span 属性来设置每行的列数和每列元素所占的宽度比例。例如:
<template>
<el-form ref="form" :model="formData" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="用户名" prop="username">
<el-input v-model="formData.username"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="formData.password"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="邮箱" prop="email">
<el-input v-model="formData.email"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="性别" prop="gender">
<el-radio-group v-model="formData.gender">
<el-radio :label="1">男</el-radio>
<el-radio :label="2">女</el-radio>
</el-radio-group>
</el-form-item>
</el-col>
</el-row>
</el-form>
</template>
2. 在表单底部添加两个按钮
可以利用 el-form 的 slot 属性来自定义表单中的内容。在此我们可以使用 slot="footer" 在表单底部添加两个居中显示的按钮。例如:
<template>
<el-form ref="form" :model="formData" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="用户名" prop="username">
<el-input v-model="formData.username"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="formData.password"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="12">
<el-form-item label="邮箱" prop="email">
<el-input v-model="formData.email"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="性别" prop="gender">
<el-radio-group v-model="formData.gender">
<el-radio :label="1">男</el-radio>
<el-radio :label="2">女</el-radio>
</el-radio-group>
</el-form-item>
</el-col>
</el-row>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="submitForm">提交</el-button>
</div>
</el-form>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const dialogVisible = ref(false);
const formData = ref({
username: '',
password: '',
email: '',
gender: '',
});
const submitForm = () => {
this.$refs.form.validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
};
return {
dialogVisible,
formData,
submitForm,
};
},
};
</script>
3. CSS 样式调整
为了实现按钮的居中效果,我们需要对底部的 div 标签添加一些样式设置。例如:
.dialog-footer {
text-align: center;
margin-top: 20px;
padding: 10px 0;
border-top: 1px solid #ddd;
}
这样就可以实现 el-form 每行显示两列底部按钮居中效果的实现。
另外还有一种方法是利用 flex 布局来实现。具体方法可以参考下面的示例代码:
<template>
<el-form ref="form" :model="formData" label-width="100px">
<el-row class="form-row">
<el-col :span="12">
<el-form-item label="用户名" prop="username">
<el-input v-model="formData.username"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="formData.password"></el-input>
</el-form-item>
</el-col>
</el-row>
<el-row class="form-row">
<el-col :span="12">
<el-form-item label="邮箱" prop="email">
<el-input v-model="formData.email"></el-input>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="性别" prop="gender">
<el-radio-group v-model="formData.gender">
<el-radio :label="1">男</el-radio>
<el-radio :label="2">女</el-radio>
</el-radio-group>
</el-form-item>
</el-col>
</el-row>
<div class="form-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="submitForm">提交</el-button>
</div>
</el-form>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const dialogVisible = ref(false);
const formData = ref({
username: '',
password: '',
email: '',
gender: '',
});
const submitForm = () => {
this.$refs.form.validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
};
return {
dialogVisible,
formData,
submitForm,
};
},
};
</script>
<style>
.form-row {
display: flex;
justify-content: space-between;
align-items: center;
}
.form-row el-col {
width: 48%;
}
.form-footer {
display: flex;
justify-content: center;
align-items: center;
margin-top: 30px;
}
.form-footer el-button {
margin: 0 10px;
}
</style>
以上两种方法都可以实现表单每行显示两列以及底部按钮居中的效果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:el-form每行显示两列底部按钮居中效果的实现 - Python技术站