view_list.hbs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script lang="ts">
  2. export default { name: '{{pascalCase name}}List' };
  3. </script>
  4. <script setup lang="ts">
  5. import { computed, onMounted, ref } from 'vue';
  6. import { ElMessage } from 'element-plus';
  7. import { Plus, Delete } from '@element-plus/icons-vue';
  8. import { useI18n } from 'vue-i18n';
  9. import { perm } from '@/store/useCurrentUser';
  10. import { moveList, toParams, resetParams } from '@/utils/common';
  11. import { delete{{pascalCase name}}, query{{pascalCase name}}List, update{{pascalCase name}}Order } from '@/api/{{kebabCase path}}';
  12. import { ColumnList, ColumnSetting } from '@/components/TableList';
  13. import { QueryForm, QueryItem } from '@/components/QueryForm';
  14. import ListMove from '@/components/ListMove.vue';
  15. import {{pascalCase name}}Form from './{{pascalCase name}}Form.vue';
  16. const { t } = useI18n();
  17. const params = ref<Record<string, any>>({});
  18. const sort = ref();
  19. const table = ref();
  20. const data = ref<any[]>([]);
  21. const selection = ref<any[]>([]);
  22. const loading = ref<boolean>(false);
  23. const formVisible = ref<boolean>(false);
  24. const beanId = ref<number>();
  25. const beanIds = computed(() => data.value.map((row) => row.id));
  26. const filtered = ref<boolean>(false);
  27. const fetchData = async () => {
  28. loading.value = true;
  29. try {
  30. data.value = await query{{pascalCase name}}List({ ...toParams(params.value), Q_OrderBy: sort.value });
  31. filtered.value = Object.values(params.value).filter((v) => v !== undefined && v !== '').length > 0 || sort.value !== undefined;
  32. } finally {
  33. loading.value = false;
  34. }
  35. };
  36. onMounted(fetchData);
  37. const handleSort = ({ column, prop, order }: { column: any; prop: string; order: string }) => {
  38. if (prop) {
  39. sort.value = (column.sortBy ?? prop) + (order === 'descending' ? '_desc' : '');
  40. } else {
  41. sort.value = undefined;
  42. }
  43. fetchData();
  44. };
  45. const handleSearch = () => fetchData();
  46. const handleReset = () => {
  47. table.value.clearSort();
  48. resetParams(params.value);
  49. sort.value = undefined;
  50. fetchData();
  51. };
  52. const handleAdd = () => {
  53. beanId.value = undefined;
  54. formVisible.value = true;
  55. };
  56. const handleEdit = (id: number) => {
  57. beanId.value = id;
  58. formVisible.value = true;
  59. };
  60. const handleDelete = async (ids: number[]) => {
  61. await delete{{pascalCase name}}(ids);
  62. fetchData();
  63. ElMessage.success(t('success'));
  64. };
  65. const move = async (selected: any[], type: 'top' | 'up' | 'down' | 'bottom') => {
  66. const list = moveList(selected, data.value, type);
  67. await update{{pascalCase name}}Order(list.map((item) => item.id));
  68. };
  69. </script>
  70. <template>
  71. <div>
  72. <div class="mb-3">
  73. <query-form :params="params" @search="handleSearch" @reset="() => handleReset()">
  74. <query-item :label="$t('{{camelCase name}}.name')" name="Q_Contains_name"></query-item>
  75. </query-form>
  76. </div>
  77. <div class="space-x-2">
  78. <el-button type="primary" :disabled="perm('{{camelCase name}}:create')" :icon="Plus" @click="() => handleAdd()">\{{ $t('add') }}</el-button>
  79. <el-popconfirm :title="$t('confirmDelete')" @confirm="() => handleDelete(selection.map((row) => row.id))">
  80. <template #reference>
  81. <el-button :disabled="selection.length <= 0 || perm('{{camelCase name}}:delete')" :icon="Delete">\{{ $t('delete') }}</el-button>
  82. </template>
  83. </el-popconfirm>
  84. <list-move :disabled="selection.length <= 0 || filtered || perm('org:update')" @move="(type) => move(selection, type)" />
  85. <column-setting name="{{camelCase name}}" />
  86. </div>
  87. <div class="app-block mt-3">
  88. <el-table
  89. ref="table"
  90. v-loading="loading"
  91. :data="data"
  92. @selection-change="(rows) => (selection = rows)"
  93. @row-dblclick="(row) => handleEdit(row.id)"
  94. @sort-change="handleSort"
  95. >
  96. <column-list name="{{camelCase name}}">
  97. <el-table-column type="selection" width="45"></el-table-column>
  98. <el-table-column property="id" label="ID" width="64" sortable="custom"></el-table-column>
  99. <el-table-column property="name" :label="$t('{{camelCase name}}.name')" sortable="custom" show-overflow-tooltip></el-table-column>
  100. <el-table-column :label="$t('table.action')">
  101. <template #default="{row}">
  102. <el-button type="primary" :disabled="perm('{{camelCase name}}:update')" @click="() => handleEdit(row.id)" size="small" link>\{{ $t('edit') }}</el-button>
  103. <el-popconfirm :title="$t('confirmDelete')" @confirm="() => handleDelete([row.id])">
  104. <template #reference>
  105. <el-button type="primary" :disabled="perm('{{camelCase name}}:delete')" size="small" link>\{{ $t('delete') }}</el-button>
  106. </template>
  107. </el-popconfirm>
  108. </template>
  109. </el-table-column>
  110. </column-list>
  111. </el-table>
  112. </div>
  113. <{{kebabCase name}}-form v-model="formVisible" :bean-id="beanId" :bean-ids="beanIds" @finished="() => fetchData()" />
  114. </div>
  115. </template>