vue+element 实现基本的增删改查页面

功能点:

  1. Form表单验证
  2. 重置表单
    在这里插入图片描述
    在这里插入图片描述
    功能点代码
//表单校验规则,dataRule()是form表单中需要引用的。见下方源码的form表单写法。
        computed:{
            dataRule(){
                return {
                    stationCode: [
                        {required: true, message: '车站编码不能为空'}
                    ],
                    lineCode: [
                        {required: true, message: '线路编码不能为空' }
                    ],
                    stationNameCN: [
                        {required: true, message: '中文名称不能为空' }
                    ],
                    stationNameEN: [
                        {required: true, message: '英文名称不能为空' }
                    ],
                    stationState: [
                        {required: true, message: '车站状态不能为空' }
                    ],
                    isHeadEnd: [
                        {required: true, message: '首末站不能为空' }
                    ],
                    isTransfer: [
                        {required: true, message: '换乘站不能为空' }
                    ],
                    isImportant: [
                        {required: true, message: '重点站不能为空' }
                    ],
                    turnBackFlag: [
                        {required: true, message: '折返站不能为空' }
                    ],
                    longitude: [
                        {required: true, message: '经度不能为空' }
                    ],
                    latitude: [
                        {required: true, message: '纬度不能为空' }
                    ],
                    atsLineId: [
                        {required: true, message: 'ATS线路编码不能为空' }
                    ],
                    atsStationId: [
                        {required: true, message: 'ATS车站编码不能为空' }
                    ],
                    telNum: [
                        {required: true, message: '电话号码不能为空' }
                    ],
                    shortName: [
                        {required: true, message: '简称不能为空' }
                    ],
                }
            }
        },


//一键重置表单,怎么用见下方源码
            dataFormResetHandle(formname){
                this.$refs[formname].resetFields();
            },

该页面完整源码:拿着上面的关键字直接ctrl+f搜,大家看看就明白了。

