| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <template>
- <el-dialog
- :title="title"
- :visible.sync="visible"
- :before-close="handleClose"
- :close-on-click-modal="false"
- top="5vh"
- :close-on-press-escape="false"
- append-to-body
- width="70%"
- >
- <el-card shadow="never">
- <ele-split-layout
- width="244px"
- allow-collapse
- :right-style="{ overflow: 'hidden' }"
- >
- <div class="ele-border-lighter split-layout-right-content">
- <DepartmentTree
- @handleNodeClick="handleNodeClick"
- :isFirstRefreshTable="false"
- ref="treeList"
- />
- </div>
- <!-- 表格 -->
- <template v-slot:content>
- <user-select-search
- ref="search"
- @search="reload"
- ></user-select-search>
- <ele-pro-table
- ref="table"
- :columns="currentColumns"
- :datasource="datasource"
- row-key="id"
- :height="tableHeight"
- class="dict-table"
- @cell-click="cellClick"
- @selection-change="handleSelectionChange"
- @done="handleTableDone"
- >
- <!-- 表头工具栏 -->
- <template v-slot:action="{ row }" v-if="!isMultiple">
- <el-radio class="radio" v-model="radio" :label="row.id"
- ><i></i
- ></el-radio>
- </template>
- </ele-pro-table>
- </template>
- </ele-split-layout>
- </el-card>
- <div slot="footer" class="dialog-footer">
- <span v-if="isMultiple" class="selected-count"
- >已选 {{ selectedCount }} 人</span
- >
- <el-button type="primary" size="small" @click="selected">选择</el-button>
- <el-button size="small" @click="handleClose">关闭</el-button>
- </div>
- </el-dialog>
- </template>
- <script>
- import { getUserPage } from '@/api/system/organization';
- import DepartmentTree from '@/components/DepartmentTree/index.vue';
- import userSelectSearch from './user-select-search.vue';
- export default {
- components: {
- DepartmentTree,
- userSelectSearch
- },
- props: {
- disabledList: {
- default: () => {
- return [];
- }
- },
- // 已选人员(打开时回显,确定后与本次勾选合并返回)
- selectedList: {
- type: Array,
- default: () => {
- return [];
- }
- }
- },
- data() {
- return {
- currentIndex: 0,
- title: '选择',
- visible: false,
- isMultiple: false,
- selectedRows: [],
- // 本次打开时已选人员(在表格中选择框中勾选回显)
- preselected: [],
- // 本次打开后被取消勾选的已选人员 id
- uncheckedIds: [],
- // 当前选中的部门节点
- curNodeData: null,
- current: null,
- statusOptions: [
- { value: 1, label: '全职' },
- { value: 2, label: '兼职' },
- { value: 3, label: '实习' },
- { value: 4, label: '正式' },
- { value: 5, label: '试用' },
- { value: 6, label: '离职' }
- ],
- columns: [
- {
- action: 'action',
- slot: 'action',
- align: 'center',
- label: '选择'
- },
- {
- columnKey: 'index',
- label: '序号',
- type: 'index',
- width: 55,
- align: 'center',
- showOverflowTooltip: true,
- fixed: 'left'
- },
- {
- prop: 'name',
- label: '姓名',
- sortable: 'custom',
- showOverflowTooltip: true,
- minWidth: 110
- },
- {
- prop: 'jobNumber',
- label: '工号',
- sortable: 'custom',
- showOverflowTooltip: true,
- minWidth: 110
- },
- {
- prop: 'loginName',
- label: '用户账号',
- sortable: 'custom',
- showOverflowTooltip: true,
- minWidth: 110
- },
- {
- prop: 'sex',
- label: '性别',
- sortable: 'custom',
- showOverflowTooltip: true,
- minWidth: 80,
- formatter: (_row, _column, cellValue) => {
- return cellValue == 1 ? '男' : cellValue == 2 ? '女' : '';
- }
- },
- {
- prop: 'status',
- label: '状态',
- align: 'center',
- sortable: 'custom',
- width: 80,
- formatter: (_row, _column, cellValue) => {
- const dom = this.statusOptions.find((item) => {
- return item.value == cellValue;
- });
- return dom ? dom.label : '';
- }
- }
- ],
- radio: null
- };
- },
- computed: {
- // 表格高度
- tableHeight() {
- return 'calc(100vh - 430px)';
- },
- // 已选人数
- selectedCount() {
- return this.mergeSelected().length;
- },
- currentColumns() {
- if (this.isMultiple) {
- return [
- {
- type: 'selection',
- // 跨页/重新加载后保留勾选状态,配合 row-key 使用
- reserveSelection: true,
- width: 55,
- align: 'center',
- selectable: (row, index) => {
- return !this.disabledList
- .map((item) => item.id)
- .includes(row.id);
- }
- },
- ...this.columns
- ];
- }
- return this.columns;
- }
- },
- watch: {},
- methods: {
- open(item, index = 0, multiple = false) {
- this.isMultiple = multiple;
- this.selectedRows = [];
- this.uncheckedIds = [];
- this.current = null;
- this.radio = null;
- // 已选人员回显(打开时在表格的选择框中勾选)
- this.preselected = [...(this.selectedList || [])];
- if (item) {
- this.radio = item.id;
- // 单选时保持原有选中,未重新选择则沿用
- this.current = item;
- } else if (!multiple && this.preselected.length) {
- const first = this.preselected[0];
- this.radio = first.id;
- this.current = first;
- }
- this.currentIndex = index;
- // 先亮起弹窗再排 nextTick:首次打开时对话框内容尚未挂载,
- // 否则 clearSelection 执行到 $refs.table 时还是 undefined
- this.visible = true;
- this.$nextTick(() => {
- if (this.$refs.table) {
- this.$refs.table.clearSelection();
- // 已选人员勾选到表格的选择框中
- this.syncPreselectedSelection();
- }
- // 每次打开重置搜索条件与部门筛选
- this.resetFilter();
- });
- },
- /* 重置搜索条件与部门树选中 */
- resetFilter() {
- this.curNodeData = null;
- const tree = this.$refs.treeList && this.$refs.treeList.getInstance();
- if (tree) {
- tree.setCurrentKey(null);
- }
- // 每页条数回初始值
- const table = this.$refs.table;
- if (table) {
- const pageSize = table.pageSize || 10;
- if (table.tableLimit !== pageSize) {
- table.tableLimit = pageSize;
- }
- }
- if (this.$refs.search) {
- // 清空搜索条件并重新加载表格
- this.$refs.search.reset();
- } else {
- this.reload();
- }
- },
- /* 表格数据加载完成:保证已选人员仍处于勾选状态 */
- handleTableDone() {
- this.syncPreselectedSelection();
- },
- /* 将已选人员同步为表格中的勾选状态 */
- syncPreselectedSelection() {
- const table = this.$refs.table;
- if (!table || !this.isMultiple) {
- return;
- }
- const dataMap = new Map();
- this.getTableData().forEach((row) => dataMap.set(row.id, row));
- const selectedIds = new Set(
- (this.selectedRows || []).map((row) => row.id)
- );
- this.preselected.forEach((item) => {
- if (
- !item ||
- item.id == null ||
- selectedIds.has(item.id) ||
- // 本次打开后被取消勾选的,不再重新勾选
- this.uncheckedIds.includes(item.id)
- ) {
- return;
- }
- // 优先使用表格中的行对象,保证复选框能正常回显
- const row = dataMap.get(item.id) || item;
- table.toggleRowSelection(row, true);
- selectedIds.add(item.id);
- });
- },
- /* 表格当前数据 */
- getTableData() {
- const table = this.$refs.table;
- if (table && typeof table.getData === 'function') {
- return table.getData() || [];
- }
- return [];
- },
- /* 合并已选人员与本次勾选的人员 */
- mergeSelected() {
- const map = new Map();
- const dataMap = new Map(
- this.getTableData().map((row) => [row.id, row])
- );
- // 已选人员(未被取消勾选的)
- this.preselected.forEach((item) => {
- if (!item || item.id == null || this.uncheckedIds.includes(item.id)) {
- return;
- }
- map.set(item.id, dataMap.get(item.id) || item);
- });
- // 本次勾选的人员
- (this.selectedRows || []).forEach((row) => {
- if (!row || row.id == null) {
- return;
- }
- map.set(row.id, dataMap.get(row.id) || row);
- });
- return Array.from(map.values());
- },
- /* 表格数据源 */
- datasource({ page, limit, where }) {
- const params = {
- pageNum: page,
- size: limit
- };
- Object.keys(where || {}).forEach((key) => {
- const value = where[key];
- if (value !== undefined && value !== null && value !== '') {
- params[key] = value;
- }
- });
- return getUserPage(params);
- },
- /* 刷新表格 */
- reload(where) {
- this.$refs.table.reload({ page: 1, where: where });
- },
- handleNodeClick(data, node) {
- this.curNodeData = data;
- this.reload({ groupId: data.id });
- },
- // 单击获取id
- cellClick(row) {
- if (this.isMultiple) return;
- this.current = row;
- this.radio = row.id;
- },
- handleSelectionChange(rows) {
- const list = rows || [];
- const prevIds = new Set((this.selectedRows || []).map((row) => row.id));
- const curIds = new Set(list.map((row) => row.id));
- // 被取消勾选的人员
- prevIds.forEach((id) => {
- if (!curIds.has(id) && !this.uncheckedIds.includes(id)) {
- this.uncheckedIds.push(id);
- }
- });
- // 被重新勾选的人员
- this.uncheckedIds = this.uncheckedIds.filter((id) => !curIds.has(id));
- this.selectedRows = list;
- },
- handleClose() {
- this.visible = false;
- this.current = null;
- this.radio = null;
- this.selectedRows = [];
- this.uncheckedIds = [];
- this.preselected = [];
- },
- selected() {
- if (this.isMultiple) {
- const rows = this.mergeSelected();
- if (!rows.length) {
- return this.$message.warning('请至少选择一项');
- }
- this.$emit('changeParent', rows, this.currentIndex);
- } else {
- if (!this.current) {
- return this.$message.warning('请至少选择一项');
- }
- this.$emit('changeParent', this.current, this.currentIndex);
- }
- this.handleClose();
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .selected-count {
- float: left;
- line-height: 32px;
- color: #606266;
- font-size: 13px;
- }
- .tree_col {
- border: 1px solid #eee;
- padding: 10px 0;
- box-sizing: border-box;
- height: 500px;
- overflow: auto;
- }
- .table_col {
- padding-left: 10px;
- ::v-deep .el-table th.el-table__cell {
- background: #f2f2f2;
- }
- }
- .pagination {
- text-align: right;
- padding: 10px 0;
- }
- .btns {
- text-align: center;
- padding: 10px 0;
- }
- .topsearch {
- margin-bottom: 15px;
- }
- </style>
|