Skip to content

vue admin CRUD模板

lxy edited this page Jul 6, 2021 · 1 revision
  • 模板名称:vue
  • 文件名称:${context.javaBeanNameLF}.vue
  • 模板内容:
#set($jq="$")
<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline" size="mini">
      #foreach($column in $columns)
      	#if(!${column.isPk})
        <el-form-item
      	#if( "${column.comment}" != "" )
        label="${column.comment}"
        #else
        label="${column.javaFieldName}"
        #end
      >
      	<el-input v-model="searchFormData.${column.javaFieldName}" :clearable="true" placeholder="${column.comment}" style="width: 250px;" />
      </el-form-item>
        #end
      #end
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="loadTable">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" icon="el-icon-plus" style="margin-bottom: 10px;" @click="onAdd">新增</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
    #foreach($column in $columns)
      <el-table-column
        prop="${column.javaFieldName}"
        #if( "${column.comment}" != "" )
        label="${column.comment}"
        #else
        label="${column.javaFieldName}"
        #end
      />
     #end
     <el-table-column>
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onTableUpdate(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="onTableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.pageIndex"
      :page-size="searchFormData.pageSize"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @close="resetForm('dialogForm')"
    >
      <el-form
        ref="dialogForm"
        :rules="dialogFormRules"
        :model="dialogFormData"
        label-width="120px"
        size="mini"
      >
      #foreach($column in $columns)
      	#if(!${column.isPk})
        <el-form-item
      	prop="${column.javaFieldName}"
        #if( "${column.comment}" != "" )
        label="${column.comment}"
        #else
        label="${column.javaFieldName}"
        #end
       >
          <el-input v-model="dialogFormData.${column.javaFieldName}" />
        </el-form-item>
        #end      
      #end
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onDialogSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
// 创建axios实例
const client = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
  timeout: 60000 // 请求超时时间,60秒
})
export default {
  data() {    
    return {
      searchFormData: {
        #foreach($column in $columns)
        ${column.javaFieldName}: '',
        #end
        pageIndex: 1,
        pageSize: 10
      },
      pageInfo: {
        rows: [],
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '',
      dialogFormData: {
        #foreach($column in $columns)
        #if($velocityCount > 1),#end ${column.javaFieldName}: ''
        #end
      },
      dialogFormRules: {        
        #foreach($column in $columns)
        #if($velocityCount > 1),#end ${column.javaFieldName}: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
        #end
      }
    }
  },
  created() {
    this.loadTable()
  },
  methods: {
    loadTable() {
      this.post('/${context.javaBeanNameLF}/list', this.searchFormData, resp => {
        this.pageInfo.rows = resp.data
      })
    },
    onTableUpdate(row) {
      this.dialogTitle = '修改'
      this.dialogVisible = true
      this.${jq}nextTick(() => {
        Object.assign(this.dialogFormData, row)
      })
    },
    onTableDelete(row) {
      this.confirm(`确认要删除该记录吗?`, function(done) {
        const data = {
          ${pk.javaFieldName}: row.${pk.javaFieldName}
        }
        this.post('/${context.javaBeanNameLF}/del', data, () => {
          done()
          this.tip('删除成功')
          this.loadTable()
        })
      })
    },
    onDialogSave() {
      this.${jq}refs.dialogForm.validate((valid) => {
        if (valid) {
          const uri = this.dialogFormData.${pk.javaFieldName} ? '/${context.javaBeanNameLF}/update' : '/${context.javaBeanNameLF}/add'
          this.post(uri, this.dialogFormData, () => {
            this.dialogVisible = false
            this.loadTable()
          })
        }
      })
    },
    onSizeChange(size) {
      this.searchFormData.pageSize = size
      this.loadTable()
    },
    onAdd() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
      this.dialogFormData.${pk.javaFieldName} = 0
    },
    onPageIndexChange(pageIndex) {
      this.searchFormData.pageIndex = pageIndex
      this.loadTable()
    },
    /**
     * 请求接口
     * @param uri uri
     * @param data 请求数据
     * @param callback 成功时回调
     * @param errorCallback 错误时回调
     */
    post(uri, data, callback, errorCallback) {
      const that = this
      client.post(uri, data).then(function(response) {
        const resp = response.data
        const code = resp.code
        if (code === '0') { // 成功
          callback && callback.call(that, resp)
        } else {
          that.${jq}message.error(resp.msg)
        }
      }).catch(function(error) {
        console.error(error)
        errorCallback && errorCallback(error)
        that.${jq}message.error(error.message)
      })
    }
  }
}
</script>

  • 效果图

CURD模板效果

Clone this wiki locally