JSelectUserByDepModal.vue 8.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<template>
  <a-modal
    :width="modalWidth"
    :visible="visible"
    :title="title"
    @ok="handleSubmit"
    @cancel="close"
    cancelText="关闭"
    style="margin-top: -70px"
    wrapClassName="ant-modal-cust-warp"
  >
    <a-row :gutter="10" style="background-color: #ececec; padding: 10px; margin: -10px">
      <a-col :md="6" :sm="24">
        <a-card :bordered="false">
          <!--组织机构-->
          <a-directory-tree
            selectable
18
            :selectedKeys="selectedDepIds"
19
            :checkStrictly="true"
20
            @select="onDepSelect"
21 22 23 24 25 26 27 28 29 30
            :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
            :treeData="departTree"
          />
        </a-card>
      </a-col>
      <a-col :md="18" :sm="24">
        <a-card :bordered="false">
          用户账号:
          <a-input-search
            :style="{width:'150px',marginBottom:'15px'}"
31
            placeholder="请输入用户账号"
32 33
            v-model="queryParam.username"
            @search="onSearch"
34 35
          ></a-input-search>
          <a-button @click="searchReset(1)" style="margin-left: 20px" icon="redo">重置</a-button>
36 37 38 39 40 41 42 43 44
          <!--用户列表-->
          <a-table
            ref="table"
            :scroll="scrollTrigger"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
45
            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange,type: getType}"
46 47 48 49 50 51 52 53 54
            @change="handleTableChange">
          </a-table>
        </a-card>
      </a-col>
    </a-row>
  </a-modal>
</template>

<script>
55 56 57
  import {filterObj} from '@/utils/util'
  import {queryDepartTreeList, getUserList, queryUserByDepId} from '@/api/api'

58
  export default {
59
    name: 'JSelectUserByDepModal',
60
    components: {},
61
    props: ['modalWidth', 'multi', 'userIds'],
62 63
    data() {
      return {
64
        queryParam: {
65
          username: "",
66
        },
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        columns: [
          {
            title: '用户账号',
            align: 'center',
            dataIndex: 'username'
          },
          {
            title: '真实姓名',
            align: 'center',
            dataIndex: 'realname'
          },
          {
            title: '性别',
            align: 'center',
            dataIndex: 'sex',
82
            customRender: function (text) {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
              if (text === 1) {
                return ''
              } else if (text === 2) {
                return ''
              } else {
                return text
              }
            }
          },
          {
            title: '手机号码',
            align: 'center',
            dataIndex: 'phone'
          },
          {
            title: '邮箱',
            align: 'center',
            dataIndex: 'email'
          }
        ],
        scrollTrigger: {},
        dataSource: [],
105 106 107
        selectedRowKeys: [],
        selectUserRows: [],
        selectUserIds: [],
108
        title: '根据部门选择用户',
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + '' + total + ''
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        isorter: {
          column: 'createTime',
          order: 'desc'
        },
124
        selectedDepIds: [],
125 126 127 128 129
        departTree: [],
        visible: false,
        form: this.$form.createForm(this)
      }
    },
130 131 132 133 134 135 136 137 138 139 140 141
    computed: {
      // 计算属性的 getter
      getType: function () {
        console.log("multi: ", this.multi);
        return this.multi == true ? 'checkbox' : 'radio';
      }
    },
    watch: {
      userIds() {
        this.initUserNames()
      }
    },
142 143 144
    created() {
      // 该方法触发屏幕自适应
      this.resetScreenSize();
145 146 147
      this.loadData().then((res) => {
        this.initUserNames();
      })
148 149
    },
    methods: {
150 151 152 153 154
      initUserNames() {
        let names = ''
        console.log("props userIds: ", this.userIds)
        if (this.userIds) {
          let currUserIds = this.userIds
155
          let userIdsArr = currUserIds.split(',');
156
          for (let item of this.dataSource) {
157
            if (userIdsArr.includes(item.username)) {
158 159 160 161 162 163 164
              names += "," + item.realname
            }
          }
          if (names) {
            names = names.substring(1)
          }
          this.$emit("initComp", names)
JEECG低代码平台's avatar
JEECG低代码平台 已提交
165 166 167
        }else{
          // JSelectUserByDep组件bug issues/I16634
          this.$emit("initComp", "")
168 169 170
        }
      },
      async loadData(arg) {
171 172 173 174
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        let params = this.getQueryParams();//查询条件
175
        await getUserList(params).then((res) => {
176 177 178 179 180 181 182 183 184 185
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
          }
        })
      },
      // 触发屏幕自适应
      resetScreenSize() {
        let screenWidth = document.body.clientWidth;
        if (screenWidth < 500) {
186
          this.scrollTrigger = {x: 800};
187 188 189 190 191 192 193
        } else {
          this.scrollTrigger = {};
        }
      },
      showModal() {
        this.visible = true;
        this.queryDepartTree();
194
        this.loadData();
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        this.form.resetFields();
      },
      getQueryParams() {
        let param = Object.assign({}, this.queryParam, this.isorter);
        param.field = this.getQueryField();
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        return filterObj(param);
      },
      getQueryField() {
        let str = 'id,';
        for (let a = 0; a < this.columns.length; a++) {
          str += ',' + this.columns[a].dataIndex;
        }
        return str;
      },
      searchReset(num) {
        let that = this;
213
        if (num !== 0) {
214 215
          that.queryParam = {};
          that.loadData(1);
216 217
        }
        that.selectedRowKeys = [];
218 219
        that.selectUserIds = [];
        that.selectedDepIds = [];
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
      },
      close() {
        this.searchReset(0);
        this.visible = false;
      },
      handleTableChange(pagination, filters, sorter) {
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field;
          this.isorter.order = 'ascend' === sorter.order ? 'asc' : 'desc';
        }
        this.ipagination = pagination;
        this.loadData();
      },
      handleSubmit() {
        let that = this;
236 237 238 239
        this.getSelectUserRows();
        console.log(that.selectUserRows)
        that.$emit('ok', that.selectUserRows, that.selectUserIds);
        that.searchReset(0)
240 241
        that.close();
      },
