lyy-table.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <template>
  2. <view class="table_box">
  3. <view class="boxlist">
  4. </view>
  5. <scroll-view scroll-x scroll-y :class="tabHeight" @scrolltolower="scrolltolower" class="lips">
  6. <uni-table stripe border :loading="loading" style="min-height: 100%;">
  7. <uni-tr id="lyy-thead" :class="headerFixed?'fixed-head':''" style="display: flex;">
  8. <uni-th v-for="(item,index) in headers" :key="index" :width="item.widtd" align="center" :style="{
  9. display:item.hidden?'none':'flex',justifyContent: 'center',
  10. left:fixedLeft(index),
  11. position:index<=columnFixed-1?'sticky':'unset',
  12. zIndex:index<=columnFixed-1?999:99,
  13. backgroundColor:'inherit'
  14. }">
  15. <view @click="doSort(item)" style="display: flex;flex-direction: row;justify-content: center;">
  16. <text :style="{lineHeight:'20px'}">{{item.label}}</text>
  17. <view class="header-icon"
  18. style="line-height:8px;display:flex;flex-direction:column;margin-left:10px;justify-content: center;"
  19. v-if="item.sort">
  20. <text class="iconfont icon-arrow-up"
  21. :style="{fontSize:'8px',height:'10px',color:lastSortItem===item.key&&sortWay=='asc'?'#333':'#bcbcbc'}" />
  22. <text class="iconfont icon-arrow-down"
  23. :style="{fontSize:'8px',height:'10px',color:lastSortItem===item.key&&sortWay=='desc'?'#333':'#bcbcbc'}" />
  24. </view>
  25. </view>
  26. </uni-th>
  27. </uni-tr>
  28. <!--<uni-tr v-if="headerFixed" :style="{height: theadHeight}">
  29. </uni-tr>
  30. <!-- <uni-td class="no_data" align="center">暂无数据</uni-td> -->
  31. <!-- <view v-if="contents.length<1" class="no_data">暂无数据</view> -->
  32. <template v-if="contents.length>0">
  33. <uni-tr v-for="(content,sindex) in sortContents" :key="sindex"
  34. style="display:flex;table-layout: fixed;min-width: 100%;" @click.native="rowClick(content)">
  35. <uni-td v-for="(header,hindex) in headers" :key="hindex" :width="header.widtd" align="center"
  36. :style="{display:header.hidden?'none':'table-cell',textAlign:'center',
  37. left:fixedLeft(hindex),
  38. position:hindex<=columnFixed-1?'sticky':'unset',
  39. zIndex:hindex<=columnFixed-1?9:'unset',
  40. backgroundColor:'inherit'}">
  41. <template v-if="header.format!==undefined">
  42. <lyy-progress
  43. v-if="header.format.type==='progress'&&!isNaN(parseFloat(content[header.key]))"
  44. :percent="content[header.key].toFixed(2)" show-info round></lyy-progress>
  45. <view v-else-if="header.format.type==='html'" v-html="content[header.key]"></view>
  46. <text v-else>{{content[header.key]||emptyString}}</text>
  47. </template>
  48. <template v-if="header.label == '告警状态'">
  49. <text
  50. :class="[content[header.key] ==1?'warnColor':'']">{{content[header.key] === 0 ? '持续': '已结束'}}</text>
  51. </template>
  52. <template v-else>
  53. <view v-if="header.label == '操作' && !battery">
  54. <view @click="EditAlarm(content)" v-if="content.type=='消防告警' && content.alarmValue==999" class="editColor">消防换仓</view>
  55. <view v-else>{{emptyString}}</view>
  56. </view>
  57. <view v-if="header.label == '操作' && battery" class="editColor" @click="EditAlarmInfo(content)">详情
  58. </view>
  59. <view v-if="header.label == '时段明细'" class="editColor" @click="EditAlarmDetail(content)">明细
  60. </view>
  61. <text v-else>{{content[header.key]||emptyString}}</text>
  62. </template>
  63. </uni-td>
  64. </uni-tr>
  65. </template>
  66. <uni-tr v-if="totalRow.length>0" style="min-width: 100%;display: flex;">
  67. <uni-td v-for="(header,index) in headers" :key="Math.random()" align="center" :style="{textAlign: 'center',display:header.hidden?'none':'table-cell',width:header.width,
  68. left:fixedLeft(index),
  69. zIndex:index<=columnFixed-1?9:'unset',
  70. position:index<=columnFixed-1?'sticky':'unset',
  71. backgroundColor:'inherit'}">
  72. </uni-td>
  73. </uni-tr>
  74. </uni-table>
  75. <uni-load-more v-show="showLoadMore" :status="loadMore"></uni-load-more>
  76. </scroll-view>
  77. </view>
  78. </template>
  79. <script>
  80. import lyyProgress from './lyy-progress'
  81. /**
  82. * lyyTable ver1.3.3
  83. * @description lyyTable表格组件 ver1.3.3
  84. */
  85. export default {
  86. name: "lyyTable",
  87. components: {
  88. lyyProgress
  89. },
  90. data() {
  91. return {
  92. lastSortItem: '', //上一次排序列
  93. sortWay: 'none', //默认无排序
  94. sortIndex: 0,
  95. sortContents: [], //排序时的表格内容
  96. footContent: {},
  97. scrollHeight: '',
  98. theadHeight: '',
  99. tabHeight: null,
  100. }
  101. },
  102. props: {
  103. faultDrawer: {
  104. type: Function
  105. },
  106. //显示加载
  107. loading: {
  108. type: Boolean,
  109. default: false
  110. },
  111. //上拉加载文字,参考uni-load-more
  112. loadMore: {
  113. type: String,
  114. default: 'more'
  115. },
  116. //是否显示上拉加载组件
  117. showLoadMore: {
  118. default: Boolean,
  119. default: false
  120. },
  121. //固定表头
  122. headerFixed: {
  123. type: Boolean,
  124. default: false
  125. },
  126. //服务记录是否显示详情
  127. battery: {
  128. type: Number,
  129. default:3
  130. },
  131. //固定首列 ver1.3.3弃用
  132. /*firstColumnFixed: {
  133. type: Boolean,
  134. default: false
  135. },*/
  136. //固定列数 ver1.3.3新增
  137. columnFixed: {
  138. type: Number,
  139. default: 0
  140. },
  141. //排序方式
  142. sortWays: {
  143. type: Array,
  144. default: () => ['none', 'asc', 'desc']
  145. },
  146. //数据为空时的占位符
  147. emptyString: {
  148. type: String,
  149. default: '-'
  150. },
  151. //表头
  152. headers: {
  153. type: Array,
  154. default: [],
  155. },
  156. //表格数据
  157. contents: {
  158. type: Array,
  159. default: []
  160. },
  161. //合计列
  162. totalRow: {
  163. type: Array,
  164. default: () => []
  165. }
  166. },
  167. mounted() {
  168. //uni.setStorageSync('contents',this.contents)
  169. this.sortContents = JSON.parse(JSON.stringify(this.contents))
  170. this.renderContents()
  171. this.createTotalRow()
  172. //ver 1.2.0 修复 uni-table width 问题
  173. /*this.$nextTick(() => {
  174. console.log('abc',this.$refs)
  175. const query = uni.createSelectorQuery().in(this)
  176. query.select('.uni-table').boundingClientRect(dom=>{
  177. console.log(123456,dom)
  178. }).exec()
  179. this.$refs['uni-table'].removeAttribute('style')
  180. })*/
  181. //ver 1.2.0 新增 固定表头
  182. /*if (this.headerFixed) {
  183. /*var wHeight = document.body.clientHeight
  184. var tablePoseY = document.getElementById('lyy-tbody').getBoundingClientRect().y
  185. document.getElementById('lyy-tbody').style.height = wHeight - tablePoseY + 'px'
  186. const query = uni.createSelectorQuery().in(this)
  187. query.select('#lyy-thead').boundingClientRect(dom => {
  188. console.log(dom)
  189. this.theadHeight = dom.height + 'px'
  190. }).exec()
  191. }*/
  192. },
  193. watch: {
  194. contents: {
  195. handler(value) {
  196. console.log(value)
  197. this.sortContents = JSON.parse(JSON.stringify(value))
  198. this.renderContents()
  199. this.createTotalRow()
  200. this.lastSortItem = ''
  201. this.sortWay = 'none'
  202. for (var header of this.headers) {
  203. this.renderTotalRow(header)
  204. }
  205. this.$forceUpdate()
  206. if (value.length > 9) {
  207. this.tabHeight = 'container'
  208. } else {
  209. this.tabHeight = null
  210. }
  211. },
  212. deep: true
  213. },
  214. //监听排序变化
  215. sortChange(value) {
  216. var that = this
  217. var contents = JSON.parse(JSON.stringify(that.contents))
  218. switch (value.sortWay) {
  219. case 'none':
  220. that.sortContents = contents
  221. this.renderContents()
  222. break
  223. case 'asc': //正序
  224. that.sortContents = that.sortContents.sort(function(a, b) {
  225. //需要排序的列为数字时直接计算
  226. if (!isNaN(parseFloat(a[that.lastSortItem])) && !isNaN(parseFloat(b[that
  227. .lastSortItem]))) {
  228. return a[that.lastSortItem] - b[that.lastSortItem]
  229. }
  230. //非数字转为ASCII排序
  231. else {
  232. return a[that.lastSortItem].charCodeAt() - b[that.lastSortItem].charCodeAt()
  233. }
  234. })
  235. break
  236. case 'desc': //倒序
  237. that.sortContents = that.sortContents.sort(function(a, b) {
  238. if (!isNaN(parseFloat(a[that.lastSortItem])) && !isNaN(parseFloat(b[that
  239. .lastSortItem]))) {
  240. return b[that.lastSortItem] - a[that.lastSortItem]
  241. } else {
  242. return b[that.lastSortItem].charCodeAt() - a[that.lastSortItem].charCodeAt()
  243. }
  244. })
  245. break
  246. }
  247. that.$forceUpdate()
  248. }
  249. },
  250. computed: {
  251. //将排序方式、上次排序列作为一个整体进行监听,不然会出现切换排序列不排序的现象
  252. sortChange() {
  253. var {
  254. sortWay,
  255. lastSortItem
  256. } = this
  257. return {
  258. sortWay,
  259. lastSortItem
  260. }
  261. }
  262. },
  263. methods: {
  264. //触发故障告警抽屉
  265. EditAlarm(row) {
  266. this.$emit('faultDrawer', row)
  267. },
  268. //换电详情弹出框
  269. EditAlarmInfo(row) {
  270. this.$emit('drawer', row)
  271. },
  272. //充电明细弹出框
  273. EditAlarmDetail(row) {
  274. this.$emit('charging', row)
  275. },
  276. //点击排序表头时存储上次排序列名,并循环切换排序方式
  277. doSort(item) {
  278. if (item.sort) {
  279. if (this.lastSortItem !== item.key) {
  280. this.lastSortItem = item.key
  281. this.sortIndex = 0
  282. this.sortIndex++
  283. this.sortWay = this.sortWays[this.sortIndex]
  284. } else {
  285. if (this.sortIndex < 2) {
  286. this.sortIndex++
  287. this.sortWay = this.sortWays[this.sortIndex]
  288. } else {
  289. this.sortIndex = 0
  290. this.sortWay = this.sortWays[0]
  291. }
  292. }
  293. }
  294. },
  295. //表格内容渲染
  296. renderContents() {
  297. const headers = this.headers
  298. //防止修改穿透
  299. var contents = JSON.parse(JSON.stringify(this.contents))
  300. //var contents=uni.getStorageSync('contents')
  301. let sortContents = JSON.parse(JSON.stringify(this.sortContents))
  302. var newArr = []
  303. var result = ''
  304. sortContents.forEach(function(content, index) {
  305. var item = content
  306. headers.forEach(function(header) {
  307. //字符类型格式化
  308. if (typeof header.format !== 'undefined' && (header.format.type === 'string' ||
  309. header.format.type === 'html')) {
  310. var template = header.format.template
  311. var keys = header.format.keys
  312. //console.log(typeof template)
  313. if (typeof template === 'function') {
  314. var arg = []
  315. keys.forEach((el, i) => {
  316. arg.push(contents[index][el])
  317. })
  318. result = template(arg)
  319. //console.log(result)
  320. } else {
  321. keys.forEach((el, i) => {
  322. var value = contents[index][el]
  323. var reg = new RegExp('\\{' + i + '}', 'g')
  324. template = template.replace(reg, value)
  325. })
  326. result = template
  327. }
  328. item[header.key] = result
  329. }
  330. //计算类型格式化
  331. else if (typeof header.format !== 'undefined' && (header.format.type ===
  332. 'compute' || header.format.type === 'progress')) {
  333. //console.log(header.format.template)
  334. var temp = header.format.template
  335. var keys = header.format.keys
  336. keys.forEach((el, i) => {
  337. var reg = new RegExp('\\{' + i + '}', 'g')
  338. temp = temp.replace(reg, contents[index][el])
  339. })
  340. //console.log(temp)
  341. item[header.key] = eval(temp)
  342. //this.sortContents[index][header.key]=result
  343. }
  344. })
  345. newArr.push(item)
  346. })
  347. this.sortContents = newArr
  348. },
  349. createTotalRow() {
  350. if (this.totalRow.length > 0 && this.sortContents[0] !== undefined) {
  351. /*var obj = {...this.contents[0]}
  352. console.log(obj)
  353. for (var i in obj) {
  354. this.footContent[i] = obj[i]
  355. }*/
  356. this.footContent = JSON.parse(JSON.stringify(this.sortContents[0]))
  357. for (var i in this.footContent) {
  358. var result = 0
  359. if (this.sortContents.length > 0) {
  360. for (var j in this.sortContents) {
  361. result += parseFloat(this.sortContents[j][i]) || 0
  362. }
  363. }
  364. this.footContent[i] = result
  365. }
  366. }
  367. },
  368. //合计列渲染
  369. renderTotalRow(header) {
  370. var content = JSON.parse(JSON.stringify(this.footContent))
  371. var result = this.emptyString
  372. if (this.totalRow.indexOf(header.key) > -1) {
  373. if (typeof header.format !== 'undefined' && header.format.type === 'progress') {
  374. var temp = header.format.template
  375. var keys = header.format.keys
  376. for (var index in keys) {
  377. var reg = new RegExp('\\{' + index + '}', 'g')
  378. temp = temp.replace(reg, content[keys[index]])
  379. }
  380. result = eval(temp)
  381. result = isNaN(result) ? 0 : result.toFixed(2)
  382. } else {
  383. if (content[header.key] != null && !isNaN(content[header.key])) {
  384. result = content[header.key]
  385. }
  386. }
  387. }
  388. return result
  389. },
  390. //行点击事件
  391. rowClick(data) {
  392. this.$emit('rowClick', data)
  393. },
  394. //上拉加载事件
  395. scrolltolower(e) {
  396. console.log(e)
  397. if (e.detail.direction == 'bottom') {
  398. this.$emit('onPullup')
  399. }
  400. },
  401. //固定列left计算
  402. fixedLeft(index) {
  403. var headers = this.headers.filter(item => !item.hidden)
  404. var left = 'calc(0px'
  405. for (var i = 1; i < index + 1; i++) {
  406. left += ' + ' + headers[i - 1].width
  407. }
  408. left += ')'
  409. return left
  410. }
  411. }
  412. }
  413. </script>
  414. <style scoped lang="scss">
  415. @import './css/iconfont.css';
  416. .boxlist {
  417. width: 100%;
  418. height: 5px;
  419. position: sticky;
  420. z-index: 99;
  421. background: #111827;
  422. top: -5px;
  423. }
  424. .table_box {
  425. height: 520px;
  426. overflow: hidden;
  427. }
  428. .warnColor {
  429. color: #636C97;
  430. }
  431. .editColor {
  432. color: #55B693;
  433. font-weight: bold;
  434. }
  435. .container {
  436. height: 100%;
  437. margin: 0;
  438. padding: 0;
  439. }
  440. .uni-table-scroll {
  441. overflow: unset !important;
  442. margin: 0;
  443. padding: 0;
  444. // height: 576px;
  445. }
  446. .no_data {
  447. position: absolute;
  448. width: 100%;
  449. background-color: #FFF;
  450. height: 50px;
  451. text-align: center;
  452. line-height: 50px;
  453. }
  454. .fixed-head {
  455. position: sticky;
  456. top: 0px;
  457. z-index: 99;
  458. height: 50px;
  459. }
  460. .uni-table-th {
  461. width: 142px;
  462. }
  463. .uni-table-td {
  464. width: 142px;
  465. white-space: nowrap;
  466. text-overflow: ellipsis;
  467. overflow: hidden;
  468. }
  469. ::v-deep .uni-table-text {
  470. position: fixed;
  471. right: 0;
  472. left: 240px;
  473. text-align: center;
  474. font-size: 14px;
  475. color: #999;
  476. width: 1001px;
  477. }
  478. /*修复滑动时偏移1px问题*/
  479. .table--border {
  480. border-top: none;
  481. border-left: none;
  482. }
  483. ::v-deep .table--border {
  484. border: none !important;
  485. }
  486. ::v-deep .uni-load-more {
  487. position: fixed;
  488. bottom: 20px;
  489. z-index: 9999;
  490. width: 1000px;
  491. text-align: center;
  492. .uni-load-more__text {
  493. font-size: 14px;
  494. color: #636C97 !important;
  495. }
  496. }
  497. ::v-deep .uni-table {
  498. background: none;
  499. .empty-border {
  500. border: none;
  501. height: 80px;
  502. line-height: 80px;
  503. }
  504. .uni-table-th {
  505. color: #FFFFFF;
  506. }
  507. .uni-table-td {
  508. font-weight: 400;
  509. color: #FFFFFF;
  510. font-size: 14px;
  511. height: 50px;
  512. line-height: 32px;
  513. }
  514. .uni-table-tr:nth-child(n + 2):hover {
  515. background-color: #1e2f36;
  516. }
  517. }
  518. #lyy-thead {
  519. /*min-width: 750upx;*/
  520. display: table;
  521. height: 50px;
  522. background: #1D283E;
  523. box-shadow: 0px 1px 0px 0px #2F3C86;
  524. font-weight: 600;
  525. color: #FFFFFF;
  526. font-size: 14px;
  527. }
  528. // ::v-deep .table--stripe .uni-table-tr:nth-child(2n + 1) {
  529. // background-color: #141B2B;
  530. // box-shadow: 0px 1px 0px 0px #292D46, 0px -1px 0px 0px #292D46;
  531. // }
  532. .table--stripe .uni-table-tr:nth-child(2n + 3) {
  533. background-color: #111827;
  534. }
  535. ::v-deep .uni-table-tr {
  536. background-color: #141B2B;
  537. // box-shadow: 0px 1px 0px 0px #292D46;
  538. border-bottom: #292D46 solid 1px;
  539. }
  540. </style>