zxl
2025-03-03 c78889e71d16d26c03ff59746db47c6d4d2b98e0
src/components/flow/Role/MyRole.vue
@@ -1,11 +1,12 @@
<template>
  <div>
    <el-dialog
      :title="`选择候选角色`"
      :title="title"
      :visible.sync="show"
      width="65%"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      :modal="false"
      :before-close="close">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
@@ -14,6 +15,7 @@
            v-model="queryParams.roleName"
            placeholder="请输入角色名称"
            clearable
            @clear="handleQuery"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
@@ -24,13 +26,13 @@
        </el-form-item>
      </el-form>
      <el-table ref="dataTable"  v-loading="loading" :data="roleList" @selection-change="handleMultipleRoleSelect">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="角色编号" prop="roleId" width="120" />
        <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="显示顺序" prop="roleSort" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      <el-table ref="dataTable" :row-key="getRowKey" v-loading="loading"  :data="roleList" @selection-change="handleMultipleRoleSelect">
        <el-table-column type="selection" width="50" align="center"  :reserve-selection="true"/>
        <el-table-column label="角色编号" prop="roleId"/>
        <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true"/>
        <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true"/>
        <el-table-column label="显示顺序" prop="roleSort"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
@@ -68,6 +70,11 @@
      required: true,
      default: true
    },
    title: {
      type: String,
      required: false,
      default: '候选角色'
    },
    // 回显数据传值
    selectValues: {
      type: Array,
@@ -79,22 +86,12 @@
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
@@ -103,72 +100,29 @@
        roleKey: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      radioSelected: 0, // 单选框传值
      selectRoleList: [] // 回显数据传值
    };
  },
  watch: {
    selectValues: {
      deep: true,
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.$nextTick(() => {
            this.$refs.dataTable.clearSelection();
            this.selectRoleList = []
            newVal.forEach(item => {
              this.roleList.forEach(role => {
                if (item.roleId === role.roleId) {
                  this.selectRoleList.push(role)
                  this.$refs.dataTable.toggleRowSelection(role)
                }
              })
            });
          })
        } else {
          this.selectRoleList = []
        }
      }
    },
    roleList: {
      deep: true,
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.$nextTick(() => {
            this.$refs.dataTable.clearSelection();
            this.selectValues.forEach(item => {
              newVal.forEach(role => {
                if (item.roleId === role.roleId) {
                  this.$refs.dataTable.toggleRowSelection(role)
                }
              })
            });
          })
        }
      }
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
    getRowKey (row) {
      console.log(row.roleId)
      return row.roleId
    },
    setChecked(val) {
      if (val && val.length > 0) {
        this.$nextTick(() => {
          this.$refs.dataTable.clearSelection();
          this.selectRoleList = val
          val.forEach(check => {
            this.roleList.forEach(item => {
              if (check.roleId === item.roleId) {
                this.$refs.dataTable.toggleRowSelection(item)
              }
            })
          })
        });
      } else {
        this.selectRoleList = []
      }
      this.selectRoleList = val
      console.log("接收到的数据", this.selectRoleList)
      this.$nextTick(() => {
        this.selectRoleList.forEach(check => {
          this.$refs.dataTable.toggleRowSelection(check, true)
        })
      });
    },
    close() {
      this.$emit("close")
@@ -187,8 +141,8 @@
      );
    },
    // 多选框选中数据
    handleMultipleRoleSelect(selection) {
      this.selectRoleList = selection
    handleMultipleRoleSelect(rows) {
      this.selectRoleList = [...new Set(rows)]
    },
    /** 搜索按钮操作 */
    handleQuery() {