index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <script>
  2. export default {
  3. name: "SuperUxTable",
  4. props: {
  5. // 数据
  6. value: {
  7. type: [Array],
  8. require: true,
  9. },
  10. // 字典
  11. dict: {
  12. type: [Object],
  13. require: true,
  14. },
  15. // 分页
  16. page: {
  17. type: [Object],
  18. require: false,
  19. },
  20. // 模板
  21. columns: {
  22. type: [Array],
  23. require: true,
  24. },
  25. // 是否显示序号
  26. index: {
  27. type: Boolean,
  28. default: false,
  29. },
  30. // 是否显示单选
  31. radio: {
  32. type: Boolean,
  33. default: false,
  34. },
  35. // 是否显示多选
  36. checkbox: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. // 是否显示分页
  41. pagination: {
  42. type: Boolean,
  43. default: false,
  44. },
  45. // 是否列操作
  46. convenitentOperation: {
  47. type: Boolean,
  48. default: false,
  49. },
  50. // 是否禁止选择
  51. selectable: {
  52. type: Function,
  53. default: () => {},
  54. },
  55. //
  56. storageKey: {
  57. type: String,
  58. },
  59. showSummary: {
  60. type: Boolean,
  61. default: false,
  62. },
  63. height: {
  64. type: [String, Number],
  65. require: false,
  66. },
  67. firstSummary: {
  68. type: Boolean,
  69. default: false,
  70. },
  71. },
  72. components: {
  73. ElDictTag: () => import("@/components/DictTag/index.vue"),
  74. ElDraggable: () => import("@/components/draggable/index.vue"),
  75. ElFilePreview: () => import("@/components/file-preview/index.vue"),
  76. ElComputedInput: () => import("@/components/computed-input/index.vue"),
  77. ElPopoverSelectV2: () => import("@/components/popover-select-v2/index.vue"),
  78. ElPopoverMultipleSelectV2: () =>
  79. import("@/components/popover-select-v2/multiple.vue"),
  80. ElComputedInputV2: () => import("@/components/computed-input-v2/index.vue"),
  81. ElPopoverTreeSelect: () =>
  82. import("@/components/popover-tree-select/index.vue"),
  83. ButtonHide: () => import("./hide.vue"),
  84. ButtonFreeze: () => import("./freeze.vue"),
  85. IconHide: () => import("./once/hide.vue"),
  86. IconSort: () => import("./once/sort.vue"),
  87. IconFreeze: () => import("./once/freeze.vue"),
  88. IconFilter: () => import("./once/filters.vue"),
  89. },
  90. data() {
  91. const { columns, storageKey } = this.$props;
  92. const localColumns = localStorage.getItem(storageKey);
  93. const innerColumns =
  94. storageKey && localColumns
  95. ? JSON.parse(localColumns)
  96. : columns.map(({ item, attr }) => ({
  97. attr,
  98. item: { hidden: true, ...item },
  99. }));
  100. return {
  101. innerColumns: innerColumns,
  102. rowKey: "id",
  103. // 选择
  104. selectData: [],
  105. selectState: false,
  106. // 过滤
  107. filterData: [],
  108. filterState: false,
  109. count: 0,
  110. scrollTop: 0,
  111. resizeHeight: 0,
  112. };
  113. },
  114. computed: {
  115. innerValue: {
  116. get() {
  117. if (this.filterState) {
  118. return this.filterData;
  119. } else if (this.selectState) {
  120. return this.selectData;
  121. } else {
  122. return this.$props.value;
  123. }
  124. },
  125. set(value) {
  126. this.$emit("input", value);
  127. },
  128. },
  129. showColumns: {
  130. get() {
  131. return this.innerColumns.filter(({ item }) => item.hidden);
  132. },
  133. set() {},
  134. },
  135. filterRules: {
  136. get() {
  137. return Object.fromEntries(
  138. this.innerColumns
  139. .filter(({ item }) => item.filter && !!item.filter.length)
  140. .map(({ item }) => [item.key, item.filter])
  141. );
  142. },
  143. set() {},
  144. },
  145. tableHeight: {
  146. get() {
  147. let { height } = this.$props;
  148. return height ? height : this.resizeHeight;
  149. },
  150. set() {},
  151. },
  152. },
  153. watch: {
  154. filterRules: {
  155. handler: function (newValue) {
  156. function multiFilter(array, filters) {
  157. const filterKeys = Object.keys(filters);
  158. // filters all elements passing the criteria
  159. return array.filter((item) => {
  160. // dynamically validate all filter criteria
  161. return filterKeys.every((key) => {
  162. //ignore when the filter is empty Anne
  163. if (!filters[key].length) return true;
  164. return !!~filters[key].indexOf(item[key]);
  165. });
  166. });
  167. }
  168. this.filterState = JSON.stringify(newValue) !== "{}";
  169. this.filterData = multiFilter(this.$props.value, newValue);
  170. },
  171. },
  172. value: {
  173. handler: function (newValue) {
  174. if (this.value.length > 0) {
  175. this.$refs.superUxTable && this.$refs.superUxTable.clearSelection();
  176. }
  177. },
  178. immediate: true,
  179. deep: true,
  180. },
  181. },
  182. directives: {
  183. // 使用局部注册指令的方式
  184. resize: {
  185. // 指令的名称
  186. bind(el, binding) {
  187. // el为绑定的元素,binding为绑定给指令的对象
  188. let width = "",
  189. height = "";
  190. function isReize() {
  191. const style = document.defaultView.getComputedStyle(el);
  192. if (width !== style.width || height !== style.height) {
  193. binding.value(); // 关键
  194. }
  195. width = style.width;
  196. height = style.height;
  197. }
  198. el.__vueSetInterval__ = setInterval(isReize, 300);
  199. },
  200. unbind(el) {
  201. clearInterval(el.__vueSetInterval__);
  202. },
  203. },
  204. },
  205. methods: {
  206. resize() {
  207. this.resizeHeight =
  208. document.getElementsByClassName("el-super-ux-table")[0].offsetHeight -
  209. 55;
  210. },
  211. //
  212. onSelectionChange(value) {
  213. this.selectData = value;
  214. this.$emit("row-select", this.selectData);
  215. },
  216. //
  217. onRowClick(row, column, event) {
  218. const { radio, checkbox } = this.$props;
  219. // 单选
  220. if (radio) {
  221. this.$emit("row-select", [row]);
  222. }
  223. // 多选
  224. if (checkbox) {
  225. this.$refs.superUxTable.toggleRowSelection([
  226. this.innerValue.find((item) => item.id === row.id),
  227. ]);
  228. }
  229. },
  230. // 宽度
  231. onWidth({ column }) {
  232. this.innerColumns = this.innerColumns.map(({ item, attr }) => ({
  233. attr,
  234. item: {
  235. ...item,
  236. width: item.key === column.property ? column.resizeWidth : item.width,
  237. },
  238. }));
  239. if (this.$props.storageKey) {
  240. localStorage.setItem(
  241. this.$props.storageKey,
  242. JSON.stringify(this.innerColumns)
  243. );
  244. }
  245. },
  246. // 隐藏
  247. onHide(prop) {
  248. this.$nextTick(() => {
  249. this.$refs.superUxTable.doLayout();
  250. if (this.$props.storageKey) {
  251. localStorage.setItem(
  252. this.$props.storageKey,
  253. JSON.stringify(this.innerColumns)
  254. );
  255. }
  256. });
  257. },
  258. // 排序
  259. onSort(prop) {
  260. const { key, sort } = prop;
  261. console.log(key, "key", sort, "sort");
  262. this.$nextTick(() => {
  263. this.$refs.superUxTable.sort(key, sort);
  264. this.$refs.superUxTable.doLayout();
  265. if (this.$props.storageKey) {
  266. localStorage.setItem(
  267. this.$props.storageKey,
  268. JSON.stringify(this.innerColumns)
  269. );
  270. }
  271. });
  272. },
  273. // 冻结
  274. onFreeze() {
  275. this.$nextTick(() => {
  276. this.$refs.superUxTable.doLayout();
  277. if (this.$props.storageKey) {
  278. localStorage.setItem(
  279. this.$props.storageKey,
  280. JSON.stringify(this.innerColumns)
  281. );
  282. }
  283. this.count++;
  284. });
  285. },
  286. // 过滤
  287. onFilter() {
  288. this.$nextTick(() => {
  289. this.$refs.superUxTable.doLayout();
  290. if (this.$props.storageKey) {
  291. localStorage.setItem(
  292. this.$props.storageKey,
  293. JSON.stringify(this.innerColumns)
  294. );
  295. }
  296. });
  297. },
  298. onFilters(value) {
  299. const {
  300. item: { key },
  301. attr: { dictName },
  302. } = value;
  303. let dataList = [];
  304. const dict = this.dict.type[dictName];
  305. dataList = Array.from(
  306. new Set(this.innerValue.map((item) => item[key]).filter((item) => item))
  307. ).map((item) => ({
  308. text: dictName
  309. ? (dict.find((dictItem) => dictItem.value == item) || {}).label
  310. : item,
  311. value: item,
  312. }));
  313. return dataList;
  314. },
  315. // 继承el-table的Method
  316. extendMethod() {
  317. const refMethod = Object.entries(this.$refs["superUxTable"]);
  318. for (const [key, value] of refMethod) {
  319. if (!(key.includes("$") || key.includes("_"))) {
  320. this[key] = value;
  321. }
  322. }
  323. },
  324. getSummaries({ columns, data }) {
  325. const means = []; // 合计
  326. let { firstSummary } = this.$props;
  327. columns.forEach((column, columnIndex) => {
  328. if (!firstSummary && columnIndex === 0) {
  329. means.push("合计");
  330. } else {
  331. const values = data.map((item) => Number(item[column.property]));
  332. let sumColumn = this.showColumns.filter(
  333. ({ item, attr }) => attr.isSummary && item.key === column.property
  334. );
  335. // 合计
  336. // if (!values.every(value => isNaN(value))) {
  337. if (sumColumn.length) {
  338. means[columnIndex] = values.reduce((prev, curr) => {
  339. const value = Number(curr);
  340. if (!isNaN(value)) {
  341. return prev + curr;
  342. } else {
  343. return prev;
  344. }
  345. }, 0);
  346. means[columnIndex] = means[columnIndex].toFixed(2);
  347. } else {
  348. means[columnIndex] = "";
  349. }
  350. }
  351. });
  352. // sums[index] = sums[index] && sums[index].toFixed(2); // 保留2位小数,解决小数合计列
  353. return [means];
  354. },
  355. },
  356. created() {},
  357. mounted() {
  358. this.extendMethod();
  359. },
  360. updated() {
  361. this.$nextTick(() => {
  362. this.$refs.superUxTable.doLayout();
  363. });
  364. },
  365. destroyed() {},
  366. };
  367. </script>
  368. <template>
  369. <div class="el-super-ux-table" :key="count" v-resize="resize">
  370. <ux-grid
  371. border
  372. row-key
  373. use-virtual
  374. keep-source
  375. show-overflow
  376. beautify-table
  377. ref="superUxTable"
  378. v-bind="$attrs"
  379. :height="tableHeight"
  380. v-on="$listeners"
  381. :data="innerValue"
  382. :show-summary="showSummary"
  383. :summary-method="getSummaries"
  384. @row-click="onRowClick"
  385. @header-dragend="onWidth"
  386. @selection-change="onSelectionChange"
  387. :header-row-style="{
  388. color: '#515a6e',
  389. }"
  390. style="flex: 1"
  391. >
  392. <!-- 多选 -->
  393. <ux-table-column
  394. v-if="checkbox"
  395. fixed="left"
  396. width="50"
  397. align="center"
  398. type="checkbox"
  399. resizable
  400. reserve-selection
  401. :column-key="rowKey"
  402. ></ux-table-column>
  403. <!-- 序号 -->
  404. <ux-table-column
  405. v-if="index"
  406. fixed="left"
  407. width="50"
  408. title="序号"
  409. type="index"
  410. align="center"
  411. class="is-index"
  412. resizable
  413. ></ux-table-column>
  414. <ux-table-column
  415. v-for="({ item, attr }, index) in showColumns"
  416. :key="item.key + index"
  417. :field="item.key"
  418. :title="item.title"
  419. :fixed="item.fixed ? 'left' : undefined"
  420. :width="item.width || 180"
  421. :sortable="item.sortabled"
  422. resizable
  423. show-overflow
  424. >
  425. <template slot="header" slot-scope="scope">
  426. <template>
  427. <span v-if="item.require" style="color: #ff4949">*</span>
  428. <span
  429. :style="{
  430. color:
  431. item.sort ||
  432. item.fixed ||
  433. (item.filter && !!item.filter.length)
  434. ? '#1890ff'
  435. : '',
  436. }"
  437. >
  438. {{ item.title }}
  439. </span>
  440. <template>
  441. <!-- <icon-sort
  442. v-if="item.sortabled"
  443. v-model="item.sort"
  444. @sort="onSort(item)"
  445. ></icon-sort> -->
  446. <icon-freeze
  447. v-if="item.fixedabled"
  448. v-model="item.fixed"
  449. @freeze="onFreeze"
  450. ></icon-freeze>
  451. <icon-filter
  452. v-if="item.filterabled"
  453. v-model="item.filter"
  454. :filters="onFilters({ item, attr })"
  455. @filter="onFilter"
  456. ></icon-filter>
  457. <icon-hide
  458. v-if="item.hiddenabled"
  459. v-model="item.hidden"
  460. @hide="onHide"
  461. ></icon-hide>
  462. </template>
  463. </template>
  464. </template>
  465. <template slot-scope="scope">
  466. <slot :name="item.key" v-bind="scope" :item="item" :attr="attr">
  467. <template v-if="attr.is">
  468. <component
  469. v-if="attr.is === 'el-dict-tag'"
  470. v-bind="attr"
  471. :size="$attrs.size"
  472. :value="scope.row[item.key]"
  473. :options="dict.type[attr.dictName]"
  474. ></component>
  475. <component
  476. v-else-if="attr.is === 'el-popover-select-v2'"
  477. v-bind="attr"
  478. v-model="scope.row[item.key]"
  479. :title="item.title"
  480. :size="$attrs.size"
  481. :source.sync="scope.row"
  482. >
  483. </component>
  484. <component
  485. v-else-if="attr.is === 'el-popover-multiple-select-v2'"
  486. v-bind="attr"
  487. v-model="scope.row[item.key]"
  488. :title="item.title"
  489. :size="$attrs.size"
  490. :source.sync="scope.row"
  491. >
  492. </component>
  493. <component
  494. v-else-if="attr.is === 'el-select'"
  495. v-bind="attr"
  496. v-model="scope.row[item.key]"
  497. :size="$attrs.size"
  498. >
  499. <template>
  500. <el-option
  501. v-for="item in dict.type[attr.dictName]"
  502. :key="item.value"
  503. :label="item.label"
  504. :value="item.value"
  505. >
  506. </el-option>
  507. </template>
  508. </component>
  509. <component
  510. v-else
  511. v-bind="attr"
  512. v-model="scope.row[item.key]"
  513. :size="$attrs.size"
  514. style="width: 100%"
  515. >
  516. </component
  517. ></template>
  518. <template v-else>
  519. <component v-if="attr.formatter" is="span">{{
  520. attr.formatter(scope.row)
  521. }}</component>
  522. <component v-else is="span">{{
  523. scope.row[item.key] || "--"
  524. }}</component>
  525. </template>
  526. </slot>
  527. </template>
  528. </ux-table-column>
  529. <slot></slot>
  530. <!-- </el-table> -->
  531. </ux-grid>
  532. <div
  533. style="
  534. height: 50px;
  535. display: flex;
  536. justify-content: space-between;
  537. align-items: center;
  538. "
  539. :style="{
  540. height: checkbox || pagination ? '50px' : '0px',
  541. }"
  542. >
  543. <div class="mr-4">
  544. <template v-if="convenitentOperation">
  545. <button-hide v-model="innerColumns" @change="onHide"></button-hide>
  546. </template>
  547. </div>
  548. <pagination
  549. v-if="pagination"
  550. v-show="!selectState"
  551. :total="page.total"
  552. :page.sync="page.pageNum"
  553. :limit.sync="page.pageSize"
  554. @pagination="$emit('pagination', { ...$event })"
  555. style="height: 32px; padding: 0 !important; flex: 1; overflow-x: auto"
  556. />
  557. </div>
  558. </div>
  559. </template>
  560. <style lang="scss" scoped>
  561. .el-super-ux-table {
  562. position: relative;
  563. display: flex;
  564. flex: 1;
  565. flex-direction: column;
  566. overflow: auto;
  567. }
  568. ::v-deep.el-super-ux-table .elx-cell {
  569. word-break: keep-all;
  570. white-space: nowrap;
  571. .icon-sort {
  572. display: none;
  573. }
  574. &:hover .icon-sort {
  575. display: inline-block;
  576. }
  577. .icon-freeze {
  578. display: none;
  579. }
  580. &:hover .icon-freeze {
  581. display: inline-block;
  582. }
  583. .icon-filter {
  584. display: none;
  585. }
  586. &:hover .icon-filter {
  587. display: inline-block;
  588. }
  589. .icon-hide {
  590. display: none;
  591. }
  592. &:hover .icon-hide {
  593. display: inline-block;
  594. }
  595. .elx-cell--sort {
  596. display: none;
  597. }
  598. &:hover .elx-cell--sort {
  599. display: inline-block;
  600. }
  601. }
  602. ::v-deep.uxbeautifyTableClass
  603. .elx-header--column
  604. .elx-resizable.is--line:before {
  605. height: 100%;
  606. background-color: #dfe6ec;
  607. }
  608. </style>