Newer
Older
sensorHubPlusFront / src / views / system / dict / listDict.vue
liyaguang 8 days ago 4 KB 页面搭建
<script lang="ts" setup name="SystemDictList">
import type { Ref } from 'vue'
import { getCurrentInstance, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { DictListInfo } from './dict-interface'
import SystemEditDict from './editDict.vue'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { delDict, getDictList } from '@/api/system/dict'

const { proxy } = getCurrentInstance() as any

const editDialog = ref() // 组件

// 默认查询条件
const defaultQuery = {
  condition: '',
  offset: 1,
  limit: 20,
  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: 180, align: 'center' },
  { text: '字典描述', value: 'tips', width: 180, align: 'center' },
  { text: '字典详情', value: 'detail', 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
    calcTableHeight()
  })
}

// 编辑资源
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')
}

// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
function changePage(val: { size: number; page: number }) {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}

onMounted(() => {
  fetchData()
  window.addEventListener('resize', calcTableHeight)
})
const tableHeight = ref(400)
const calcTableHeight = () => {
  // 顶部高度
  const topHeight = 50
  // app-container的 padding距离
  const appPadding = 20
  // 查询组件的高度
  const searchDiv = document.getElementById('search-div-id')
  const searchHeight = searchDiv ? searchDiv.clientHeight : 0
  // 表格顶部的文字提示高度
  const tableTopHeight = 32 + 10
  // 表格表头
  const tableHeaderHeight = 40
  // 分页器的高度
  const tablePaginationHeight = 40
  // 判断数据长度
  const height = window.innerHeight - topHeight - appPadding - searchHeight - tableTopHeight - tableHeaderHeight - tablePaginationHeight
  if (list.value.length * 40 >= height) {
    tableHeight.value = height
  }
  else {
    tableHeight.value = list.value.length ? (list.value.length + 1) * 40 : 200
  }
}
onBeforeUnmount(() => {
  window.removeEventListener('resize', calcTableHeight)
})
</script>

<template>
  <app-container>
    <search-area @search="search">
      <search-item>
        <el-input v-model="listQuery.condition" placeholder="字典名称" />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button v-if="proxy.hasPerm('/sys/dict/add')" icon="icon-add" title="新增" @click="add" />
      </template>
      <normal-table :query="listQuery" :list-loading="loading" :columns="columns" :data="list" :total="total" :border="false" @change="changePage" :height="tableHeight">
        <template #columns>
          <el-table-column v-if="proxy.hasPerm('/sys/dict/update') || proxy.hasPerm('/sys/dict/delete')" label="操作" width="140" align="center">
            <template #default="scope">
              <el-button v-if="proxy.hasPerm('/sys/dict/update')" type="primary" link size="small" class="table-text-button" @click="edit(scope.row)">
                修改
              </el-button>
              <el-button v-if="proxy.hasPerm('/sys/dict/delete')" type="primary" link size="small" class="table-text-button" @click="del(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <system-edit-dict ref="editDialog" @close-refresh="search" />
  </app-container>
</template>