user-select.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <el-dialog :title="title" :visible.sync="visible" width="600px" :close-on-click-modal="false">
  3. <div class="content">
  4. <div class="left">
  5. <el-scrollbar>
  6. <el-tree ref="tree" :data="treeItems" :props="navProps" node-key="_id" @node-click="handleNavClick"
  7. :highlight-current="true">
  8. <span class="custom-tree-node" slot-scope="{ node, data }">
  9. <span>
  10. <i class="prefix naf-icons naf-icon-user1" v-if="data.userid" />
  11. <i class="prefix naf-icons naf-icon-folder1" v-else />
  12. {{ node.label }}
  13. </span>
  14. <span>
  15. <i class="suffix naf-icons naf-icon-ok" v-show="inSelected(data)" />
  16. </span>
  17. </span>
  18. </el-tree>
  19. </el-scrollbar>
  20. </div>
  21. <div class="right">
  22. <el-scrollbar>
  23. <el-tree ref="selected" :data="selected" :props="navProps" node-key="id" :highlight-current="true">
  24. <span class="custom-tree-node" slot-scope="{ node, data }">
  25. <span>
  26. <i class="prefix naf-icons naf-icon-user1" v-if="data.userid" />
  27. <i class="prefix naf-icons naf-icon-folder1" v-else />
  28. {{ node.label }}
  29. </span>
  30. <span style="float: right">
  31. <i class="suffix naf-icons naf-icon-close" />
  32. </span>
  33. </span>
  34. </el-tree>
  35. </el-scrollbar>
  36. </div>
  37. </div>
  38. <div slot="footer" class="dialog-footer">
  39. <el-button @click="$emit('cancel')" :size="options.size">取 消</el-button>
  40. <el-button type="primary" @click="$emit('ok', selected)" :size="options.size">确 定</el-button>
  41. </div>
  42. </el-dialog>
  43. </template>
  44. <script>
  45. import { mapState } from 'vuex';
  46. import _ from 'lodash';
  47. export default {
  48. props: {
  49. options: {
  50. type: Object,
  51. default: () => ({ size: 'small' }),
  52. } /* form options */,
  53. title: String,
  54. visible: { type: Boolean, default: true },
  55. },
  56. data() {
  57. return {
  58. navProps: {
  59. children: 'children',
  60. label: 'name',
  61. },
  62. selected: [],
  63. };
  64. },
  65. methods: {
  66. handleNavClick(data) {
  67. // this.$emit('selected', data);
  68. const idx = this.selected.findIndex(p => (_.isNumber(data.id) && p.id === data.id) || (_.isString(data.userid) && p.userid === data.userid));
  69. console.log('idx:', idx);
  70. if (idx === -1) {
  71. this.selected.push(_.omit(data, ['children']));
  72. } else {
  73. this.selected.splice(idx, 1);
  74. }
  75. },
  76. inSelected(data) {
  77. return this.selected.some(p => (_.isNumber(data.id) && p.id === data.id) || (_.isString(data.userid) && p.userid === data.userid));
  78. },
  79. },
  80. computed: {
  81. ...mapState({
  82. deptItems: state => state.system.dept.items,
  83. userItems: state => state.system.user.items,
  84. }),
  85. treeItems() {
  86. if (this.navItems) return this.navItems;
  87. const items = this.deptItems || [];
  88. const root = items.filter(a => !items.some(b => b.id === a.parentid));
  89. const fetchChildren = item => {
  90. const children = items.filter(p => p.parentid === item.id).map(p => fetchChildren(p));
  91. const users = this.userItems.filter(p => p.department && p.department.includes(item.id));
  92. return { ...item, children: [...children, ...users] };
  93. };
  94. const rootUsers = this.userItems.filter(p => p.department && p.department.includes(0));
  95. return root.map(p => fetchChildren(p)).concat(rootUsers);
  96. },
  97. },
  98. };
  99. </script>
  100. <style lang="less" scoped>
  101. .nav-tree {
  102. /deep/ .el-tree-node.is-current > .el-tree-node__content {
  103. background-color: #409eff;
  104. color: white;
  105. .naf-icon-dian {
  106. display: inline;
  107. color: white;
  108. }
  109. }
  110. /deep/ .naf-icon-dian {
  111. display: none;
  112. }
  113. /deep/ .el-tree-node__content:hover .naf-icon-dian {
  114. display: inline;
  115. }
  116. }
  117. .content {
  118. display: flex;
  119. flex-direction: row;
  120. height: 340px;
  121. }
  122. .left {
  123. flex: 1;
  124. }
  125. .right {
  126. width: 240px;
  127. }
  128. .naf-icons.prefix {
  129. color: grey;
  130. }
  131. .naf-icons.suffix {
  132. color: lightgrey;
  133. }
  134. .custom-tree-node {
  135. flex: 1;
  136. display: flex;
  137. align-items: center;
  138. justify-content: space-between;
  139. font-size: 14px;
  140. padding-right: 8px;
  141. }
  142. .el-scrollbar {
  143. height: 100%;
  144. }
  145. </style>