index.vue 15 KB

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