1250c1bb2f00c666a090d1cc086b2c0640aefe95.svn-base 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <div ref="tableWarp" class="tableWarp">
  3. <!-- 操作按钮区域 -->
  4. <slot name="extra_" slot="extra"></slot>
  5. <div class="table-operator">
  6. <a-row type="flex" :gutter="24" justify="space-between">
  7. <a-col :xl="16" :lg="16" :md="16" :sm="16" >
  8. <a-button type="primary" icon="download" >导出</a-button>
  9. <h5 style="display:inline-block;">{{cggraphreportData.head.annotation}}</h5>
  10. </a-col>
  11. <a-col :xl="4" :lg="4" :md="4" :sm="4" style="text-align:end">
  12. <a-switch checked-children="分页" un-checked-children="分页" v-model="ipagination"/>
  13. </a-col>
  14. </a-row>
  15. </div>
  16. <a-table
  17. size="middle"
  18. bordered
  19. :rowKey="(record, index) => index"
  20. :columns="columns"
  21. :data-source="getTableAddTotalRow"
  22. :pagination="ipagination"
  23. :scroll="{x:getTableScroll}"
  24. >
  25. <template v-for="(key,i) in Object.keys(customRender)" :slot="key" slot-scope="text, record, index">
  26. <div class="custom-render" :key="i" @click="addComment(text,record,index,$event)" v-html="customRender[key](text, record)"></div>
  27. </template>
  28. </a-table>
  29. </div>
  30. </template>
  31. <script>
  32. import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
  33. export default {
  34. name: 'GraphreportAutoTable',
  35. computed: {
  36. getTableAddTotalRow() { // 根据配置获取表格数据
  37. let tableDataSource_
  38. if (this.cggraphreportData.head.dataType === 'json') {
  39. tableDataSource_ = JSON.parse(JSON.stringify(this.chartData))
  40. } else if (this.cggraphreportData.head.dataType === 'sql' || this.cggraphreportData.head.dataType === 'bean') {
  41. tableDataSource_ = JSON.parse(JSON.stringify(this.cggraphreportData.data.data))
  42. }
  43. if (!this.isTotal) {
  44. return tableDataSource_
  45. }
  46. const numKey = 'rowIndex'
  47. const totalRow = { [numKey]: '合计' }
  48. this.columns.forEach(column => {
  49. const { key, dataIndex } = column
  50. if (![key, dataIndex].includes(numKey)) {
  51. let total = 0
  52. tableDataSource_.forEach(data => {
  53. total += /^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseFloat(data[dataIndex]) : Number.NaN
  54. })
  55. if (Number.isNaN(total) || !column.isTotal) {
  56. total = '-'
  57. }
  58. totalRow[dataIndex] = total
  59. }
  60. })
  61. tableDataSource_.push(totalRow)
  62. return tableDataSource_
  63. },
  64. getTableScroll() {
  65. const width = this.columns.length * 100 - 40
  66. // 解决表格列分配宽度小于表格宽度时 多一列bug
  67. if (width < this.tableWarpWidth) {
  68. this.columns.forEach(function(item) {
  69. if (item.dataIndex !== 'rowIndex') {
  70. delete item.width
  71. }
  72. })
  73. }
  74. return width > this.tableWarpWidth ? width : false
  75. }
  76. },
  77. data() {
  78. return {
  79. columns: [], // 需要渲染的表头
  80. rowIndexColumns: {// 序号列头
  81. title: '#',
  82. dataIndex: 'rowIndex',
  83. width: 60,
  84. align: 'center',
  85. fixed: 'left',
  86. customRender: function(text, r, index) {
  87. return parseInt(index) + 1
  88. }
  89. },
  90. rowIndexTotalColumns: {// 序号列头
  91. title: '#',
  92. dataIndex: 'rowIndex',
  93. width: 60,
  94. align: 'center',
  95. fixed: 'left',
  96. customRender: function(text, r, index) {
  97. return (text !== '合计') ? (parseInt(index) + 1) : text
  98. }
  99. },
  100. customRender: {},
  101. ipagination: true,
  102. spinning: true,
  103. tableWarpWidth: 0, // 判断table是否超宽
  104. isTotal: false // 判断是否合计
  105. }
  106. },
  107. props: ['items', 'cggraphreportData', 'chartData'],
  108. watch: {
  109. items: {
  110. handler(val, oldVal) {
  111. this.getColunms()
  112. },
  113. deep: true // true 深度监听
  114. }
  115. },
  116. mounted() {
  117. this.$nextTick(() => {
  118. if (this.$refs.tableWarp) {
  119. this.tableWarpWidth = this.$refs.tableWarp.offsetWidth
  120. }
  121. this.getColunms()
  122. })
  123. },
  124. methods: {
  125. getColunms() { // 根据弹窗的数据获取表格的表头
  126. const that = this
  127. that.columns = [this.rowIndexColumns]
  128. const items = JSON.parse(JSON.stringify(this.items))
  129. items.sort(function(a, b) {
  130. return a.orderNum - b.orderNum
  131. })
  132. items.forEach((item, i) => {
  133. if (item.isShow) {
  134. const column = {
  135. title: item.fieldTxt,
  136. align: 'center',
  137. dataIndex: item.fieldName,
  138. isTotal: item.isTotal,
  139. width: item.fieldWidth || 100
  140. }
  141. if (item.dictCode) {
  142. column.customRender = function(t, r, i) {
  143. return filterDictTextByCache(item.dictCode, t)
  144. }
  145. }
  146. if (item.jsEnhance) {
  147. column.scopedSlots = { customRender: column.dataIndex }
  148. var obj = eval('(' + item.jsEnhance + ')')
  149. that.customRender[column.dataIndex] = obj.customRender
  150. }
  151. that.columns.push(column)
  152. }
  153. if (item.isTotal) {
  154. this.isTotal = true
  155. }
  156. })
  157. if (this.isTotal) {
  158. that.columns.splice(0, 1, this.rowIndexTotalColumns)
  159. }
  160. if (that.getTableScroll) {
  161. this.rowIndexColumns.fixed = 'left'
  162. } else {
  163. this.rowIndexColumns.fixed = false
  164. }
  165. },
  166. addComment(text, record, index, $event) { // 处理js增强添加的方法 目前只支持click
  167. var domClick = $event.target.getAttribute('@click')
  168. if (domClick) {
  169. domClick = eval('(' + domClick + ')')
  170. domClick.call(this, text, record, index)
  171. }
  172. }
  173. }
  174. }
  175. </script>
  176. <style scoped>
  177. .ant-table td { white-space: nowrap; }
  178. </style>
  179. <style>
  180. .custom-render>*{
  181. display: inline-block;
  182. min-width: 20px;
  183. height: 100%;
  184. border-radius: 5px;
  185. }
  186. .custom-render{
  187. display: inline-block;
  188. }
  189. </style>