<template>
        <el-card shadow="never" class="aui-card-fill">
            <div style="margin-left: 20px;margin-right: 20px">

                <div>
                    <br>
                    <span style="font-size: 30px;">车站基本信息</span>
                </div>


                <div style="height: 60px;margin-top: 10px;font-size: 16px">



                    <el-row  >
                        <el-col :span="21">
                            <el-form :inline="true" ref="form" :model="form" label-width="80px">
                                <el-form-item >
                                    <span>开通状态</span>&nbsp;
                                </el-form-item>
                                <el-form-item>
                                    <el-radio-group v-model="form.stationStateRadio">
                                        <el-radio :label="0" >全部</el-radio>
                                        <el-radio :label="1" >已开通</el-radio>
                                        <el-radio :label="2" >未开通</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                                <el-form-item>
                                    <el-input v-model="form.searchMessage" placeholder="请输入关键字搜索"  ></el-input>
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" style="margin-left: 15px;" icon="el-icon-search"  @click="searchHandle()">搜索</el-button>
                                </el-form-item>
                            </el-form>
                        </el-col>


                        <el-col :span="3" style="text-align: right">
                            <el-button type="primary" @click="add()" icon="el-icon-plus" >新增</el-button>
                            <el-button type="danger" class="my-icon" icon="el-icon-delete"  @click="deleteStation()">删除</el-button>
                        </el-col>
                    </el-row>
                </div>


                <!--            第二行table-->
                <div style="margin-top: 10px">
                    <el-table
                            ref="multipleTable"
                            :data="tableData"
                            tooltip-effect="dark"
                            style="width: 100%;"
                            header-cell-style="background:#E3EDF7"
                            @selection-change="handleSelectionChange">
                        <el-table-column
                                type="selection"
                                width="30">
                        </el-table-column>
                        <el-table-column
                                label="序号"
                                width="52"
                                prop="id"
                                type="index">
                        </el-table-column>
                        <el-table-column
                                prop="stationCode"
                                label="站点编号"
                                width="82">
                        </el-table-column>
                        <el-table-column
                                prop="lineCode"
                                label="所属线路"
                                width="82">
                        </el-table-column>
                        <el-table-column
                                prop="stationNameCN"
                                label="中文名称"
                                width="100">
                        </el-table-column>
                        <el-table-column
                                :show-overflow-tooltip="true"
                                prop="stationNameEN"
                                label="英文名称"
                                show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                                prop="stationState"
                                label="开通状态"
                                width="82">
                        </el-table-column>
                        <el-table-column
                                prop="isHeadEnd"
                                label="首末站"
                                width="72">
                        </el-table-column>
                        <el-table-column
                                prop="isTransfer"
                                label="换乘站"
                                width="72">
                        </el-table-column>
                        <el-table-column
                                prop="isImportant"
                                label="重点站"
                                width="72">
                        </el-table-column>
                        <el-table-column
                                prop="turnBackFlag"
                                label="折返站"
                                width="72">
                        </el-table-column>
                        <el-table-column
                                prop="longitude"
                                label="经度"
                                width="95">
                        </el-table-column>
                        <el-table-column
                                prop="latitude"
                                label="纬度"
                                width="90">
                        </el-table-column>
                        <el-table-column
                                prop="updateTime"
                                label="更新日期"
                                show-overflow-tooltip>
                        </el-table-column>
                        <el-table-column
                                prop="atsLineId"
                                label="ATS线路编码"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="atsStationId"
                                label="ATS车站编码"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="telNum"
                                label="车站电话"
                                show-overflow-tooltip>
                        </el-table-column>

                        <el-table-column
                                prop="shortName"
                                label="车站简称"
                                width="140">
                        </el-table-column>

                        <el-table-column
                                label="操作"
                                width="110">
                            <template slot-scope="scope">
                                <el-button  size="mini" type="text" @click="update(scope.row.stationCode)">编辑</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <!--          分页      -->
                <div><pagination ref="paginationRef" @refreshDataList="getData"></pagination></div>

            </div>



            <!-- 弹窗添加车站-->
            <el-dialog
                    :title="insertUpdatetag=='insert' ? '新增车站信息' : '编辑车站信息'"
                    :visible.sync="dialogVisible"
                    width="50%"
                    :before-close="handleClose">

                <!--   form表单         -->
                <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="120px">

                    <el-row>
                        <el-col :span="12"><div class="grid-content bg-purple">
                            <el-form-item label="车站编码:" prop="stationCode" >
                                <el-input style="width: 95%" v-model="dataForm.stationCode" placeholder="请输入车站编码"></el-input>
                            </el-form-item>

                            <el-form-item label="中文名称:" prop="stationNameCN">
                                <el-input style="width: 95%" v-model="dataForm.stationNameCN" placeholder="请输入中文名称"></el-input>
                            </el-form-item>

                            <el-form-item label="车站电话:" prop="telNum">
                                <el-input style="width: 95%" v-model="dataForm.telNum" placeholder="请输入车站电话"></el-input>
                            </el-form-item>

                            <el-form-item label="开通状态:" prop="stationState" style="height: 18px">
                                <el-radio-group v-model="dataForm.stationState" >
                                    <el-radio :label="0">已开通</el-radio>
                                    <el-radio :label="1">未开通</el-radio>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item label="首末站:" prop="isHeadEnd" style="height: 18px">
                                <el-radio-group v-model="dataForm.isHeadEnd" >
                                    <el-radio :label="0">首末站</el-radio>
                                    <el-radio :label="1">中间站</el-radio>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item label="换乘站:" prop="isTransfer" >
                                <el-radio-group v-model="dataForm.isTransfer">
                                    <el-radio :label="0">换乘站</el-radio>
                                    <el-radio :label="1">普通站</el-radio>
                                </el-radio-group>
                            </el-form-item>

                            <el-form-item label="经度:" prop="longitude">
                                <el-input style="width: 95%" v-model="dataForm.longitude" placeholder="请输入经度"></el-input>
                            </el-form-item>

                            <el-form-item label="ATS线路编码:" prop="atsLineId">
                                <el-input style="width: 95%" v-model="dataForm.atsLineId" placeholder="请输入ATS线路编码"></el-input>
                            </el-form-item>

                        </div></el-col>


                        <el-col :span="12"><div class="grid-content bg-purple-light">
                            <el-form-item label="所属线路:" prop="lineCode">
                                <el-input style="width: 95%" v-model="dataForm.lineCode" placeholder="请输入所属线路"></el-input>
                            </el-form-item>
                            <el-form-item label="英文名称:" prop="stationNameEN" >
                                <el-input style="width: 95%" v-model="dataForm.stationNameEN" placeholder="请输入英文名称"></el-input>
                            </el-form-item>
                            <el-form-item label="简称:" prop="shortName">
                                <el-input style="width: 95%" v-model="dataForm.shortName" placeholder="请输入简称"></el-input>
                            </el-form-item>

                            <el-form-item label="重点站:" prop="isImportant" style="height: 18px">
                                <el-radio-group v-model="dataForm.isImportant">
                                    <el-radio :label="0">重点站</el-radio>
                                    <el-radio :label="1">普通站</el-radio>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item label="折返站:" prop="turnBackFlag" style="height: 18px">
                                <el-radio-group v-model="dataForm.turnBackFlag">
                                    <el-radio :label="0">折返站</el-radio>
                                    <el-radio :label="1">普通站</el-radio>
                                </el-radio-group>
                            </el-form-item>

                            <el-form-item  prop="turnBackFlag">
                                &nbsp;
                            </el-form-item>

                            <el-form-item label="纬度:" prop="latitude">
                                <el-input style="width: 95%" v-model="dataForm.latitude" placeholder="请输入纬度"></el-input>
                            </el-form-item>

                            <el-form-item label="ATS车站编码:" prop="atsStationId">
                                <el-input style="width: 95%" v-model="dataForm.atsStationId" placeholder="请输入ATS车站编码"></el-input>
                            </el-form-item>



                        </div></el-col>
                    </el-row>

                    <!--                    <el-form-item style="margin-top: 20px;margin-left: 0px">-->
                    <!--                        -->
                    <!--                    </el-form-item>-->


                </el-form>
                <el-row  style="margin-top: 15px;text-align: right">
                    <el-button type="primary"  @click="dataFormSubmitHandle('dataForm')">立即提交</el-button>
                    <el-button type="primary"   @click="dataFormResetHandle('dataForm')">重 置</el-button>

                </el-row>

            </el-dialog>

        </el-card>
