table.vue.tpl 11.6 KB
Newer Older
1 2
<template>
  <div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
3
    <div class="gva-search-box">
4
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
5
           {{- range .Fields}}  {{- if .FieldSearchType}} {{- if eq .FieldType "bool" }}
6
            <el-form-item label="{{.FieldDesc}}" prop="{{.FieldJson}}">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
            <el-select v-model="searchInfo.{{.FieldJson}}" clearable placeholder="请选择">
8 9 10 11 12 13 14 15 16 17 18 19 20
                <el-option
                    key="true"
                    label="是"
                    value="true">
                </el-option>
                <el-option
                    key="false"
                    label="否"
                    value="false">
                </el-option>
            </el-select>
            </el-form-item>
                  {{- else }}
21
        <el-form-item label="{{.FieldDesc}}">
22 23
          <el-input v-model="searchInfo.{{.FieldJson}}" placeholder="搜索条件" />
        </el-form-item>{{ end }}{{ end }}{{ end }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24
        <el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
25 26
          <el-button size="small" type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button size="small" icon="refresh" @click="onReset">重置</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
27
        </el-form-item>
28 29
      </el-form>
    </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
30 31
    <div class="gva-table-box">
        <div class="gva-btn-list">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
32
            <el-button size="small" type="primary" icon="plus" @click="openDialog">新增</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33 34
            <el-popover v-model:visible="deleteVisible" placement="top" width="160">
            <p>确定要删除吗?</p>
35
            <div style="text-align: right; margin-top: 8px;">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36 37
                <el-button size="small" type="text" @click="deleteVisible = false">取消</el-button>
                <el-button size="small" type="primary" @click="onDelete">确定</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38 39
            </div>
            <template #reference>
40
                <el-button icon="delete" size="small" style="margin-left: 10px;" :disabled="!multipleSelection.length" @click="deleteVisible = true">删除</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
41 42 43 44 45 46 47 48
            </template>
            </el-popover>
        </div>
        <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
49
        row-key="ID"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
50 51 52
        @selection-change="handleSelectionChange"
        >
        <el-table-column type="selection" width="55" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
53
        <el-table-column align="left" label="日期" width="180">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
54 55 56 57
            <template #default="scope">{{ "{{ formatDate(scope.row.CreatedAt) }}" }}</template>
        </el-table-column>
        {{- range .Fields}}
        {{- if .DictType}}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
58
        <el-table-column align="left" label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
            <template #default="scope">
P
piexlmax 已提交
60
            {{"{{"}} filterDict(scope.row.{{.FieldJson}},{{.DictType}}Options) {{"}}"}}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
61 62 63
            </template>
        </el-table-column>
        {{- else if eq .FieldType "bool" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
64
        <el-table-column align="left" label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
65 66
            <template #default="scope">{{"{{"}} formatBoolean(scope.row.{{.FieldJson}}) {{"}}"}}</template>
        </el-table-column> {{- else }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
67
        <el-table-column align="left" label="{{.FieldDesc}}" prop="{{.FieldJson}}" width="120" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
68 69
        {{- end }}
        {{- end }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
70
        <el-table-column align="left" label="按钮组">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
71
            <template #default="scope">
P
piexlmax 已提交
72
            <el-button type="text" icon="edit" size="small" class="table-button" @click="update{{.StructName}}Func(scope.row)">变更</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
73
            <el-button type="text" icon="delete" size="small" @click="deleteRow(scope.row)">删除</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
            </template>
        </el-table-column>
        </el-table>
        <div class="gva-pagination">
            <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            />
        </div>
    </div>
89
    <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" title="弹窗操作">
90 91
      <el-form :model="formData" label-position="right" label-width="80px">
    {{- range .Fields}}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
92
        <el-form-item label="{{.FieldDesc}}:">
93
      {{- if eq .FieldType "bool" }}
94
          <el-switch v-model="formData.{{.FieldJson}}" active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否" clearable ></el-switch>
95 96
      {{- end }}
      {{- if eq .FieldType "string" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
97
          <el-input v-model="formData.{{.FieldJson}}" clearable placeholder="请输入" />
98 99
      {{- end }}
      {{- if eq .FieldType "int" }}
100
      {{- if .DictType}}
P
piexlmax 已提交
101
          <el-select v-model="formData.{{ .FieldJson }}" placeholder="请选择" style="width:100%" clearable>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
102 103
            <el-option v-for="(item,key) in {{ .DictType }}Options" :key="key" :label="item.label" :value="item.value" />
          </el-select>
104
      {{- else }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
105
          <el-input v-model.number="formData.{{ .FieldJson }}" clearable placeholder="请输入" />
106 107 108
      {{- end }}
      {{- end }}
      {{- if eq .FieldType "time.Time" }}
P
piexlmax 已提交
109
          <el-date-picker v-model="formData.{{ .FieldJson }}" type="date" style="width:100%" placeholder="选择日期" clearable />
110 111
      {{- end }}
      {{- if eq .FieldType "float64" }}
P
piexlmax 已提交
112
          <el-input-number v-model="formData.{{ .FieldJson }}"  style="width:100%" :precision="2" clearable />
113 114 115 116
      {{- end }}
        </el-form-item>
      {{- end }}
      </el-form>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
117 118
      <template #footer>
        <div class="dialog-footer">
P
piexlmax 已提交
119 120
          <el-button size="small" @click="closeDialog">取 消</el-button>
          <el-button size="small" type="primary" @click="enterDialog">确 定</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
121 122
        </div>
      </template>
123 124 125 126 127
    </el-dialog>
  </div>
</template>

<script>
P
piexlmax 已提交
128 129 130 131 132 133
export default {
  name: '{{.StructName}}'
}
</script>

<script setup>
134
import {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
135 136 137 138 139 140
  create{{.StructName}},
  delete{{.StructName}},
  delete{{.StructName}}ByIds,
  update{{.StructName}},
  find{{.StructName}},
  get{{.StructName}}List
P
piexlmax 已提交
141 142
} from '@/api/{{.PackageName}}'

P
piexlmax 已提交
143
// 全量引入格式化工具 请按需保留
P
piexlmax 已提交
144 145 146 147
import { getDictFunc, formatDate, formatBoolean, filterDict } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'

P
piexlmax 已提交
148
// 自动化生成的字典(可能为空)以及字段
P
piexlmax 已提交
149 150 151 152 153 154
    {{- range $index, $element := .DictTypes}}
const {{ $element }}Options = ref([])
    {{- end }}
const formData = ref({
        {{- range .Fields}}
        {{- if eq .FieldType "bool" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
155
        {{.FieldJson}}: false,
P
piexlmax 已提交
156 157
        {{- end }}
        {{- if eq .FieldType "string" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
158
        {{.FieldJson}}: '',
P
piexlmax 已提交
159 160 161 162 163
        {{- end }}
        {{- if eq .FieldType "int" }}
        {{.FieldJson}}: {{- if .DictType }} undefined{{ else }} 0{{- end }},
        {{- end }}
        {{- if eq .FieldType "time.Time" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
164
        {{.FieldJson}}: new Date(),
P
piexlmax 已提交
165 166
        {{- end }}
        {{- if eq .FieldType "float64" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
167
        {{.FieldJson}}: 0,
P
piexlmax 已提交
168 169 170 171
        {{- end }}
        {{- end }}
        })

P
piexlmax 已提交
172
// =========== 表格控制部分 ===========
P
piexlmax 已提交
173 174 175 176 177 178
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

P
piexlmax 已提交
179
// 重置
P
piexlmax 已提交
180 181 182 183
const onReset = () => {
  searchInfo.value = {}
}

P
piexlmax 已提交
184
// 搜索
P
piexlmax 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
{{- range .Fields}}{{- if eq .FieldType "bool" }}
  if (searchInfo.value.{{.FieldJson}} === ""){
      searchInfo.value.{{.FieldJson}}=null
  }{{ end }}{{ end }}
  getTableData()
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

P
piexlmax 已提交
201
// 修改页面容量
P
piexlmax 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await get{{.StructName}}List({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

P
piexlmax 已提交
220 221 222
// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
P
piexlmax 已提交
223
const setOptions = async () =>{
Mr.奇淼('s avatar
Mr.奇淼( 已提交
224
{{- range $index, $element := .DictTypes }}
P
piexlmax 已提交
225
    {{ $element }}Options.value = await getDictFunc('{{$element}}')
226
{{- end }}
P
piexlmax 已提交
227 228
}

P
piexlmax 已提交
229
// 获取需要的字典 可能为空 按需保留
P
piexlmax 已提交
230 231
setOptions()

P
piexlmax 已提交
232 233 234 235

// 多选数据
const multipleSelection = ref([])
// 多选
P
piexlmax 已提交
236 237 238 239
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}

P
piexlmax 已提交
240
// 删除行
P
piexlmax 已提交
241 242
const deleteRow = (row) => {
    ElMessageBox.confirm('确定要删除吗?', '提示', {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
243 244 245
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
P
piexlmax 已提交
246 247 248 249
    }).then(() => {
            delete{{.StructName}}Func(row)
        })
    }
P
piexlmax 已提交
250 251 252 253 254 255 256


// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async() => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
257
      const ids = []
P
piexlmax 已提交
258 259
      if (multipleSelection.value.length === 0) {
        ElMessage({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
260 261 262 263 264
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
P
piexlmax 已提交
265 266
      multipleSelection.value &&
        multipleSelection.value.map(item => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
267 268 269 270
          ids.push(item.ID)
        })
      const res = await delete{{.StructName}}ByIds({ ids })
      if (res.code === 0) {
P
piexlmax 已提交
271
        ElMessage({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
272 273 274
          type: 'success',
          message: '删除成功'
        })
P
piexlmax 已提交
275 276
        if (tableData.value.length === ids.length && page.value > 1) {
          page.value--
277
        }
P
piexlmax 已提交
278 279
        deleteVisible.value = false
        getTableData()
280
      }
P
piexlmax 已提交
281
    }
P
piexlmax 已提交
282 283 284 285 286

// 行为控制标记(弹窗内部需要增还是改)
const type = ref('')

// 更新行
P
piexlmax 已提交
287 288 289 290 291 292 293 294
const update{{.StructName}}Func = async(row) => {
    const res = await find{{.StructName}}({ ID: row.ID })
    type.value = 'update'
    if (res.code === 0) {
        formData.value = res.data.re{{.Abbreviation}}
        dialogFormVisible.value = true
    }
}
P
piexlmax 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321


// 删除行
const delete{{.StructName}}Func = async (row) => {
    const res = await delete{{.StructName}}({ ID: row.ID })
    if (res.code === 0) {
        ElMessage({
                type: 'success',
                message: '删除成功'
            })
            if (tableData.value.length === 1 && page.value > 1) {
            page.value--
        }
        getTableData()
    }
}

// 弹窗控制标记
const dialogFormVisible = ref(false)

// 打开弹窗
const openDialog = () => {
    type.value = 'create'
    dialogFormVisible.value = true
}

// 关闭弹窗
P
piexlmax 已提交
322 323 324 325
const closeDialog = () => {
    dialogFormVisible.value = false
    formData.value = {
    {{- range .Fields}}
326
        {{- if eq .FieldType "bool" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
327
        {{.FieldJson}}: false,
328 329
        {{- end }}
        {{- if eq .FieldType "string" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
330
        {{.FieldJson}}: '',
331 332
        {{- end }}
        {{- if eq .FieldType "int" }}
333
        {{.FieldJson}}: {{- if .DictType }} undefined{{ else }} 0{{- end }},
334 335
        {{- end }}
        {{- if eq .FieldType "time.Time" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
336
        {{.FieldJson}}: new Date(),
337 338
        {{- end }}
        {{- if eq .FieldType "float64" }}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
339
        {{.FieldJson}}: 0,
340
        {{- end }}
P
piexlmax 已提交
341
        {{- end }}
342
        }
P
piexlmax 已提交
343
}
P
piexlmax 已提交
344
// 弹窗确定
P
piexlmax 已提交
345
const enterDialog = async () => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
346
      let res
P
piexlmax 已提交
347
      switch (type.value) {
348
        case 'create':
P
piexlmax 已提交
349
          res = await create{{.StructName}}(formData.value)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
350
          break
351
        case 'update':
P
piexlmax 已提交
352
          res = await update{{.StructName}}(formData.value)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
353
          break
354
        default:
P
piexlmax 已提交
355
          res = await create{{.StructName}}(formData.value)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
356
          break
357
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
358
      if (res.code === 0) {
P
piexlmax 已提交
359
        ElMessage({
Mr.奇淼('s avatar
Mr.奇淼( 已提交
360 361
          type: 'success',
          message: '创建/更改成功'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
362
        })
P
piexlmax 已提交
363 364
        closeDialog()
        getTableData()
365
      }
P
piexlmax 已提交
366
}
367 368 369
</script>

<style>
Z
zzZ007 已提交
370
</style>