DictList.vue 10.2 KB
<template>
  <el-container>
    <el-header style="height: 30px">
      <div class="search-container">
        <div class="search-item">
          <span class="search-label">字典编码</span>
          <el-input
            placeholder="请输入内容"
            v-model="inputCode"
            style="width: 200px; margin-right: 20px"
          >
          </el-input>
        </div>
        <div class="search-item">
          <span class="search-label">字典名称</span>
          <el-input
            size="medium"
            placeholder="请输入内容"
            v-model="inputName"
            style="width: 200px"
          >
          </el-input>
        </div>
      </div>
    </el-header>

    <el-main>
      <div>
        <el-card shadow="never">
          <div style="text-align: right">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-search"
              @click="handleSearch"
            >
              查询
            </el-button>
            <el-button class="T1" size="small" @click="handleDictDetail"
              >查询详情</el-button
            >
            <el-button
              type="success"
              size="small"
              icon="el-icon-plus"
              @click="handleAdd"
            >
              新增
            </el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleBatchDelete"
            >
              删除
            </el-button>
          </div>
        </el-card>
      </div>
      <el-dialog
        title="新增字典"
        :visible.sync="dialogFormVisible"
        width="30%"
        center
      >
        <el-form :model="form" label-width="80px">
          <el-form-item label="字典编码">
            <el-input v-model="form.code" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="字典名称">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAddData">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        title="编辑字典"
        :visible.sync="EditdialogFormVisible"
        width="30%"
        center
      >
        <el-form :model="Editform" label-width="80px">
          <el-form-item label="字典编码">
            <el-input v-model="Editform.code" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="字典名称">
            <el-input v-model="Editform.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="Editform.remark" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="EditdialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            :loading="editLoading"
            @click="handleEditData"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <el-table
        :data="dictList"
        :height="TableHeight"
        border
        highlight-current-row
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="code" label="字典编码"></el-table-column>
        <el-table-column
          prop="name"
          width="280"
          label="字典名称"
        ></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="created" label="创建时间"></el-table-column>
        <el-table-column prop="updatedBy" label="创建人"></el-table-column>
        <el-table-column prop="updated" label="更新时间"></el-table-column>
        <el-table-column prop="updatedBy" label="更新人"></el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.id"
              type="text"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 15px"
        align="center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-main>
  </el-container>
</template>

<script>
import {
  GetDictsByPage,
  AddDict,
  EditDict,
  DeleteDicts,
} from "@/api/systemManage/dict";
export default {
  name: "DictList",
  props: {
    switchToDetail: {
      type: Function,
      required: true,
    },
    sendParams: {
      type: Function,
      required: true,
    },
  },
  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSize: 10,
      dictList: [], // 字典列表数据
      inputCode: "",
      inputName: "",
      selectedItems: [],
      dialogFormVisible: false,
      form: {
        code: "",
        name: "",
        remark: "",
      },
      EditdialogFormVisible: false,
      Editform: {
        code: "",
        name: "",
        remark: "",
      },
      editLoading: false,
      lastSelectedRow: null, // 新增:记录最后选中的行
    };
  },
  computed: {
    TableHeight() {
      return `calc(100vh - 350px )`;
    },
  },
  methods: {
    handleRowClick(row) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    handleDictDetail() {
      if (this.selectedItems.length === 0 || this.selectedItems.length > 1) {
        this.$message.warning("请选择一条数据");
        return;
      }

      const currentRow = this.selectedItems[0];
      if (!this.lastSelectedRow || this.lastSelectedRow.id !== currentRow.id) {
        this.sendParams(currentRow.code, currentRow.name, currentRow.id);
        this.lastSelectedRow = currentRow;
      } else {
        this.switchToDetail();
      }
    },
    handleSelectionChange(val) {
      this.selectedItems = val;
      // console.log("选中的行数据:", this.selectedItems);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.handleSearch();
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.handleSearch();
    },
    handleEdit(row) {
      this.EditdialogFormVisible = true;
      this.Editform = { ...row };
    },
    handleEditData() {
      if (!this.Editform.code) {
        this.$message.error("字典编码不能为空");
        return;
      }
      if (!this.Editform.name) {
        this.$message.error("字典名称不能为空");
        return;
      }
      this.editLoading = true;
      EditDict(this.Editform).then((res) => {
        if (res.code === "Success") {
          this.$message.success("数据修改成功");
          this.EditdialogFormVisible = false;
          this.editLoading = false;
          this.handleSearch();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    handleSearch() {
      const requestData = {
        pageNumber: this.currentPage,
        perPageCount: this.pageSize,
        queryConfig: {
          dictId: 0,
          code: this.inputCode.trim(),
          name: this.inputName.trim(),
        },
      };
      GetDictsByPage(requestData).then((res) => {
        console.log("查询字典", res.data.data);
        if (res.code == "Success" && res.data) {
          this.dictList = res.data.data || res.data;
          this.total = res.data.totalCount || res.data.data.length;
        }
      });
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {
        code: "",
        name: "",
        remark: "",
      };
    },
    handleAddData() {
      if (!this.form.code) {
        this.$message.error("字典编码不能为空");
        return;
      }
      if (!this.form.name) {
        this.$message.error("字典名称不能为空");
        return;
      }
      AddDict(this.form).then((res) => {
        if (res.code === "Success") {
          this.$message.success("新增成功");
          this.dialogFormVisible = false;
          this.handleSearch();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    handleBatchDelete() {
      if (this.selectedItems.length === 0) {
        this.$alert("请至少选择一条数据", "提示", {
          confirmButtonText: "确定",
        });
        return;
      }

      this.$confirm("确定要删除选中的数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const list = this.selectedItems;
          DeleteDicts(list).then((res) => {
            if (res.code === "Success") {
              this.$message.success("删除成功");
              this.handleSearch();
            }
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep.el-table thead {
  color: #101010;
  font-weight: 600;
}
.app-container {
  padding: 16px;
}
.search-container {
  display: flex;
  align-items: center;
  .search-item {
    display: flex;
    align-items: center;
    margin-right: 20px;
    .search-label {
      margin-right: 10px;
      white-space: nowrap;
    }
  }
}
.el-table__body tr.current-row {
  background-color: #f0f9eb; /* 自定义高亮背景色 */
}
.el-card {
  border-radius: 4px;
  border: 1px solid transparent;
  background-color: #fff;
  overflow: hidden;
  color: #303133;
  -webkit-transition: 0.3s;
  transition: 0.3s;
}
</style>