</template>

<script>
    import pagination from '../programme/Pagination'

    export default {
        name: 'Station',
        components:{
            pagination
        },
        data() {
            return {
                dialogVisible: false,
                dataForm:{
                    stationCode:'',
                    lineCode:'',
                    stationNameCN:'',
                    stationNameEN:'',
                    stationState:0,
                    isHeadEnd:0,
                    isTransfer:0,
                    isImportant:0,
                    turnBackFlag:0,
                    longitude:'',
                    latitude:'',
                    atsLineId:'',
                    atsStationId:'',
                    telNum:'',
                    shortName:''
                },
                form:{
                    stationStateRadio:0,
                    searchMessage:'',
                },
                stationStateRadio:3,
                checkBoxData: [],
                InfoUnitData:[],
                unitsort:'',
                searchMessage:'',
                tableData: [],
                multipleSelection: [],
                insertUpdatetag:'insert',
                isFirstResetForm:'0',
            }
        },
        mounted(){
            this.getData();
        },
        //校验规则
        computed:{
            dataRule(){
                return {
                    stationCode: [
                        {required: true, message: '车站编码不能为空'}
                    ],
                    lineCode: [
                        {required: true, message: '线路编码不能为空' }
                    ],
                    stationNameCN: [
                        {required: true, message: '中文名称不能为空' }
                    ],
                    stationNameEN: [
                        {required: true, message: '英文名称不能为空' }
                    ],
                    stationState: [
                        {required: true, message: '车站状态不能为空' }
                    ],
                    isHeadEnd: [
                        {required: true, message: '首末站不能为空' }
                    ],
                    isTransfer: [
                        {required: true, message: '换乘站不能为空' }
                    ],
                    isImportant: [
                        {required: true, message: '重点站不能为空' }
                    ],
                    turnBackFlag: [
                        {required: true, message: '折返站不能为空' }
                    ],
                    longitude: [
                        {required: true, message: '经度不能为空' }
                    ],
                    latitude: [
                        {required: true, message: '纬度不能为空' }
                    ],
                    atsLineId: [
                        {required: true, message: 'ATS线路编码不能为空' }
                    ],
                    atsStationId: [
                        {required: true, message: 'ATS车站编码不能为空' }
                    ],
                    telNum: [
                        {required: true, message: '电话号码不能为空' }
                    ],
                    shortName: [
                        {required: true, message: '简称不能为空' }
                    ],
                }
            }
        },
        methods:{
            getData(){
                let formData=new FormData();
                this.$http.post('/station/listStation',formData,{baseURL:this.global.apis1}).then(({data:res})=>{
                    if (res.IsError =="false"){
                        this.tableData=res.Data;
                        this.$refs.paginationRef.total = res.total
                    }else{
                        return this.$message.error(res.Message)
                    }
                })
            },

            //新增按钮
            add(){
                this.insertUpdatetag='insert';
                this.dialogVisible=true;
                this.dataFormResetHandle('dataForm');
                this.isFirstResetForm='1'
            },

            //修改按钮
            update(updateStationId){
                this.insertUpdatetag='update';
                this.dialogVisible=true;
                if (this.isFirstResetForm=='1'){
                    this.dataFormResetHandle('dataForm');
                }
                this.getDataById(updateStationId);
                },

            //查找某一值
            getDataById(updateLineId){
                let formData=new FormData();
                formData.append('stationId',updateLineId);
                this.$http.post('/station/getDataById',formData,{baseURL:this.global.apis1}).then(({data:res})=>{
                    if (res.IsError =="false"){
                        this.dataForm=res.Data;
                    }else{
                        return this.$message.error(res.Message)
                    }
                })
            },

            //提交按钮
            dataFormSubmitHandle(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let formData=new FormData();
                        formData.append('stationCode',this.dataForm.stationCode);
                        formData.append('lineCode',this.dataForm.lineCode);
                        formData.append('stationNameCN',this.dataForm.stationNameCN);
                        formData.append('stationNameEN',this.dataForm.stationNameEN);
                        formData.append('stationState',this.dataForm.stationState);
                        formData.append('isHeadEnd',this.dataForm.isHeadEnd);
                        formData.append('isTransfer',this.dataForm.isTransfer);
                        formData.append('isImportant',this.dataForm.isImportant);
                        formData.append('turnBackFlag',this.dataForm.turnBackFlag);
                        formData.append('longitude',this.dataForm.longitude);
                        formData.append('latitude',this.dataForm.latitude);
                        formData.append('atsLineId',this.dataForm.atsLineId);
                        formData.append('atsStationId',this.dataForm.atsStationId);
                        formData.append('telNum',this.dataForm.telNum);
                        formData.append('shortName',this.dataForm.shortName);
                        if(this.insertUpdatetag=='insert'){
                            this.$http.post('/common/insertStation',formData,{baseURL:this.global.apis1}).then(({data:res})=>{
                                if (res.IsError =="false"){
                                    this.dialogVisible=false;
                                    this.getData();
                                    this.$message({
                                        message: '新增成功',
                                        type: 'success'
                                    });
                                }else{
                                    return this.$message.error(res.Message)
                                }
                            })
                        }else {
                            // 修改
                            this.$http.post('/common/updateStation',formData,{baseURL:this.global.apis1}).then(({data:res})=>{
                                if (res.IsError =="false"){
                                    this.dialogVisible=false;
                                    this.getData();
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });
                                }else{
                                    return this.$message.error(res.Message)
                                }
                            })
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            //重置按钮
            dataFormResetHandle(formname){
                this.$refs[formname].resetFields();
            },
            //删除项目
            deleteStation(){
                if (this.checkBoxData!=0){
                    this.$confirm('确认删除?')
                        .then(_ => {
                            let formData=new FormData();
                            formData.append('checkBoxData',this.checkBoxData);
                            this.$http.post('/common/deleteStation',formData,{baseURL:this.global.apis1}).then(({data:res})=>{
                                if (res.IsError =="false"){
                                    this.getData();

                                    this.$message({
                                        message: '删除成功',
                                        type: 'success'
                                    });
                                }else{
                                    return this.$message.error(res.Message)
                                }
                            })
                        })
                        .catch(_ => {});
                } else {
                    this.$message({
                        message: '请选择至少一条数据',
                        type: 'warning'
                    });
                }

            },
            //搜索按钮
            searchHandle(){
                let formData=new FormData();
                    this.$http.post('/common/searchStation',formData,{baseURL:this.global.apis1}).then(({data:res})=>{
                        if (res.IsError =="false"){
                            this.tableData=res.Data;
                        }else{
                            return this.$message.error(res.Message)
                        }
                    })
            },
            handleSelectionChange(val){
                this.checkBoxData = val;

            },
        }



    }
