Newer
Older
vue3-front / src / views / system / dict / list.dict.vue
Stephanie on 24 Nov 2022 3 KB first commit
<script lang="ts" setup name="SystemDictList">
import type { Ref } from 'vue'
import { computed, getCurrentInstance, reactive, ref } from 'vue'
import { ElButton, ElInput, ElMessage, ElMessageBox, ElOption, ElSelect, ElTable, ElTableColumn } from 'element-plus'
import { Plus, Switch } from '@element-plus/icons-vue'
import type { DictListInfo } from './dict_interface'
import SystemEditDict from './edit.dict.vue'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { delDict, getDictList } from '@/api/system/dict'
import { toTreeList } from '@/utils/structure'

const { proxy } = getCurrentInstance() as any

const editDialog = ref() // 组件

// 默认查询条件
const defaultQuery = {
  condition: '',
  offset: 1,
  limit: 10,
  sort: '',
  order: '',
}
const total = ref(0)
// 查询条件
const listQuery = reactive({ ...defaultQuery })
// 搜索重置
function reset() {
  Object.assign(listQuery, defaultQuery)
  fetchData()
}
// 表格表头
const columns: Ref<TableColumn[]> = ref([])
columns.value = [
  { text: '字典名称', value: 'name', width: 180, align: 'center' },
  { text: '字典编码', value: 'code', width: 130, align: 'center' },
  { text: '字典描述', value: 'tips', width: 180, align: 'center' },
  { text: '字典详情', value: 'detail', align: 'center' },
  { text: '字典排序', value: 'num', width: 80, align: 'center' },
]
// 数据列表
const list: Ref<DictListInfo[]> = ref([])
const loading = ref(false)

// 搜索按钮
function search() {
  fetchData()
}
// 查询数据
function fetchData() {
  loading.value = true
  getDictList(listQuery).then((res: { data: { rows: []; total: number } }) => {
    list.value = res.data.rows
    total.value = res.data.total
    loading.value = false
  })
}

// 编辑资源
function edit(row: DictListInfo) {
  editDialog.value.initDialog('update', row)
}
// 删除资源
function del(row: DictListInfo) {
  ElMessageBox.confirm(
    `确定要删除${row.name}吗?`,
    '确认删除',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    delDict(row.id).then(() => {
      ElMessage.success('删除成功')
    })
    fetchData()
  })
}
// 添加资源
function add() {
  editDialog.value.initDialog('create')
}

onMounted(() => {
  fetchData()
})
</script>

<template>
  <AppContainer>
    <SearchArea @search="search">
      <SearchItem>
        <ElInput v-model="listQuery.condition" placeholder="字典名称" />
      </SearchItem>
    </SearchArea>
    <TableContainer>
      <template #btns-left>
        <ElButton v-if="proxy.hasPerm('/sys/dict/add')" :icon="Plus" style="margin-bottom: 10px;" @click="add">
          新增
        </ElButton>
      </template>
      <NormalTable :query="listQuery" :loading="loading" :columns="columns" :data="list" :total="total" :border="false">
        <template #columns>
          <ElTableColumn v-if="proxy.hasPerm('/sys/dict/update') || proxy.hasPerm('/sys/dict/delete')" label="操作" width="140" align="center">
            <template #default="scope">
              <ElButton v-if="proxy.hasPerm('/sys/dict/update')" type="primary" text size="small" class="table-text-button" @click="edit(scope.row)">
                修改
              </ElButton>
              <ElButton v-if="proxy.hasPerm('/sys/dict/delete')" type="primary" text size="small" class="table-text-button" @click="del(scope.row)">
                删除
              </ElButton>
            </template>
          </ElTableColumn>
        </template>
      </NormalTable>
    </TableContainer>
    <SystemEditDict ref="editDialog" @close-refresh="search" />
  </appcontainer>
</template>

<style lang="scss" scoped>
.table-text-button {
  margin-left: 0;
}
</style>