242 243
      //获取选择用户信息
      getSelectUserRows(rowId) {
244
        let dataSource = this.dataSource;
245 246
        let userIds = "";
        this.selectUserRows = [];
247
        for (let i = 0, len = dataSource.length; i < len; i++) {
248 249 250
          if (this.selectedRowKeys.includes(dataSource[i].id)) {
            this.selectUserRows.push(dataSource[i]);
            userIds = userIds + "," + dataSource[i].username
251 252
          }
        }
253
        this.selectUserIds = userIds.substring(1);
254 255
      },
      // 点击树节点,筛选出对应的用户
256 257 258 259 260
      onDepSelect(selectedDepIds) {
        if (selectedDepIds[0] != null) {
          this.initQueryUserByDepId(selectedDepIds); // 调用方法根据选选择的id查询用户信息
          if (this.selectedDepIds[0] !== selectedDepIds[0]) {
            this.selectedDepIds = [selectedDepIds[0]];
261 262 263 264 265 266 267 268 269 270 271
          }
        }
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectionRows = selectionRows;
      },
      onSearch() {
        this.loadData(1);
      },
      // 根据选择的id来查询用户信息
272 273
      initQueryUserByDepId(selectedDepIds) {
        queryUserByDepId({id: selectedDepIds.toString()}).then((res) => {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
          if (res.success) {
            this.dataSource = res.result;
            this.ipagination.total = res.result.length;
          }
        })
      },
      queryDepartTree() {
        queryDepartTreeList().then((res) => {
          if (res.success) {
            this.departTree = res.result;
          }
        })
      },
      modalFormOk() {
        this.loadData();
      }
    }
  }
</script>

<style scoped>
  .ant-table-tbody .ant-table-row td {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  #components-layout-demo-custom-trigger .trigger {
    font-size: 18px;
    line-height: 64px;
    padding: 0 24px;
    cursor: pointer;
    transition: color .3s;
  }
</style>