index.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <el-dialog v-model="visible" title="选择设备" width="800px" :close-on-click-modal="false" @close="onCancel">
  3. <div style="margin-bottom: 12px">
  4. <el-input v-model="search" placeholder="输入关键字搜索" style="width: 240px; margin-right: 8px" @keyup.enter="fetchData(true)" />
  5. <el-button type="primary" @click="fetchData(true)">搜索</el-button>
  6. </div>
  7. <el-table :data="tableData" border style="width: 100%" @selection-change="onSelectionChange" ref="tableRef">
  8. <el-table-column type="selection" width="50" />
  9. <el-table-column type="index" label="序号" width="60" />
  10. <el-table-column prop="category_name" label="设备分类" />
  11. <el-table-column prop="warehouse" label="存放仓库" />
  12. </el-table>
  13. <el-pagination
  14. style="margin-top: 12px; text-align: right"
  15. :current-page="page"
  16. :page-size="pageSize"
  17. :total="total"
  18. @current-change="onPageChange"
  19. />
  20. <template #footer>
  21. <el-button @click="onCancel">取消</el-button>
  22. <el-button type="primary" @click="onConfirm">确定</el-button>
  23. </template>
  24. </el-dialog>
  25. </template>
  26. <script setup lang="ts">
  27. import { ref, watch, defineProps, defineEmits } from 'vue';
  28. import * as deviceApi from '../../../../device/api';
  29. import * as storeapi from '../../../../storelist/api';
  30. const props = defineProps<{ visible: boolean }>();
  31. const emit = defineEmits(['update:visible', 'confirm']);
  32. const visible = ref(props.visible);
  33. watch(
  34. () => props.visible,
  35. (v) => (visible.value = v)
  36. );
  37. const search = ref('');
  38. const page = ref(1);
  39. const pageSize = ref(10);
  40. const total = ref(0);
  41. const tableData = ref([]);
  42. const selectedRows = ref([]);
  43. const warehouseMap = ref<Record<number, string>>({});
  44. function fetchWarehouseMap() {
  45. storeapi.GetPermission().then((res) => {
  46. const map: Record<number, string> = {};
  47. res.data.forEach((item) => {
  48. map[item.id] = item.name;
  49. });
  50. warehouseMap.value = map;
  51. });
  52. }
  53. async function fetchData(resetPage = false) {
  54. if (resetPage) page.value = 1;
  55. await fetchWarehouseMap();
  56. deviceApi.GetList({ page: page.value, limit: pageSize.value, category_name: search.value })
  57. .then((res) => {
  58. const devices = res.data
  59. .filter(device => device.available_quantity > 0)
  60. .map((device) => ({
  61. ...device,
  62. warehouse: `${warehouseMap.value[device.warehouse] || `ID:${device.warehouse}`} (${device.available_quantity})`
  63. }));
  64. tableData.value = devices;
  65. // tableData.value = [...res.data];
  66. total.value = res.total;
  67. });
  68. }
  69. function onSelectionChange(val) {
  70. selectedRows.value = val;
  71. }
  72. function onPageChange(val) {
  73. page.value = val;
  74. fetchData();
  75. }
  76. function onConfirm() {
  77. emit('confirm', selectedRows.value);
  78. emit('update:visible', false);
  79. }
  80. function onCancel() {
  81. emit('update:visible', false);
  82. }
  83. watch([visible, page], (v) => {
  84. if (visible.value) fetchData();
  85. });
  86. </script>