baseDateTable.vue 15 KB

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