table.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <!--
  2. * @Author: zzy6937@qq.com
  3. * @Date: 2019-07-01 09:14:32
  4. * @LastEditors: wf
  5. * @LastEditTime: 2021-09-22 21:24:17
  6. * @Description:
  7. -->
  8. <template>
  9. <div class="zz-table">
  10. <!-- :height='isScroll ? boxHeight : null' -->
  11. <el-table
  12. :border="settings.tableBorder"
  13. :height="height"
  14. style="width: 100%"
  15. ref="table"
  16. v-loading="loading"
  17. :data="data"
  18. :summary-method="getSummaries"
  19. :show-summary="!!(settings.summaryCol && settings.summaryCol.length)"
  20. tooltip-effect="light"
  21. size="small"
  22. :key="updateFlag"
  23. @selection-change="selectionChange"
  24. row-key="id"
  25. :expand-row-keys="expands"
  26. @expand-change="toggleRow"
  27. @sort-change="sortChange"
  28. :default-sort="settings.defaultSort"
  29. :empty-text="emptyText"
  30. :show-header="settings.showHeader"
  31. :row-class-name="setRowClassName"
  32. lazy
  33. :load="load"
  34. :tree-props="treeProps"
  35. class="customer-table"
  36. >
  37. <!-- 显示多选框 -->
  38. <el-table-column
  39. type="selection"
  40. width="60"
  41. align="center"
  42. v-if="settings.showCheckbox"
  43. :selectable="selectable"
  44. ></el-table-column>
  45. <!-- 显示单选框 -->
  46. <el-table-column width="50" align="left" v-if="settings.showRadio">
  47. <template slot-scope="scope">
  48. <el-radio-group v-model="radioSelect">
  49. <el-radio :label="scope.$index"></el-radio>
  50. </el-radio-group>
  51. </template>
  52. </el-table-column>
  53. <!-- 显示序号 -->
  54. <el-table-column label="No." type="index" :width="70" align="left" v-if="settings.showIndex"></el-table-column>
  55. <el-table-column
  56. label="NO."
  57. type="index"
  58. :width="settings.showNumberWidth"
  59. align="left"
  60. v-if="settings.showNumber"
  61. ></el-table-column>
  62. <!-- 显示具体行 -->
  63. <el-table-column
  64. v-for="(item, index) in cols"
  65. :fixed="item.fixed"
  66. :width="item.width"
  67. :min-width="item.minWidth"
  68. :prop="item.prop"
  69. :label="item.label"
  70. :key="index"
  71. :type="item.type"
  72. align="left"
  73. :sortable="item.sort || false"
  74. filter-placement="bottom"
  75. :show-overflow-tooltip="!isIE"
  76. >
  77. <template slot="header" slot-scope="scope">
  78. <slot v-if="item.headerSlot" :name="item.headerSlot" :data="scope"></slot>
  79. <span v-else>{{ item.label }}</span>
  80. </template>
  81. <template slot-scope="scope">
  82. <!-- 插槽 -->
  83. <slotItem v-if="item.slot" :row="scope.row" :index="scope.$index" :slots="$scopedSlots[item.slot]"></slotItem>
  84. <!-- 过滤方法 -->
  85. <el-tooltip
  86. v-else-if="item.format && isIE"
  87. class="item"
  88. effect="light"
  89. placement="bottom"
  90. :disabled="item.format(scope.row[item.prop]).length < 2"
  91. :content="item.format(scope.row[item.prop])"
  92. >
  93. <p
  94. @click="item.click ? item.click() : null"
  95. v-html="
  96. item.format(scope.row[item.prop], scope.row) ||
  97. (item.format(scope.row[item.prop], scope.row) === 0 ? 0 : item.empty ? '' : '-')
  98. "
  99. ></p>
  100. </el-tooltip>
  101. <p
  102. v-else-if="item.format && !isIE"
  103. @click="item.click ? item.click() : null"
  104. v-html="item.format(scope.row[item.prop]) || (item.format(scope.row[item.prop]) == 0 ? 0 : '-')"
  105. ></p>
  106. <!-- <p
  107. v-else-if="item.format && isIE"
  108. class="item" effect="light" placement="top-start"
  109. @click="item.click ? item.click() : null"
  110. >
  111. {{
  112. item.format(scope.row[item.prop], scope.row) ||
  113. (item.format(scope.row[item.prop], scope.row) === 0 ? 0 : item.empty ? '' : '-')
  114. }}
  115. </p> -->
  116. <!-- <p v-else-if="item.format" @click='item.click?item.click():null'>{{item.format(scope.row[item.prop]) || (item.format(scope.row[item.prop]) == 0 ? 0 : '-')}}</p> -->
  117. <!-- 默认显示 -->
  118. <el-tooltip
  119. v-else-if="!item.slot && !item.format && isIE"
  120. class="item"
  121. effect="light"
  122. placement="bottom"
  123. :disabled="scope.row[item.prop] && scope.row[item.prop].length < 2"
  124. :content="scope.row[item.prop]"
  125. >
  126. <p
  127. @click="item.click ? item.click() : null"
  128. v-html="scope.row[item.prop] || (scope.row[item.prop] === 0 ? 0 : item.empty ? '' : '-')"
  129. ></p>
  130. </el-tooltip>
  131. <p
  132. v-else
  133. @click="item.click ? item.click() : null"
  134. v-html="scope.row[item.prop] || (scope.row[item.prop] === 0 ? 0 : item.empty ? '' : '-')"
  135. ></p>
  136. <!-- <p v-else-if="item.fixToolTip" v-tip.same :data-txt="scope.row[item.prop]" @click="item.click ? item.click() : null">
  137. {{ scope.row[item.prop] || (scope.row[item.prop] === 0 ? 0 : item.empty ? '' : '-') }}
  138. </p> -->
  139. <!-- <p v-else v-tip :data-txt="scope.row[item.prop]" @click="item.click ? item.click() : null">
  140. {{ scope.row[item.prop] || (scope.row[item.prop] === 0 ? 0 : item.empty ? '' : '-') }}
  141. </p> -->
  142. </template>
  143. </el-table-column>
  144. </el-table>
  145. <div class="foot" v-if="!settings.hideFoot">
  146. <!-- 手动全选及操作 -->
  147. <!-- <div v-if="false" class="selectAll" :class="{ hasIndex: settings.showIndex }">
  148. <el-checkbox v-if="checkHide()" v-model="isSelectAll" @change="checkAllChange"></el-checkbox>
  149. <el-tooltip class="item" effect="light" content="展示/隐藏" placement="bottom">
  150. <span :class="checkHide()?'batch_display':'batch_collapse'" class="batch_button" @click="hideordisplay"></span>
  151. </el-tooltip>
  152. <slot v-if="checkHide()" name="batchtodo"></slot>
  153. </div> -->
  154. <el-pagination
  155. v-if="pageset.hasOwnProperty('total') && !settings.hidePagination"
  156. class="pagination"
  157. background
  158. @size-change="sizeChange"
  159. @current-change="currentChange"
  160. :current-page="currentPage"
  161. :page-sizes="pageset.pageSizes || [15, 30, 60, 120]"
  162. :page-size="pageset.pageSize || 15"
  163. :total="pageset.total || 0"
  164. layout="total, sizes, prev, pager, next, jumper"
  165. >
  166. </el-pagination>
  167. </div>
  168. </div>
  169. </template>
  170. <script>
  171. export default {
  172. name: 'zz-table',
  173. props: {
  174. /*
  175. table例设置
  176. [{
  177. label: 例名. 必须. String
  178. prop: 数据字段. 必须. String
  179. fixed: 当前例是否固定. 值有left,right。 String
  180. width: 例宽. Number
  181. slot: 插槽方式, String
  182. format: 数据过滤方法, Function
  183. click: 当前内容的点击事件, Function
  184. }]
  185. */
  186. cols: {
  187. type: Array,
  188. default() {
  189. return [];
  190. }
  191. },
  192. /*
  193. table数据
  194. */
  195. data: {
  196. type: Array,
  197. default() {
  198. return [];
  199. }
  200. },
  201. /*
  202. table表格设置
  203. */
  204. settings: {
  205. type: Object,
  206. default() {
  207. return {
  208. // 是否显示表头
  209. showHeader: true,
  210. // 是否显示序号
  211. showIndex: false,
  212. // 是否显示序列号大写
  213. showNUmber: false,
  214. // 序列号宽度
  215. showNumberWidth: '70',
  216. // 是否显示多选框
  217. showCheckbox: false,
  218. hideAllCheckbox: false,
  219. // 是否显示单选框
  220. showRadio: false,
  221. // 需要统计列的prop集合,从0开始
  222. summaryCol: [],
  223. // 是否显示边框
  224. tableBorder: false
  225. };
  226. }
  227. },
  228. /*
  229. 分页设置
  230. */
  231. pageset: {
  232. type: Object,
  233. default() {
  234. return {
  235. total: 0,
  236. pageSize: 10,
  237. pageNum: 1,
  238. pageSizes: [15, 30, 60, 120]
  239. };
  240. }
  241. },
  242. loading: {
  243. type: Boolean,
  244. default() {
  245. return false;
  246. }
  247. },
  248. height: 0,
  249. updateFlag: false, //触发表格重绘
  250. // 统计数据.传入统计数据则显示该统计数据,否则显示现有列表数据
  251. summaryData: {},
  252. // 判断多选框是否可以勾选
  253. selectable: {
  254. type: Function,
  255. default() {
  256. return (row, index) => {
  257. return true;
  258. };
  259. }
  260. },
  261. expands: {
  262. type: Array,
  263. default() {
  264. return [];
  265. }
  266. },
  267. treeProps: {
  268. type: Object,
  269. default() {
  270. return {};
  271. }
  272. },
  273. load: {
  274. type: Function,
  275. default() {
  276. return () => {};
  277. }
  278. },
  279. emptyText: {
  280. type: String,
  281. default() {
  282. return '暂无相关结果';
  283. }
  284. }
  285. },
  286. data() {
  287. return {
  288. isIE: false,
  289. radioSelect: '',
  290. boxHeight: null,
  291. currentPage: 1,
  292. isSelectAll: false,
  293. isScroll: false,
  294. hideornot: true
  295. };
  296. },
  297. methods: {
  298. checkHide() {
  299. if (this.settings.showCheckbox && this.hideornot) {
  300. return true;
  301. } else {
  302. return false;
  303. }
  304. },
  305. hideordisplay() {
  306. this.hideornot = !this.hideornot;
  307. },
  308. setRadioIndex(index) {
  309. this.radioSelect = index;
  310. },
  311. sortChange(column, prop, order) {
  312. //点击排序按钮后拿到column.order,可以发送column.order给后台,后台再根据什么排序来返回排序过后的数据
  313. // console.log(column + '---' + column.prop + '---' + column.order)
  314. const data = JSON.parse(JSON.stringify(column));
  315. data.prop = (data.prop && data.prop.replace(/([A-Z])/g, '_$1').toLowerCase()) || null;
  316. if (this.settings.defaultSort && this.settings.defaultSort.propExtend) {
  317. data.prop = `${data.prop}${this.settings.defaultSort.propExtend}`;
  318. }
  319. data.order = data.order == 'ascending' ? 'ASC' : column.order == 'descending' ? 'DESC' : null;
  320. this.$emit('sort-change', data);
  321. },
  322. toggleRow(row, expandedRows) {
  323. // this.expands = [];
  324. const index = _.findIndex(expandedRows, (v) => v.id == row.id);
  325. if (index != -1) {
  326. this.expands.push(row.id);
  327. this.$emit('toggle-change', row);
  328. } else {
  329. this.expands = _.filter(this.expands, (v) => v != row.id);
  330. }
  331. },
  332. // 多选派发事件selection-change
  333. selectionChange(val) {
  334. this.isSelectAll = val.length == this.data.length;
  335. this.$emit('selection-change', val);
  336. },
  337. /*
  338. 页数改变时,派发pageChange事件,并传递当前分页参数;
  339. 其中pageSize为每页显示多少条数据,page为当前显示多少页
  340. */
  341. sizeChange(pageSize) {
  342. this.$emit('page-change', {
  343. pageSize: pageSize
  344. });
  345. },
  346. currentChange(page) {
  347. this.currentPage = page;
  348. this.$emit('page-change', {
  349. page: page
  350. });
  351. },
  352. // 合计
  353. getSummaries(params) {
  354. let { data } = params;
  355. if ('{}' != JSON.stringify(this.summaryData)) {
  356. data = [this.summaryData];
  357. }
  358. let sums = [];
  359. sums[0] = '合计';
  360. let cols = JSON.parse(JSON.stringify(this.settings.summaryCol));
  361. let i = cols.length;
  362. while (i--) {
  363. // 计算当前列的prop字段
  364. let index = cols[i];
  365. if (this.settings.showIndex) {
  366. index -= 1;
  367. }
  368. if (this.settings.showCheckbox) {
  369. index -= 1;
  370. }
  371. if (this.settings.showRadio) {
  372. index -= 1;
  373. }
  374. let prop = this.cols[index].prop;
  375. const values = data.map((item) => Number(item[prop]));
  376. if (!values.every((value) => isNaN(value))) {
  377. sums[cols[i]] = values.reduce((prev, curr) => {
  378. const value = Number(curr);
  379. if (!isNaN(value)) {
  380. let sumVa = Number(prev) + curr;
  381. return sumVa.toFixed(2);
  382. } else {
  383. return Number(prev.toFixed(2));
  384. }
  385. }, 0);
  386. sums[cols[i]];
  387. }
  388. }
  389. return sums;
  390. },
  391. checkAllChange(val) {
  392. if (val) {
  393. this.$refs.table.toggleAllSelection();
  394. } else {
  395. this.$refs.table.clearSelection();
  396. }
  397. },
  398. //自定义表头斑马线
  399. setRowClassName({ row, rowIndex }) {
  400. let index = rowIndex;
  401. if (index % 2 == 0) {
  402. return 'warning-row';
  403. }
  404. }
  405. },
  406. watch: {
  407. radioSelect(n) {
  408. this.$emit('radio-change', this.data[n]);
  409. },
  410. pageset: {
  411. deep: true,
  412. handler(n) {
  413. this.currentPage = n.pageNum;
  414. }
  415. }
  416. },
  417. components: {
  418. slotItem: {
  419. props: ['slots', 'row', 'index'],
  420. render(h) {
  421. let vnode = this.slots({
  422. row: this.row,
  423. index: this.index
  424. });
  425. return h('div', vnode);
  426. }
  427. }
  428. },
  429. created() {
  430. const userAgent = window.navigator.userAgent;
  431. const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1; //判断是否IE<11浏览器
  432. const isEdge = userAgent.indexOf('Edge') > -1 && !isIE; //判断是否IE的Edge浏览器
  433. const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;
  434. this.isIE = isIE11 || isEdge || isIE;
  435. }
  436. };
  437. </script>
  438. <style lang="scss" scoped>
  439. @import '@assets/css/public-style.scss';
  440. .zz-table .el-table {
  441. border-radius: 6px;
  442. /deep/ td {
  443. .opt {
  444. display: flex;
  445. align-items: center;
  446. cursor: pointer;
  447. i {
  448. margin-right: 20px;
  449. width: 14px;
  450. height: 14px;
  451. line-height: 14px;
  452. &:last-child {
  453. margin-right: 0;
  454. }
  455. }
  456. }
  457. }
  458. /deep/ .el-table-column--selection .cell {
  459. padding-left: 0 !important;
  460. padding-right: 10px !important;
  461. }
  462. /deep/ .el-table__header .el-table-column--selection {
  463. .cell {
  464. padding-left: 0;
  465. }
  466. }
  467. }
  468. .foot {
  469. display: flex;
  470. flex-direction: row;
  471. align-items: center;
  472. justify-content: flex-end;
  473. .selectAll {
  474. display: flex;
  475. align-items: center;
  476. justify-content: flex-start;
  477. .el-checkbox {
  478. text-align: center;
  479. width: 40px;
  480. margin-right: 10px;
  481. }
  482. //table下面foot的按钮样式
  483. .el-button.is-disabled {
  484. border: none;
  485. background: #ffffff !important;
  486. border: 1px solid #a6abb6;
  487. opacity: 0.5;
  488. border-radius: 4px;
  489. & span {
  490. font-size: 12px;
  491. font-family: Microsoft YaHei;
  492. font-weight: 400;
  493. line-height: 16px;
  494. color: #b6b1b1 !important;
  495. opacity: 1;
  496. }
  497. }
  498. .el-button--primary.batch_btn {
  499. background-color: #ffffff;
  500. border: 1px solid #a6abb6;
  501. opacity: 1;
  502. border-radius: 4px;
  503. color: #7b7f86;
  504. &:hover {
  505. border-color: $mainTextColor !important;
  506. span {
  507. color: $mainTextColor !important;
  508. }
  509. }
  510. &.is-disabled {
  511. color: #7b7f86ad;
  512. border: 0.01rem solid #a6abb66b;
  513. &:hover {
  514. color: #7b7f86ad;
  515. border-color: #a6abb66b !important;
  516. }
  517. }
  518. span {
  519. font-size: 12px;
  520. font-family: Microsoft YaHei;
  521. font-weight: 400;
  522. line-height: 16px;
  523. color: #7b7f86 !important;
  524. opacity: 1;
  525. }
  526. }
  527. .batch_button {
  528. display: inline-block;
  529. width: 30px;
  530. height: 30px;
  531. background-size: 100% 100%;
  532. cursor: pointer;
  533. margin-right: 10px;
  534. }
  535. .batch_display {
  536. // background: url('~@/assets/img/btn_shou.png') center no-repeat;
  537. background-size: 100% 100%;
  538. }
  539. .batch_collapse {
  540. // background: url('~@/assets/img/btn_collapse.png') center no-repeat;
  541. background-size: 100% 100%;
  542. }
  543. }
  544. }
  545. </style>