ExTableNew.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <template>
  2. <div>
  3. <div class="table-header">
  4. <template v-for="(item, index) in table._defaultHeader_">
  5. <el-dropdown
  6. v-if="item == 'setcol'"
  7. placement="bottom-end"
  8. trigger="click"
  9. class="setcol-table-lie"
  10. :hide-on-click="false"
  11. :key="index"
  12. >
  13. <el-button size="small">列设置</el-button>
  14. <el-dropdown-menu slot="dropdown" :class="'ex-table-setcol-dropdown'">
  15. <template v-for="(column, index) in setColumns">
  16. <el-dropdown-item
  17. v-if="!column._noset_"
  18. :key="'setColumns' + index"
  19. >
  20. <el-switch
  21. v-model="column._hidden_"
  22. :active-text="column.label"
  23. :active-value="false"
  24. :inactive-value="true"
  25. ></el-switch>
  26. </el-dropdown-item>
  27. </template>
  28. </el-dropdown-menu>
  29. </el-dropdown>
  30. </template>
  31. <slot
  32. name="table-header"
  33. :selection="tableSelection"
  34. :alldata="data"
  35. ></slot>
  36. </div>
  37. <el-table
  38. ref="elTable"
  39. :data="data"
  40. v-bind="table"
  41. :size="size"
  42. v-on="$listeners"
  43. @selection-change="tableSelection = $event"
  44. :span-method="arraySpanMethod"
  45. style="width: 100%"
  46. >
  47. <template v-for="(column, index) in columns">
  48. <el-table-column
  49. align="center"
  50. v-if="!column._hidden_ && column.type && !column._slot_"
  51. v-bind="column"
  52. :key="index"
  53. width="45px"
  54. ></el-table-column>
  55. <el-table-column
  56. show-overflow-tooltip
  57. v-else-if="!column._hidden_"
  58. v-bind="column"
  59. :key="index"
  60. >
  61. <template slot="header" slot-scope="scope">
  62. <span>{{ column.label }}</span>
  63. <span
  64. v-if="column._screen_"
  65. @click.stop="addConditionItemByCol(column, scope)"
  66. >
  67. <i
  68. v-if="column._screen_tip_ === false"
  69. :style="isHasScreenColumn(column) ? 'color:#3888e5;' : ''"
  70. class="el-icon-connection"
  71. />
  72. <el-tooltip
  73. v-else
  74. :effect="column._screen_tip_effect_ || 'dark'"
  75. :content="column._screen_tip_ || '筛选'"
  76. :placement="column._screen_tip_placement_ || 'top'"
  77. >
  78. <i
  79. :style="isHasScreenColumn(column) ? 'color:#3888e5;' : ''"
  80. class="el-icon-connection"
  81. />
  82. </el-tooltip>
  83. </span>
  84. </template>
  85. <slot
  86. v-if="column._slot_"
  87. slot-scope="scope"
  88. :name="column._slot_"
  89. :scope="scope"
  90. ></slot>
  91. <template v-else-if="column._render_" slot-scope="scope">
  92. <ex-slot
  93. :column="column"
  94. :render="column._render_"
  95. :scope="scope"
  96. ></ex-slot>
  97. </template>
  98. <template v-else slot-scope="scope">
  99. <span>{{
  100. column._format_
  101. ? column._format_(scope.row)
  102. : getObjPrototype(scope.row, column.prop)
  103. }}</span>
  104. </template>
  105. </el-table-column>
  106. </template>
  107. </el-table>
  108. <!-- <el-pagination
  109. v-if="page !== false && data && data.length > 0"
  110. :current-page="page.curr"
  111. :page-sizes="[10, 20, 50, 100]"
  112. :total="page.total"
  113. layout="total, sizes, prev, pager, next, jumper"
  114. style="text-align: right"
  115. class="fu-page"
  116. @current-change="$emit('page-curr-change', $event)"
  117. @size-change="$emit('page-size-change', $event)"
  118. />
  119. -->
  120. <div
  121. v-if="page !== false && data && data.length > 0"
  122. class="Pagination"
  123. style="text-align: right; margin-top: 10px"
  124. >
  125. <el-pagination
  126. :current-page="page.curr"
  127. :page-sizes="[15, 50, 100]"
  128. :page-size="page.size"
  129. :size="searchSize"
  130. layout="total, sizes, prev, pager, next, jumper"
  131. :total="page.total"
  132. @size-change="$emit('page-size-change', $event)"
  133. @current-change="$emit('page-curr-change', $event)"
  134. />
  135. </div>
  136. </div>
  137. </template>
  138. <script>
  139. import Sortable from "sortablejs";
  140. export default {
  141. name: "ex-table",
  142. components: {
  143. "ex-slot": {
  144. functional: true,
  145. props: {
  146. render: Function,
  147. scope: {
  148. type: Object,
  149. default: null,
  150. },
  151. column: {
  152. type: Object,
  153. default: null,
  154. },
  155. },
  156. render: (h, data) => {
  157. if (data.props.column) data.props.scope.column = data.props.column;
  158. return data.props.render(h, data.props.scope);
  159. },
  160. },
  161. },
  162. props: {
  163. /**
  164. * el-table 属性集合
  165. * 自定义属性:
  166. * @param {Array} _defaultHeader_ : 默认的表格头部,支持['setcol', 'screen'] setcol-列设置 screen-筛选
  167. * 可使用插槽扩展表格头部:
  168. * table-header - 具名插槽
  169. * slotProps:selection - 表格选中的数据 alldata - 表格当前全部数据
  170. * <template #table-header="slotProps">
  171. * <el-button size="small" @click="click(slotProps)">扩展1</el-button>
  172. * </template>
  173. */
  174. table: {
  175. type: Object,
  176. default: () => {},
  177. },
  178. // el-table 表格数据 对应data字段
  179. data: {
  180. type: Array,
  181. default: () => [],
  182. },
  183. /**
  184. * el-table-column 表格列属性集合
  185. * 自定义属性:
  186. * @param {String} _filter_ : 是否不允许设置列
  187. * @param {Boolean} _noset_ : 是否不允许设置列
  188. * @param {Boolean} _screen_ : 是否允许筛选
  189. * @param {Boolean} _hidden_ : 是否隐藏列
  190. * @param {String/Boolean} _screen_tip_ : 筛选按钮提示信息,为false不提示
  191. * @param {String} _screen_tip_effect_ : 筛选按钮提示信息主题,dark/light
  192. * @param {String} _screen_tip_placement_: 筛选按钮提示信息位置,top/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-end
  193. * @param {Array} _keys_ : 支持的筛选key
  194. * @param {Array} _select_options_ : 若需支持包含select的key,value的备选数据,示例:{_select_options_: [{id:'', label:''},...]}
  195. * @param {Array} _select_options_end_ : 若需支持包含select的key,value_end的备选数据,默认使用_select_options_,示例:{_select_options_: [{id:'', label:''},...]}
  196. * @param {Array} _keys_ : 支持的筛选key
  197. * @param {String} _slot_ : 使用具名插槽时的名称
  198. * @param {Function} _format_ : 数据格式化函数,支持返回html
  199. * @param {Function} _render_ : 自定义列,采用vue中的render函数,示例: { _render_:(h, scope) => { return h(...) } }
  200. */
  201. columns: {
  202. type: Array,
  203. default: () => [],
  204. },
  205. /**
  206. * el-pagination
  207. * @param {Boolean} page 是否开启表格分页,若不传或传false则不开启
  208. * @param {Object} page {
  209. * size: '10'
  210. * total: '100'
  211. * curr: '1'
  212. * }
  213. * Event:
  214. * page-curr-change 分页当前页改变
  215. * page-size-change 分页大小改变
  216. */
  217. page: {
  218. type: [Boolean, Object],
  219. default: false,
  220. },
  221. /**
  222. * 筛选条件集合
  223. */
  224. conditions: {
  225. type: Array,
  226. default: () => [],
  227. },
  228. /**
  229. * 是否允许拖拽行
  230. */
  231. isRowDrop: {
  232. type: Boolean,
  233. default: false,
  234. },
  235. /**
  236. * 是否允许拖拽列
  237. */
  238. isColDrop: {
  239. type: Boolean,
  240. default: false,
  241. },
  242. /**
  243. * 表格大小
  244. */
  245. size: {
  246. type: String,
  247. default: "mini",
  248. },
  249. },
  250. computed: {
  251. // 允许筛选的列
  252. screenColumns() {
  253. return this.columns.filter((item) => item._screen_ === true);
  254. },
  255. // 允许设置的列
  256. setColumns() {
  257. return this.columns.filter((item) => !item._noset_);
  258. },
  259. // 筛选条件是否包含当前列
  260. isHasScreenColumn() {
  261. return function (col) {
  262. return this.conditions.some((item) => item.prop === col.prop);
  263. };
  264. },
  265. },
  266. data() {
  267. return {
  268. // 表格选中项
  269. tableSelection: [],
  270. };
  271. },
  272. computed: {
  273. page_curr() {
  274. return this.page.curr;
  275. },
  276. page_size() {
  277. return this.page.size;
  278. },
  279. page_total() {
  280. return this.page.total;
  281. },
  282. },
  283. watch: {
  284. page_curr: function (val) {
  285. this.page.curr = val;
  286. },
  287. page_size: function (val) {
  288. this.page.size = val;
  289. },
  290. page_total: function (val) {
  291. this.page.total = val;
  292. },
  293. },
  294. mounted() {
  295. // 拖拽绑定
  296. this.rowDrop();
  297. this.columnDrop();
  298. },
  299. directives: {
  300. // v-drap 可拖拽指令
  301. drap: {
  302. inserted: function (element, binding) {
  303. element.onmousedown = function (e) {
  304. let el = binding.modifiers.parent ? element.parentNode : element,
  305. bodyWidth = document.body.offsetWidth,
  306. bodyHeight = document.body.offsetHeight,
  307. elWidth = el.offsetWidth,
  308. elHeight = el.offsetHeight;
  309. // 开始拖动,记录左上角坐标点
  310. let disX = e.clientX - el.offsetLeft;
  311. let disY = e.clientY - el.offsetTop;
  312. document.onmousemove = function (e) {
  313. // 拖动中,修改dom的左上角坐标点
  314. let l = e.clientX - disX;
  315. let t = e.clientY - disY;
  316. if (binding.modifiers.limit) {
  317. l = l < 0 ? 0 : l;
  318. t = t < 0 ? 0 : t;
  319. l = l > bodyWidth - elWidth ? bodyWidth - elWidth : l;
  320. t = t > bodyHeight - elHeight ? bodyHeight - elHeight : t;
  321. }
  322. el.style.left = l + "px";
  323. el.style.top = t + "px";
  324. };
  325. document.onmouseup = function () {
  326. // 结束拖动
  327. document.onmousemove = null;
  328. };
  329. };
  330. },
  331. },
  332. },
  333. methods: {
  334. //
  335. arraySpanMethod({ row, column, rowIndex, columnIndex }) {
  336. // if (rowIndex % 2 === 0) {
  337. // if (columnIndex === 0) {
  338. // return [1, 2];
  339. // } else if (columnIndex === 1) {
  340. // return [0, 0];
  341. // }
  342. // }
  343. },
  344. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  345. if (columnIndex === 0) {
  346. if (rowIndex % 2 === 0) {
  347. return {
  348. rowspan: 2,
  349. colspan: 1,
  350. };
  351. } else {
  352. return {
  353. rowspan: 0,
  354. colspan: 0,
  355. };
  356. }
  357. }
  358. },
  359. //行拖拽
  360. rowDrop() {
  361. const tbody = document.querySelector(
  362. ".el-table__body-wrapper > table > tbody"
  363. );
  364. Sortable.create(tbody, {
  365. disabled: !this.isRowDrop,
  366. ghostClass: "sortable-ghost",
  367. animation: 180,
  368. delay: 0,
  369. onEndevt: (evt) => {
  370. const currItem = this.data.splice(evt.oldIndex, 1)[0];
  371. this.data.splice(evt.newIndex, 0, currItem);
  372. },
  373. });
  374. },
  375. //列拖拽
  376. columnDrop() {
  377. const wrapperTr = document.querySelector(".el-table__header-wrapper tr");
  378. Sortable.create(wrapperTr, {
  379. disabled: !this.isColDrop,
  380. ghostClass: "sortable-ghost",
  381. animation: 180,
  382. delay: 0,
  383. onEnd: (evt) => {
  384. const currItem = this.columns.splice(evt.oldIndex, 1)[0];
  385. this.columns.splice(evt.newIndex, 0, currItem);
  386. },
  387. });
  388. },
  389. // 获取对象的属性
  390. getObjPrototype(o, a) {
  391. let fn = Function;
  392. return new fn("obj", `return obj.${a}`)(o);
  393. },
  394. /** 表格方法 传递给el-table,后续有需要可继续增加 **/
  395. // 对 Table 进行重新布局。当 Table 或其祖先元素由隐藏切换为显示时,可能需要调用此方法
  396. doLayout(...arg) {
  397. this.$refs.elTable.doLayout(...arg);
  398. },
  399. // 用于多选表格,清空用户的选择
  400. clearSelection(...arg) {
  401. this.$refs.elTable.clearSelection(...arg);
  402. },
  403. // 用于多选表格,切换某一行的选中状态
  404. toggleRowSelection(...arg) {
  405. this.$refs.elTable.toggleRowSelection(...arg);
  406. },
  407. // 用于多选表格,切换所有行的选中状态
  408. toggleAllSelection(...arg) {
  409. this.$refs.elTable.toggleAllSelection(...arg);
  410. },
  411. // 用于单选表格,设定某一行为选中行
  412. setCurrentRow(...arg) {
  413. this.$refs.elTable.setCurrentRow(...arg);
  414. },
  415. // 用于可展开表格与树形表格,切换某一行的展开状态,如果使用了第二个参数,则是设置这一行展开与否(expanded 为 true 则展开)
  416. toggleRowExpansion(...arg) {
  417. this.$refs.elTable.toggleRowExpansion(...arg);
  418. },
  419. },
  420. };
  421. </script>
  422. <style lang="css">
  423. .el-switch__core {
  424. flex-shrink: 0;
  425. }
  426. .el-switch,
  427. .el-switch__label {
  428. height: auto;
  429. }
  430. .ex-table-setcol-dropdown {
  431. max-width: 180px;
  432. max-height: 280px;
  433. overflow-y: auto;
  434. }
  435. </style>
  436. <style lang="scss" scoped>
  437. .table-header {
  438. display: flex;
  439. position: relative;
  440. display: -webkit-flex;
  441. // background-color: #f5f5f5;
  442. padding: 0 0;
  443. .setcol-table-lie{
  444. position: absolute;
  445. top:0;
  446. left:0;
  447. z-index: 7;
  448. }
  449. }
  450. .table-header > * {
  451. margin-top: 10px;
  452. margin-bottom: 12px;
  453. }
  454. // .table-header :nth-child(n + 2) {
  455. // margin-left: 10px;
  456. // }
  457. </style>