lyy-table.vue 16 KB

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