KtCommonTable.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <template>
  2. <div style="position: relative" ref="commonTable">
  3. <!--表格栏-->
  4. <el-table
  5. id="commonTable"
  6. ref="doLayout"
  7. :data="data.content"
  8. :highlight-current-row="highlightCurrentRow"
  9. @selection-change="selectionChange"
  10. @row-click="rowClick"
  11. @current-change="handleCurrentChange"
  12. :element-loading-text="$t('action.loading')"
  13. :border="border"
  14. :stripe="stripe"
  15. :show-summary="showSummary"
  16. :summary-method="getSummaries"
  17. :show-overflow-tooltip="showOverflowTooltip"
  18. :height="tableHeight"
  19. :size="size"
  20. :align="align"
  21. :header-cell-style="{
  22. background: '#F2F7FC',
  23. fontWeight: '900',
  24. color: '#606266',
  25. }"
  26. style="width: 100%"
  27. :row-class-name="tableRowClassName"
  28. >
  29. <el-table-column
  30. type="selection"
  31. width="50"
  32. fixed
  33. align="center"
  34. v-if="showBatchDelete || showBatchSettled || showBatchPrint"
  35. ></el-table-column>
  36. <el-table-column
  37. v-if="SerialShow"
  38. label="序号"
  39. type="index"
  40. width="50"
  41. align="center"
  42. ></el-table-column>
  43. <template v-for="column in columns">
  44. <el-table-column
  45. header-align="center"
  46. align="center"
  47. :prop="column.prop"
  48. :label="column.label"
  49. :width="column.width"
  50. :min-width="column.minWidth"
  51. :fixed="column.fixed"
  52. :key="column.prop"
  53. :type="column.type"
  54. :formatter="column.formatter"
  55. :sortable="column.sortable ? column.sortable : false"
  56. v-if="column.signIcon || column.isImg || column.isBtn"
  57. >
  58. <template slot-scope="scope">
  59. <el-switch
  60. :disabled="column.isDisabled"
  61. v-if="column.signIcon"
  62. v-model="scope.row[column.prop]"
  63. active-value="1"
  64. inactive-value="0"
  65. @change="signStateChanged(scope.$index, scope.row)"
  66. ></el-switch>
  67. <img
  68. v-if="column.isImg"
  69. :width="column.imgWidth"
  70. :height="column.imgHeight"
  71. :src="scope.row[column.prop]"
  72. />
  73. <kt-button
  74. v-if="column.isBtn"
  75. icon="fa fa-edit"
  76. :label="column.btnName"
  77. :size="size"
  78. :perms="column.permsEdit"
  79. @click="handleBtnEdit(scope.$index, scope.row)"
  80. />
  81. </template>
  82. </el-table-column>
  83. <el-table-column
  84. header-align="center"
  85. align="center"
  86. :prop="column.prop"
  87. :label="column.label"
  88. :width="column.width"
  89. :min-width="column.minWidth"
  90. :fixed="column.fixed"
  91. :key="column.prop"
  92. :type="column.type"
  93. :formatter="column.formatter"
  94. :sortable="column.sortable ? column.sortable : false"
  95. v-else
  96. >
  97. <template v-for="columnchildren in column.children">
  98. <el-table-column
  99. v-if="columnchildren"
  100. header-align="center"
  101. align="center"
  102. :prop="columnchildren.prop"
  103. :label="columnchildren.label"
  104. :width="columnchildren.width"
  105. :min-width="columnchildren.minWidth"
  106. :fixed="columnchildren.fixed"
  107. :key="columnchildren.prop"
  108. :type="columnchildren.type"
  109. :formatter="columnchildren.formatter"
  110. :sortable="
  111. columnchildren.sortable == null ? true : columnchildren.sortable
  112. "
  113. >
  114. </el-table-column>
  115. </template>
  116. </el-table-column>
  117. </template>
  118. <el-table-column
  119. :label="$t('action.operation')"
  120. :width="operationWidth"
  121. fixed="right"
  122. v-if="showOperation"
  123. header-align="center"
  124. align="center"
  125. >
  126. <template slot-scope="scope">
  127. <kt-button
  128. style="
  129. background: #1e87ed;
  130. border: 1px solid #1e87ed;
  131. color: #ffffff;
  132. "
  133. v-if="editButtonName"
  134. icon="fa fa-edit"
  135. :label="editButtonName"
  136. :perms="permsEdit"
  137. :size="size"
  138. @click="handleEdit(scope.$index, scope.row)"
  139. />
  140. <kt-button
  141. v-if="deleteButtonName"
  142. icon="fa fa-trash"
  143. :label="deleteButtonName"
  144. :perms="permsDelete"
  145. :size="size"
  146. type="danger"
  147. @click="handleDeleteFlag(scope.$index, scope.row)"
  148. />
  149. <kt-button
  150. :style="{
  151. background: button1Background + '',
  152. border: '1px solid' + button1Background,
  153. color: '#fff',
  154. }"
  155. v-if="button1Name"
  156. :label="button1Name"
  157. :perms="permsButton1"
  158. :size="size"
  159. :icon="button1Icon"
  160. @click="handleButton1(scope.$index, scope.row)"
  161. />
  162. <kt-button
  163. :style="{
  164. background: button2Background,
  165. border: '1px solid' + button2Background,
  166. color: '#fff',
  167. }"
  168. v-if="button2Name"
  169. :label="button2Name"
  170. :perms="permsButton2"
  171. :size="size"
  172. :icon="button2Icon"
  173. @click="handleButton2(scope.$index, scope.row)"
  174. />
  175. <kt-button
  176. :style="{
  177. background: button3Background,
  178. border: '1px solid' + button3Background,
  179. color: '#fff',
  180. }"
  181. v-if="button3Name"
  182. :label="button3Name"
  183. :perms="permsButton3"
  184. :size="size"
  185. :icon="button3Icon"
  186. @click="handleButton3(scope.$index, scope.row)"
  187. />
  188. <kt-button
  189. :style="{
  190. background: button4Background,
  191. border: '1px solid ' + button4Background,
  192. color: '#ffffff',
  193. }"
  194. v-if="button4Name"
  195. :icon="button4Icon"
  196. :label="button4Name"
  197. :perms="permsButton4"
  198. :size="size"
  199. @click="handleButton4(scope.$index, scope.row)"
  200. />
  201. </template>
  202. </el-table-column>
  203. </el-table>
  204. <!--分页栏-->
  205. <!-- 底部分页 -->
  206. <div class="bottomFixed" ref="bottomFixed">
  207. <kt-button
  208. class="debutton"
  209. :label="$t('action.batchDelete')"
  210. :perms="permsDelete"
  211. :size="size"
  212. type="danger"
  213. @click="handleBatchDelete()"
  214. :disabled="selections.length === 0"
  215. style="float: left"
  216. v-if="showBatchDelete & showOperation"
  217. />
  218. <kt-button
  219. :label="settledName"
  220. :perms="permsDelete"
  221. :size="size"
  222. type="danger"
  223. @click="handleBatchSettled()"
  224. :disabled="selections.length === 0"
  225. style="float: left"
  226. v-if="showBatchSettled"
  227. />
  228. <!-- <kt-button label="导出Excel" :perms="permsDelete" :size="size" type="danger" @click="handleBatchPrint()"
  229. :disabled="selections.length === 0" style="float: left" v-if="showBatchPrint" /> -->
  230. <kt-button
  231. label="导出Excel"
  232. :perms="permsDelete"
  233. :size="size"
  234. type="danger"
  235. @click="handleBatchPrint()"
  236. style="float: left"
  237. v-if="showBatchPrint"
  238. />
  239. <el-pagination
  240. @size-change="handleSizeChange"
  241. layout="total, sizes, prev, pager, next, jumper"
  242. @current-change="refreshPageRequest"
  243. :current-page="data.pageNum"
  244. :page-size="data.pageSize"
  245. :page-sizes="[10, 20, 50, 100, 200, 300, 400, 500]"
  246. :total="data.totalSize"
  247. style="float: left; margin-left: 20px"
  248. v-if="isshowpagination"
  249. >
  250. </el-pagination>
  251. </div>
  252. </div>
  253. </template>
  254. <script>
  255. import KtButton from "@/views/Core/KtButton";
  256. import BigNumber from "bignumber.js";
  257. export default {
  258. name: "KtCommonTable",
  259. components: {
  260. KtButton,
  261. },
  262. props: {
  263. operationWidth: {
  264. type: Number,
  265. default: 200,
  266. },
  267. showBatchSettled: {
  268. //显示批量结算
  269. type: Boolean,
  270. default: false,
  271. },
  272. SerialShow: {
  273. type: Boolean,
  274. default: true,
  275. },
  276. settledName: {
  277. //批量按钮name
  278. type: String,
  279. },
  280. showBatchPrint: {
  281. //显示批量导出
  282. type: Boolean,
  283. default: false,
  284. },
  285. //显示合计
  286. showSummary: {
  287. type: Boolean,
  288. default: false,
  289. },
  290. button1Name: {
  291. type: String,
  292. },
  293. button2Name: {
  294. type: String,
  295. },
  296. button3Name: {
  297. type: String,
  298. },
  299. button4Name: {
  300. type: String,
  301. },
  302. permsButton1: {
  303. type: String,
  304. },
  305. permsButton2: {
  306. type: String,
  307. },
  308. permsButton3: {
  309. type: String,
  310. },
  311. permsButton4: {
  312. type: String,
  313. },
  314. button1Background: {
  315. type: String,
  316. default: "#f56c6c",
  317. },
  318. button2Background: {
  319. type: String,
  320. default: "rgb(222, 80, 76)",
  321. },
  322. button3Background: {
  323. type: String,
  324. default: "rgb(14, 194, 22)",
  325. },
  326. button4Background: {
  327. type: String,
  328. default: "#1e87ed",
  329. },
  330. button1Icon: {
  331. type: String,
  332. default: "fa fa-trash",
  333. },
  334. button2Icon: {
  335. type: String,
  336. default: "fa fa-edit",
  337. },
  338. button3Icon: {
  339. type: String,
  340. default: "fa fa-edit",
  341. },
  342. button4Icon: {
  343. type: String,
  344. default: "fa fa-trash",
  345. },
  346. editButtonName: {
  347. type: String,
  348. },
  349. deleteButtonName: {
  350. type: String,
  351. },
  352. isshowpagination: {
  353. //是否显示分页栏
  354. type: Boolean,
  355. default: true,
  356. },
  357. showSummaryFieldList: Array,//自定义合计列配置
  358. keyName: String, //主键字段名
  359. columns: Array, // 表格列配置
  360. data: {}, // 表格分页数据
  361. permsEdit: String, // 编辑权限标识
  362. permsDelete: String, // 删除权限标识
  363. size: {
  364. // 尺寸样式
  365. type: String,
  366. default: "small ",
  367. },
  368. align: {
  369. // 文本对齐方式
  370. type: String,
  371. default: "left",
  372. },
  373. // maxHeight: { // 表格最大高度
  374. // type: Number,
  375. // default: 1420
  376. // },
  377. // height: { // 表格最大高度
  378. // type: Number,
  379. // default: 250
  380. // },
  381. showOperation: {
  382. // 是否显示操作组件
  383. type: Boolean,
  384. default: true,
  385. },
  386. border: {
  387. // 是否显示边框
  388. type: Boolean,
  389. default: true,
  390. },
  391. stripe: {
  392. // 是否显示斑马线
  393. type: Boolean,
  394. default: true,
  395. },
  396. highlightCurrentRow: {
  397. // 是否高亮当前行
  398. type: Boolean,
  399. default: true,
  400. },
  401. showOverflowTooltip: {
  402. // 是否单行显示
  403. type: Boolean,
  404. default: true,
  405. },
  406. showBatchDelete: {
  407. // 是否显示操作组件
  408. type: Boolean,
  409. default: false,
  410. },
  411. },
  412. data() {
  413. return {
  414. tableHeight: 0,
  415. // 分页信息
  416. pageRequest: {
  417. pageNum: 1,
  418. pageSize: 20,
  419. },
  420. loading: false, // 加载标识
  421. selections: [], // 列表选中列
  422. };
  423. },
  424. methods: {
  425. tableRowClassName({ row, rowIndex }) {
  426. if (!!row.fieldName24 && row.fieldName24.indexOf("未") >= 0) {
  427. return "warningRow";
  428. } else if (!!row.fieldName24 && row.fieldName24.indexOf("已") >= 0) {
  429. return "successRow";
  430. }
  431. // return '';
  432. },
  433. getSummaries(param) {
  434. const { columns, data } = param;
  435. const sums = [];
  436. columns.forEach((column, index) => {
  437. if (index === 0) {
  438. sums[index] = "合计"; // 假设第一列显示文本'合计'
  439. return;
  440. }
  441. if (this.showSummaryFieldList) {
  442. // 如果当前列名不在需要计算的列名数组中,直接返回空值或其他标记
  443. if (!this.showSummaryFieldList.includes(column.property)) {
  444. sums[index] = "-";
  445. return;
  446. }
  447. }
  448. let sum = new BigNumber(0);
  449. data.forEach((item) => {
  450. let value = item[column.property];
  451. let number = new BigNumber(value);
  452. if (!number.isNaN()) {
  453. sum = sum.plus(number);
  454. }
  455. });
  456. sums[index] = sum.toFixed(); // 转换成字符串避免精度问题
  457. });
  458. return sums;
  459. },
  460. // 分页查询
  461. findPage: function () {
  462. this.loading = true;
  463. let callback = (res) => {
  464. this.loading = false;
  465. //解决操作栏位置偏移问题
  466. this.$nextTick(() => {
  467. this.$refs.doLayout.doLayout();
  468. }, 200);
  469. };
  470. this.$emit("findPage", {
  471. pageRequest: this.pageRequest,
  472. callback: callback,
  473. });
  474. },
  475. hideLoading() {
  476. this.loading = false;
  477. },
  478. // 选择切换
  479. selectionChange: function (selections) {
  480. this.selections = selections;
  481. this.$emit("selectionChange", {
  482. selections: selections,
  483. });
  484. },
  485. // 选择切换
  486. handleCurrentChange: function (val) {
  487. this.$emit("handleCurrentChange", {
  488. val: val,
  489. });
  490. },
  491. rowClick: function (row) {
  492. this.$emit("rowClick", {
  493. row: row,
  494. });
  495. },
  496. // 换页刷新
  497. refreshPageRequest: function (pageNum) {
  498. this.pageRequest.pageNum = pageNum;
  499. this.findPage();
  500. },
  501. // 每页条数发生改变
  502. handleSizeChange(pageSize) {
  503. this.pageRequest.pageSize = pageSize;
  504. this.findPage();
  505. },
  506. signStateChanged(index, row) {
  507. this.$emit("signStateChanged", {
  508. index: index,
  509. row: row,
  510. });
  511. },
  512. // 按钮编辑
  513. handleBtnEdit: function (index, row) {
  514. this.$emit("handleBtnEdit", {
  515. index: index,
  516. row: row,
  517. });
  518. },
  519. // 编辑
  520. handleEdit: function (index, row) {
  521. this.$emit("handleEdit", {
  522. index: index,
  523. row: row,
  524. });
  525. // this.$emit('findLeftTreeData')
  526. },
  527. // 删除
  528. handleDelete: function (index, row) {
  529. let _keyName = "id";
  530. if (!!this.keyName) {
  531. _keyName = this.keyName;
  532. }
  533. this.delete(row[_keyName]);
  534. },
  535. handleButton1: function (index, row) {
  536. this.$emit("handleButton1", {
  537. index: index,
  538. row: row,
  539. });
  540. },
  541. handleButton2: function (index, row) {
  542. this.$emit("handleButton2", {
  543. index: index,
  544. row: row,
  545. });
  546. },
  547. handleButton3: function (index, row) {
  548. this.$emit("handleButton3", {
  549. index: index,
  550. row: row,
  551. });
  552. },
  553. handleButton4: function (index, row) {
  554. this.$emit("handleButton4", {
  555. index: index,
  556. row: row,
  557. });
  558. },
  559. //删除单个
  560. handleDeleteFlag: function (index, row) {
  561. this.$emit("handleDeleteFlag", { index: index, row: row });
  562. // var _keyName = "id";
  563. // if (!!this.keyName) {
  564. // _keyName = this.keyName;
  565. // }
  566. // let ids = row[_keyName];
  567. // this.$confirm("确认删除吗?", "提示", {
  568. // type: "warning",
  569. // })
  570. // .then(() => {
  571. // let params = [];
  572. // let idArray = (ids + "").split(",");
  573. // for (var i = 0; i < idArray.length; i++) {
  574. // var obj = {};
  575. // obj[_keyName] = idArray[i];
  576. // params.push(obj);
  577. // }
  578. // this.loading = true;
  579. // let callback = (res) => {
  580. // if (res.code == 200) {
  581. // this.$message({
  582. // message: "操作成功",
  583. // type: "success",
  584. // });
  585. // this.findPage();
  586. // // this.$emit('findLeftTreeData')
  587. // } else {
  588. // this.$message({
  589. // message: "操作失败, " + res.msg,
  590. // type: "error",
  591. // });
  592. // }
  593. // };
  594. // this.$emit("handleDeleteFlag", {
  595. // params: params,
  596. // callback: callback,
  597. // });
  598. // this.loading = false;
  599. // })
  600. // .catch(() => {
  601. // this.loading = false;
  602. // });
  603. },
  604. // 批量删除
  605. handleBatchDelete: function () {
  606. let idlist = [];
  607. this.selections.map((item) => idlist.push(item.id));
  608. this.$emit("handleDelete", idlist);
  609. },
  610. // 删除操作
  611. delete: function (ids) {
  612. var _keyName = "id";
  613. if (!!this.keyName) {
  614. _keyName = this.keyName;
  615. }
  616. this.$confirm("确认删除吗?", "提示", {
  617. type: "warning",
  618. })
  619. .then(() => {
  620. let params = [];
  621. let idArray = (ids + "").split(",");
  622. for (var i = 0; i < idArray.length; i++) {
  623. var obj = {};
  624. obj[_keyName] = idArray[i];
  625. params.push(obj);
  626. }
  627. let callback = (res) => {
  628. if (res.code == 200) {
  629. this.$message({
  630. message: "操作成功",
  631. type: "success",
  632. });
  633. this.findPage();
  634. // this.$emit('findLeftTreeData')
  635. } else {
  636. this.$message({
  637. message: "操作失败, " + res.msg,
  638. type: "error",
  639. });
  640. }
  641. };
  642. this.$emit("handleDelete", {
  643. params: params,
  644. callback: callback,
  645. });
  646. })
  647. .catch(() => {});
  648. },
  649. //批量结算
  650. handleBatchSettled: function () {
  651. let _keyName = "orderno";
  652. if (!!this.keyName) {
  653. _keyName = this.keyName;
  654. }
  655. let idlist = [];
  656. this.selections.map((item) => idlist.push(item[_keyName]));
  657. this.$emit("handleSettled", idlist);
  658. },
  659. //批量打印
  660. handleBatchPrint: function () {
  661. // let _keyName = "orderno";
  662. // if(!!this.keyName){
  663. // _keyName = this.keyName;
  664. // }
  665. // let ids = this.selections.map(item => item[_keyName]).toString()
  666. // console.log(ids);
  667. this.$emit("handlePrint");
  668. },
  669. },
  670. mounted() {
  671. const that = this;
  672. that.refreshPageRequest(1);
  673. setTimeout(() => {
  674. let tableTop = that.$refs.commonTable.offsetTop;
  675. let bottomTop = that.$refs.bottomFixed.offsetTop;
  676. that.tableHeight = bottomTop - tableTop - 120 + "px";
  677. }, 10);
  678. // window.onresize = function () {
  679. // let tableTop = that.$refs.commonTable.offsetTop;
  680. // let bottomTop = that.$refs.bottomFixed.offsetTop;
  681. // that.tableHeight = bottomTop - tableTop - 120 + "px";
  682. // };
  683. },
  684. };
  685. </script>
  686. <style scoped>
  687. /* 底部固定分页 */
  688. .bottomFixed {
  689. height: 40px;
  690. position: fixed;
  691. bottom: 0;
  692. width: 100%;
  693. background: #fff;
  694. /* margin-top: 10px; */
  695. /* box-shadow: 0 -2px 0 0 #efefef; */
  696. }
  697. .el-table .warningRow {
  698. background: oldlace;
  699. }
  700. .el-table .successRow {
  701. background: #f0f9eb;
  702. }
  703. </style>