mailList.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <a-row :gutter="24">
  3. <!-- 左边 -->
  4. <a-col :span="6" style="padding:5px">
  5. <a-card>
  6. <a-input-search style="margin-bottom: 8px" placeholder="搜索" @change="onChange" />
  7. <a-tree
  8. :show-line="true"
  9. :expanded-keys="expandedKeys"
  10. :auto-expand-parent="autoExpandParent"
  11. :tree-data="gData"
  12. @expand="onExpand"
  13. >
  14. <template slot="title" slot-scope="{ title }">
  15. <span v-if="title.indexOf(searchValue) > -1">
  16. {{ title.substr(0, title.indexOf(searchValue)) }}
  17. <span
  18. style="color: #f50"
  19. >{{ searchValue }}</span>
  20. {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
  21. </span>
  22. <span v-else>{{ title }}</span>
  23. </template>
  24. </a-tree>
  25. </a-card>
  26. </a-col>
  27. <!-- 右边 -->
  28. <a-col :span="18" style="padding:5px">
  29. <a-card>
  30. <a-table
  31. :columns="columns"
  32. :row-key="record => record.id"
  33. :data-source="dataPageList"
  34. :pagination="pagination"
  35. :loading="loading"
  36. @change="handleTableChange"
  37. ></a-table>
  38. </a-card>
  39. </a-col>
  40. </a-row>
  41. </template>
  42. <script>
  43. //递归找出搜索项的父级key
  44. const getParentKey = (key, tree) => {
  45. let parentKey = ''
  46. for (let i = 0; i < tree.length; i++) {
  47. const node = tree[i]
  48. if (node.children) {
  49. node.children.forEach(item => {
  50. if (parentKey == '' && item.key == key) {
  51. parentKey = node.key
  52. } else {
  53. parentKey = getParentKey(key, node.children)
  54. }
  55. })
  56. }
  57. }
  58. return parentKey
  59. }
  60. export default {
  61. name: 'mailList',
  62. data() {
  63. return {
  64. expandedKeys: [],
  65. searchValue: '',
  66. autoExpandParent: true,
  67. //树形数据
  68. gData: [
  69. {
  70. key: '上海萃颠',
  71. title: '上海萃颠',
  72. scopedSlots: { title: 'title' },
  73. children: [
  74. {
  75. key: '开发部',
  76. title: '开发部',
  77. scopedSlots: { title: 'title' }
  78. },
  79. {
  80. key: '测试部',
  81. title: '测试部',
  82. scopedSlots: { title: 'title' }
  83. },
  84. {
  85. key: '实施部',
  86. title: '开发部',
  87. scopedSlots: { title: 'title' }
  88. },
  89. {
  90. key: '销售部',
  91. title: '销售部',
  92. scopedSlots: { title: 'title' }
  93. },
  94. {
  95. key: '行政部',
  96. title: '行政部',
  97. scopedSlots: { title: 'title' }
  98. },
  99. {
  100. key: '财务部',
  101. title: '财务部',
  102. scopedSlots: { title: 'title' }
  103. }
  104. ]
  105. }
  106. ],
  107. dataList: [],
  108. //表格标题
  109. columns: [
  110. {
  111. title: '#',
  112. dataIndex: 'index'
  113. },
  114. {
  115. title: '姓名',
  116. dataIndex: 'name',
  117. // sorter: true,
  118. width: '20%'
  119. // scopedSlots: { customRender: 'name' }
  120. },
  121. {
  122. title: '部门',
  123. dataIndex: 'gender',
  124. // filters: [
  125. // { text: 'Male', value: 'male' },
  126. // { text: 'Female', value: 'female' }
  127. // ],
  128. width: '20%'
  129. },
  130. {
  131. title: '职务',
  132. dataIndex: 'zhiwu'
  133. },
  134. {
  135. title: '手机',
  136. dataIndex: 'phone'
  137. },
  138. {
  139. title: '公司邮箱',
  140. dataIndex: 'email'
  141. }
  142. ],
  143. dataPageList: [{ id:"123",index: 1, name: '袁少华',gender:"开发部",zhiwu:"开发", phone:"12345697485"}], //表格数据集合
  144. loading: false, //表格加载
  145. pagination: {} //分页
  146. }
  147. },
  148. created() {
  149. this.generateList(this.gData)
  150. },
  151. computed: {},
  152. mounted() {},
  153. methods: {
  154. //把tree中的数据用list保存平级数据,用来查找搜索项
  155. generateList(data) {
  156. for (let i = 0; i < data.length; i++) {
  157. const node = data[i]
  158. const key = node.key
  159. this.dataList.push({ key, title: key })
  160. if (node.children) {
  161. this.generateList(node.children)
  162. }
  163. }
  164. },
  165. onExpand(expandedKeys) {
  166. this.expandedKeys = expandedKeys
  167. this.autoExpandParent = false
  168. },
  169. //搜索项初发事件
  170. onChange(e) {
  171. const value = e.target.value
  172. const expandedKeys = this.dataList
  173. .map(item => {
  174. if (item.title.indexOf(value) > -1) {
  175. return getParentKey(item.key, this.gData)
  176. }
  177. return null
  178. })
  179. .filter((item, i, self) => item && self.indexOf(item) === i)
  180. Object.assign(this, {
  181. expandedKeys,
  182. searchValue: value,
  183. autoExpandParent: true
  184. })
  185. },
  186. handleTableChange(pagination, filters, sorter) {
  187. // console.log(pagination)
  188. // const pager = { ...this.pagination }
  189. // pager.current = pagination.current
  190. // this.pagination = pager
  191. // this.fetch({
  192. // results: pagination.pageSize,
  193. // page: pagination.current,
  194. // sortField: sorter.field,
  195. // sortOrder: sorter.order,
  196. // ...filters
  197. // })
  198. }
  199. }
  200. }
  201. </script>
  202. <style lang="less" scoped>
  203. </style>