index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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-tableMethod
  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(({item,attr}) => attr.isSummary && item.key === column.property);
  297. // 合计
  298. // if (!values.every(value => isNaN(value))) {
  299. if (sumColumn.length) {
  300. means[columnIndex] = values.reduce((prev, curr) => {
  301. const value = Number(curr);
  302. if (!isNaN(value)) {
  303. return prev + curr;
  304. } else {
  305. return prev;
  306. }
  307. }, 0);
  308. means[columnIndex] = means[columnIndex].toFixed(2);
  309. } else {
  310. means[columnIndex] = '';
  311. }
  312. }
  313. })
  314. // sums[index] = sums[index] && sums[index].toFixed(2); // 保留2位小数,解决小数合计列
  315. return [means]
  316. },
  317. },
  318. created() {},
  319. mounted() {
  320. this.extendMethod();
  321. },
  322. updated() {
  323. this.$nextTick(()=>{
  324. this.$refs.superUxTable.doLayout();
  325. })
  326. },
  327. destroyed() {},
  328. };
  329. </script>
  330. <template>
  331. <div class="el-super-ux-table" :key="count">
  332. <ux-grid
  333. border
  334. row-key
  335. use-virtual
  336. keep-source
  337. show-overflow
  338. beautify-table
  339. :height="height"
  340. ref="superUxTable"
  341. v-bind="$attrs"
  342. v-on="$listeners"
  343. :data="innerValue"
  344. :show-summary="showSummary"
  345. :summary-method="getSummaries"
  346. highlight-current-row
  347. @row-click="onRowClick"
  348. @header-dragend="onWidth"
  349. @selection-change="onSelectionChange"
  350. :header-row-style="{
  351. color: '#515a6e' ,
  352. }"
  353. style="flex: 1"
  354. >
  355. <!-- 多选 -->
  356. <ux-table-column
  357. v-if="checkbox"
  358. fixed="left"
  359. width="60"
  360. align="center"
  361. type="checkbox"
  362. resizable
  363. reserve-selection
  364. :column-key="rowKey"
  365. >
  366. </ux-table-column>
  367. <!-- 序号 -->
  368. <ux-table-column
  369. v-if="index"
  370. fixed="left"
  371. width="50"
  372. title="序号"
  373. type="index"
  374. align="center"
  375. class="is-index"
  376. resizable
  377. >
  378. </ux-table-column>
  379. <ux-table-column
  380. v-for="({ item, attr }, index) in showColumns"
  381. :key="item.key + index"
  382. :field="item.key"
  383. :title="item.title"
  384. :fixed="item.fixed ? 'left' :undefined"
  385. :width="item.width || 180"
  386. resizable
  387. show-overflow
  388. >
  389. <template slot="header" slot-scope="scope">
  390. <template>
  391. <span v-if="item.require" style="color: #ff4949">*</span>
  392. <span
  393. :style="{
  394. color:
  395. item.sort ||
  396. item.fixed ||
  397. (item.filter && !!item.filter.length)
  398. ? '#1890ff'
  399. : '',
  400. }"
  401. >
  402. {{ item.title }}
  403. </span>
  404. <template>
  405. <icon-sort
  406. v-if="item.sortabled"
  407. v-model="item.sort"
  408. @sort="onSort(item)"
  409. ></icon-sort>
  410. <icon-freeze
  411. v-if="item.fixedabled"
  412. v-model="item.fixed"
  413. @freeze="onFreeze"
  414. ></icon-freeze>
  415. <icon-filter
  416. v-if="item.filterabled"
  417. v-model="item.filter"
  418. :filters="onFilters({ item, attr })"
  419. @filter="onFilter"
  420. ></icon-filter>
  421. <icon-hide
  422. v-if="item.hiddenabled"
  423. v-model="item.hidden"
  424. @hide="onHide"
  425. ></icon-hide>
  426. </template>
  427. </template>
  428. </template>
  429. <template slot-scope="scope">
  430. <slot :name="item.key" v-bind="scope" :item="item" :attr="attr">
  431. <template v-if="attr.is">
  432. <component
  433. v-if="attr.is === 'el-dict-tag'"
  434. v-bind="attr"
  435. :size="$attrs.size"
  436. :value="scope.row[item.key]"
  437. :options="dict.type[attr.dictName]"
  438. ></component>
  439. <component
  440. v-else-if="attr.is === 'el-popover-select-v2'"
  441. v-bind="attr"
  442. v-model="scope.row[item.key]"
  443. :title="item.title"
  444. :size="$attrs.size"
  445. :source.sync="scope.row"
  446. >
  447. </component>
  448. <component
  449. v-else-if="attr.is === 'el-popover-multiple-select-v2'"
  450. v-bind="attr"
  451. v-model="scope.row[item.key]"
  452. :title="item.title"
  453. :size="$attrs.size"
  454. :source.sync="scope.row"
  455. >
  456. </component>
  457. <component
  458. v-else-if="attr.is === 'el-select'"
  459. v-bind="attr"
  460. v-model="scope.row[item.key]"
  461. :size="$attrs.size"
  462. >
  463. <template>
  464. <el-option
  465. v-for="item in dict.type[attr.dictName]"
  466. :key="item.value"
  467. :label="item.label"
  468. :value="item.value"
  469. >
  470. </el-option>
  471. </template>
  472. </component>
  473. <component
  474. v-else
  475. v-bind="attr"
  476. v-model="scope.row[item.key]"
  477. :size="$attrs.size"
  478. style="width: 100%"
  479. >
  480. </component
  481. ></template>
  482. <template v-else>
  483. <component v-if="attr.formatter" is="span">{{
  484. attr.formatter(scope.row)
  485. }}</component>
  486. <component v-else is="span">{{
  487. scope.row[item.key] || "--"
  488. }}</component>
  489. </template>
  490. </slot>
  491. </template>
  492. </ux-table-column>
  493. <slot></slot>
  494. <!-- </el-table> -->
  495. </ux-grid>
  496. <div
  497. style="
  498. height: 50px;
  499. display: flex;
  500. justify-content: space-between;
  501. align-items: center;
  502. "
  503. :style="{
  504. height: checkbox || pagination ? '50px' : '0px',
  505. }"
  506. >
  507. <div class="mr-4">
  508. <template v-if="convenitentOperation">
  509. <button-hide v-model="innerColumns" @change="onHide"></button-hide>
  510. </template>
  511. </div>
  512. <pagination
  513. v-if="pagination"
  514. v-show="!selectState"
  515. :total="page.total"
  516. :page.sync="page.pageNum"
  517. :limit.sync="page.pageSize"
  518. @pagination="$emit('pagination', { ...$event })"
  519. style="height: 32px; padding: 0 !important; flex: 1; overflow-x: auto"
  520. />
  521. </div>
  522. </div>
  523. </template>
  524. <style lang="scss" scoped>
  525. .el-super-ux-table {
  526. position: relative;
  527. display: flex;
  528. flex: 1;
  529. flex-direction: column;
  530. overflow: auto;
  531. }
  532. ::v-deep.el-super-ux-table .elx-cell {
  533. word-break: keep-all;
  534. white-space: nowrap;
  535. .icon-sort {
  536. display: none;
  537. }
  538. &:hover .icon-sort {
  539. display: inline-block;
  540. }
  541. .icon-freeze {
  542. display: none;
  543. }
  544. &:hover .icon-freeze {
  545. display: inline-block;
  546. }
  547. .icon-filter {
  548. display: none;
  549. }
  550. &:hover .icon-filter {
  551. display: inline-block;
  552. }
  553. .icon-hide {
  554. display: none;
  555. }
  556. &:hover .icon-hide {
  557. display: inline-block;
  558. }
  559. }
  560. ::v-deep.uxbeautifyTableClass .elx-header--column .elx-resizable.is--line:before{
  561. height: 100%;
  562. background-color: #dfe6ec;
  563. }
  564. </style>