CtableClass.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { formatDate } from '@/utils/date.js'
  2. /**
  3. * 将后台取回来的数据, 转换成table所需要的二位数组
  4. * @param obj 【数据源】
  5. * @param arr 【所需要取的字段】
  6. * @return {[[],[],[],[]]} 返回一个 二维数组
  7. */
  8. export function CtableClass(obj, arr) {
  9. if (obj.length < 1) { return [] }
  10. let _result = [];
  11. const han = /^[\u4e00-\u9fa5]+$/;
  12. obj.forEach((item, key) => {
  13. let _arr = []
  14. for (let i = 0; i < arr.length; i++) {
  15. if (han.test(arr[i])) {
  16. _arr.push(arr[i])
  17. } else if (arr[i] instanceof Array) {
  18. if (arr[i][0] === '时间戳') {
  19. let _time = formatDate(new Date(item[arr[i][1]]), 'yyyy-MM-dd hh:mm:ss')
  20. _arr.push([arr[i][0], _time])
  21. } else if (arr[i][0] === '规格书') {
  22. _arr.push([arr[i][0], item[arr[i][1]]])
  23. } else if (arr[i][0] === '地址') {
  24. let area = item[arr[i][2]].split(',').join(' ')
  25. let str = area + ' ' + item[arr[i][1]]
  26. _arr.push(str)
  27. } else {
  28. _arr.push([arr[i][0], Math.abs(item[arr[i][1]])])
  29. }
  30. } else {
  31. _arr.push(item[arr[i]])
  32. }
  33. }
  34. _result.push(_arr)
  35. })
  36. return _result
  37. }
  38. export function WtableClass (obj, arr) {
  39. if (obj.length === 0) {return []}
  40. obj.forEach((item, key) => {
  41. item.isDisabled = false
  42. })
  43. return obj
  44. }
  45. export function splitTableClass (obj, arr) {
  46. if (obj.length === 0) {return []}
  47. obj.forEach((item, keys) => {
  48. for (let i = 0; i < arr.attr.length; i++) {
  49. let _n = {}
  50. for (let j = 0; j < arr.thead.length; j++) {
  51. _n[arr.thead[j].id] = arr.attr[i].default
  52. }
  53. item[arr.attr[i].id] = _n
  54. }
  55. })
  56. console.log(obj)
  57. return obj
  58. }