</script>

<style >

</style>

【最怕一生碌碌无为,安慰自己平凡可贵】

好的,这个Demo需要分为前端页面和后端代码两部分来实现,下面我将介绍具体的实现步骤。 ## 后端代码实现 1. 创建SpringBoot项目,引入相关依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies> ``` 2. 创建数据库表,并使用Mybatis进行数据访问操作,这里使用Druid作为数据源: ```java @Mapper public interface DemoMapper { List<Demo> getDemoList(); Demo getDemoById(Integer id); int addDemo(Demo demo); int updateDemo(Demo demo); int deleteDemo(Integer id); } ``` 3. 在Controller层编写接口,提供增删改查的功能: ```java @RestController @RequestMapping("/demo") public class DemoController { @Autowired private DemoMapper demoMapper; @GetMapping("/list") public List<Demo> getDemoList() { return demoMapper.getDemoList(); } @GetMapping("/{id}") public Demo getDemoById(@PathVariable Integer id) { return demoMapper.getDemoById(id); } @PostMapping("/add") public int addDemo(@RequestBody Demo demo) { return demoMapper.addDemo(demo); } @PutMapping("/update") public int updateDemo(@RequestBody Demo demo) { return demoMapper.updateDemo(demo); } @DeleteMapping("/{id}") public int deleteDemo(@PathVariable Integer id) { return demoMapper.deleteDemo(id); } } ``` ## 前端页面实现 1. 创建Vue项目,并引入相关依赖: ```javascript import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; ``` 2. 在页面中使用Element-Plus组件,实现增删改查的功能: ```vue <template> <div class="demo"> <el-button type="primary" @click="showAddDialog">添加</el-button> <el-table :data="tableData"> <el-table-column prop="id" label="ID"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="sex" label="性别"></el-table-column> <el-table-column label="操作"> <template #default="{ row }"> <el-button type="text" @click="showUpdateDialog(row)">编辑</el-button> <el-button type="text" @click="deleteDemo(row.id)">删除</el-button> </template> </el-table-column> </el-table> <el-dialog title="添加" :visible.sync="addDialogVisible"> <el-form :model="addForm" :rules="rules"> <el-form-item label="姓名" prop="name"> <el-input v-model="addForm.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="age"> <el-input v-model="addForm.age"></el-input> </el-form-item> <el-form-item label="性别" prop="sex"> <el-radio-group v-model="addForm.sex"> <el-radio label="男"></el-radio> <el-radio label="女"></el-radio> </el-radio-group> </el-form-item> </el-form> <div slot="footer"> <el-button @click="addDialogVisible = false">取消</el-button> <el-button type="primary" @click="addDemo">确定</el-button> </div> </el-dialog> <el-dialog title="编辑" :visible.sync="updateDialogVisible"> <el-form :model="updateForm" :rules="rules"> <el-form-item label="姓名" prop="name"> <el-input v-model="updateForm.name"></el-input> </el-form-item> <el-form-item label="年龄" prop="age"> <el-input v-model="updateForm.age"></el-input> </el-form-item> <el-form-item label="性别" prop="sex"> <el-radio-group v-model="updateForm.sex"> <el-radio label="男"></el-radio> <el-radio label="女"></el-radio> </el-radio-group> </el-form-item> </el-form> <div slot="footer"> <el-button @click="updateDialogVisible = false">取消</el-button> <el-button type="primary" @click="updateDemo">确定</el-button> </div> </el-dialog> </div> </template> <script> import { reactive, toRefs } from 'vue'; import axios from 'axios'; export default { name: 'Demo', setup() { const state = reactive({ tableData: [], addDialogVisible: false, updateDialogVisible: false, addForm: { name: '', age: '', sex: '男' }, updateForm: { id: '', name: '', age: '', sex: '' }, rules: { name: [ { required: true, message: '请输入姓名', trigger: 'blur' } ], age: [ { required: true, message: '请输入年龄', trigger: 'blur' } ], sex: [ { required: true, message: '请选择性别', trigger: 'change' } ] } }); const getDemoList = () => { axios.get('/demo/list').then(res => { state.tableData = res.data; }); }; const addDemo = () => { axios.post('/demo/add', state.addForm).then(() => { state.addDialogVisible = false; state.addForm = { name: '', age: '', sex: '男' }; getDemoList(); }); }; const updateDemo = () => { axios.put('/demo/update', state.updateForm).then(() => { state.updateDialogVisible = false; state.updateForm = { id: '', name: '', age: '', sex: '' }; getDemoList(); }); }; const deleteDemo = id => { axios.delete(`/demo/${id}`).then(() => { getDemoList(); }); }; const showAddDialog = () => { state.addDialogVisible = true; }; const showUpdateDialog = row => { state.updateDialogVisible = true; state.updateForm = Object.assign({}, row); }; getDemoList(); return { ...toRefs(state), getDemoList, addDemo, updateDemo, deleteDemo, showAddDialog, showUpdateDialog }; } }; </script> ``` 这样就完成了一个简单的增删改查Demo,前后端分离,前端使用VueElement-Plus实现
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值