1. 程式人生 > 其它 >vue-element-admin 二次開發

vue-element-admin 二次開發

vue後臺開發

新增路由

{
  path: '/building-type',
  component: Layout,
  name: 'buildingType',
  children: [
    {
      path: 'index',
      component: () => import('@/views/building-type/index'),
      name: 'BuildingTypeList',
      meta: { title: '樓型管理', icon: 'el-icon-s-shop' }
    }
  ]
}

新增介面地址

//樓型管理
export function getBuildingTypeList(data) {
  return request({
    url: '/VillageBuildingType/getBuildingTypeList',
    method: 'post',
    data
  })
}
export function addEditBuildingType(data) {
  return request({
    url: '/VillageBuildingType/addEdit',
    method: 'post',
    data
  })
}
export function deleteBuildingType(data) {
  return request({
    url: '/VillageBuildingType/delete',
    method: 'post',
    data
  })
}

新增頁面

<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" @click="handleCreate">新增樓型</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" prop="id"  align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="樓型名稱"  align="center" min-width="180">
        <template slot-scope="{row}">
           <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="新增時間"  align="center" min-width="280px">
        <template slot-scope="{row}">
           <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改時間"  align="center" min-width="280px">
        <template slot-scope="{row}">
           <span>{{ row.update_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="200" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">編輯</el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">刪除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" center>
     <el-form ref="dataForm" :model="dataForm" :rules="rules" label-position="left" label-width="130px" style="width: 450px;">
        <el-form-item label="房型名稱"  prop="name">
          <el-input v-model="dataForm.name"  placeholder="請輸入房型名稱"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="createEditData();">確認</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  // 引入介面
  import { getBuildingTypeList,addEditBuildingType,deleteBuildingType } from '@/api/village-api'
  export default {
    name: 'BuildingTypeList',
    data() {
      return {
        listLoading:true,
        tableKey:0,
        list:[],
        dialogStatus: '',
        textMap: {
          update: '編輯樓型',
          create: '新增樓型'
        },
        dialogFormVisible:false,
        // 正則驗證
        rules:{
          name:[{ required: true, message: '請輸入樓型名稱' , trigger: 'blur' }],
        },
        // 初始化值
        dataForm:{
          id:undefined,
          name:undefined
        }
      }
    },
    created() {
      // 獲取列表
      this.getBuildingTypeList();
    },
    methods: {
      getBuildingTypeList(){
          this.listLoading = true
          getBuildingTypeList().then(({ data }) => {
            this.list = data;
            // Just to simulate the time of the request
            setTimeout(() => {
              this.listLoading = false
            }, 1.5 * 1000)
          })
      },
      resetForm(){
        this.dataForm = {
          id:undefined,
          name:undefined
        }
      },
      handleCreate(){
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createEditData(){
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 驗證通過後,呼叫新增修改介面
            addEditBuildingType(this.dataForm).then((res) => {
              this.getBuildingTypeList();
              this.dialogFormVisible = false
              if(this.dialogStatus == 'create'){
                this.$notify({
                  title: '成功',
                  message: '新增成功',
                  type: 'success',
                  duration: 2000
                })
              }else{
                 this.$message.success('編輯成功')
              }

            })
          }
        })
      },
      handleUpdate(row){
        this.resetForm()
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.dataForm = Object.assign({}, row);
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      handleDelete(row,index){
        let _this = this;
        // 確認
        this.$confirm('確認刪除樓型, 是否繼續?','提示',{
          confirmButtonText: '確定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          // 呼叫刪除介面
          deleteBuildingType({id:row.id}).then(()=>{
            _this.list.splice(index,1)
            this.$message.success('刪除成功')
          })
        })
      }
    },
    watch: {

    },
  }
</script>

<style scoped lang=''>

</style>