TableView.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <pannel class="el-main">
  3. <div slot="top" class="top">
  4. <div>
  5. <el-btn
  6. v-for="btn in view.btns.items"
  7. :key="btn.label"
  8. :act="btn"
  9. :multi-select="multiSelect"
  10. :plain="true"
  11. @action="handleButton"
  12. />
  13. </div>
  14. <ve-form :view="view.search" class="form-inline" @search="search" />
  15. </div>
  16. <ve-table
  17. slot="content"
  18. ref="table"
  19. v-loading="loading"
  20. :rows="rows"
  21. :columns="view.columns"
  22. :actions="view.actions"
  23. @action="ajax"
  24. @edit="edit"
  25. @sort="sortChange"
  26. @multipleSelection="handleSelectionChange"
  27. />
  28. <el-pagination
  29. v-if="pSize > 0"
  30. slot="foot"
  31. background
  32. :current-page.sync="page"
  33. layout="total, prev, pager, next"
  34. :total="total"
  35. :page-size="pSize"
  36. @current-change="handleCurrentChange"
  37. />
  38. </pannel>
  39. </template>
  40. <script>
  41. // import Rank from './Rank'
  42. import Pannel from './Pannel'
  43. import VeTable from './VeTable'
  44. import ElBtn from './elements/ElBtn'
  45. import VeForm from './VeForm'
  46. const Demo = {
  47. columns: [
  48. {
  49. index: 'user-name',
  50. label: '用户名',
  51. name: 'name',
  52. colWidth: 200,
  53. sortable: true,
  54. editable: true,
  55. editUrl: '/table/edit/name.json'
  56. }, {
  57. index: 'user-sex',
  58. label: '性别',
  59. name: 'sex',
  60. colWidth: 100,
  61. holder: 'switch',
  62. activeText: '男',
  63. inactiveText: '女',
  64. activeIconClass: 'el-icon-location',
  65. inactiveIconClass: 'el-icon-location-outline'
  66. }, {
  67. index: 'user-year',
  68. label: '年龄',
  69. name: 'year',
  70. colWidth: 100
  71. }, {
  72. index: 'user-depart',
  73. label: '部门',
  74. name: 'depart',
  75. colWidth: 200,
  76. holder: 'select',
  77. options: [
  78. {
  79. value: 1,
  80. label: '第一中学'
  81. },
  82. {
  83. value: 2,
  84. label: '第二中学'
  85. },
  86. {
  87. value: 3,
  88. label: '第三中学'
  89. },
  90. {
  91. value: 4,
  92. label: '第四中学'
  93. }
  94. ]
  95. }, {
  96. index: 'user-address',
  97. label: '地址',
  98. name: 'address',
  99. align: 'left'
  100. }
  101. ],
  102. btns: {
  103. items: [
  104. {
  105. icon: 'el-icon-edit',
  106. type: 'primary',
  107. label: '新建',
  108. next: '/form.json',
  109. size: 'medium'
  110. },
  111. {
  112. icon: 'el-icon-delete',
  113. url: '/delete.json',
  114. multiSelect: true,
  115. type: 'danger',
  116. label: '删除',
  117. size: 'medium'
  118. }
  119. ]
  120. },
  121. department: [
  122. {
  123. label: '名称',
  124. name: 'name',
  125. index: 'department-name'
  126. },
  127. {
  128. label: '状态',
  129. name: 'sex',
  130. index: 'department-sex'
  131. },
  132. {
  133. label: '简介',
  134. name: 'display',
  135. index: 'department-display',
  136. width: '200px'
  137. }
  138. ],
  139. actions: {
  140. items: [
  141. {
  142. icon: 'el-icon-edit',
  143. next: '/form.json',
  144. type: 'primary',
  145. label: '修改'
  146. },
  147. {
  148. icon: ['el-icon-location-outline', 'el-icon-location'],
  149. switch: 'sex',
  150. switchUrl: ['/sex.json?sex=1', '/sex.json?sex=0'],
  151. label: ['女', '男'],
  152. type: ['primary', 'denger']
  153. },
  154. {
  155. icon: 'el-icon-edit',
  156. url: '/status.json',
  157. type: 'success',
  158. label: '状态',
  159. disable: 'sex'
  160. }
  161. ]
  162. }
  163. }
  164. export default {
  165. components: {
  166. Pannel,
  167. VeTable,
  168. ElBtn,
  169. VeForm
  170. },
  171. // mixins: [Rank],
  172. data() {
  173. return {
  174. // 查询
  175. multipleSelection: [],
  176. searchValues: {},
  177. // 排序
  178. sort: {},
  179. // action: null,
  180. // 分页变量
  181. page: 1,
  182. pSize: 10,
  183. total: 0,
  184. // 搜索
  185. formInline: {
  186. user: '',
  187. region: ''
  188. },
  189. view: {
  190. name: 'TableView',
  191. columns: Demo.columns,
  192. search: {
  193. size: 'medium',
  194. inline: true,
  195. fields: [
  196. {
  197. index: 'user-name',
  198. label: '用户名',
  199. name: 'name'
  200. }
  201. ]
  202. },
  203. btns: Demo.btns,
  204. actions: Demo.actions,
  205. url: '/table/data.json'
  206. }
  207. }
  208. },
  209. computed: {
  210. params() {
  211. const self = this
  212. return {
  213. page: self.page,
  214. sort: self.sort,
  215. search: self.searchValues
  216. }
  217. },
  218. multiSelect() {
  219. return this.multipleSelection.length !== 0
  220. }
  221. },
  222. mounted() {
  223. this.getData()
  224. },
  225. methods: {
  226. handleButton(btn) {
  227. let value
  228. if (btn.multiSelect) value = { id: this.multipleSelection.map(r => r.id) }
  229. this.ajax(btn, value)
  230. },
  231. handleSelectionChange(val) {
  232. this.multipleSelection = val
  233. },
  234. search(form) {
  235. this.search = form
  236. this.getData()
  237. },
  238. /**
  239. * 相应翻页动作
  240. */
  241. handleCurrentChange() {
  242. this.getData()
  243. },
  244. /**
  245. * 排序动作
  246. */
  247. sortChange(sort) {
  248. if (!sort || !sort.column || !sort.column.sortable) return
  249. this.page = 1
  250. this.sort = { column: sort.column.property, order: sort.order }
  251. this.getData()
  252. },
  253. /**
  254. * 重新远程获取数据
  255. */
  256. getData() {
  257. // const url = this.view.url
  258. // this.load(url, this.params).then((data) => {
  259. // this.freshTable(data)
  260. // })
  261. },
  262. /**
  263. * 刷新数据
  264. */
  265. freshTable(data) {
  266. if (!data) return
  267. if (data.rows) this.rows = data.rows
  268. if (data.total) this.total = data.total
  269. this.$nextTick(() => {
  270. if (data.selected) this.$refs.table.rowSelect(data.selected)
  271. })
  272. }
  273. }
  274. }
  275. </script>
  276. <style scoped>
  277. .top {
  278. display: flex;
  279. flex-direction: row;
  280. align-items: center;
  281. justify-content: space-between;
  282. flex: 1;
  283. }
  284. .form-inline {
  285. margin-bottom: -18px;
  286. }
  287. </style>