utils.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * 判断是否是对象
  3. *
  4. * @export
  5. * @param {*} val
  6. * @returns true/false
  7. */
  8. export function isObject (val) {
  9. return Object.prototype.toString.call(val) === '[object Object]'
  10. }
  11. /**
  12. * 判断是否是Function
  13. *
  14. * @export
  15. * @param {*} val
  16. * @returns true/false
  17. */
  18. export function isFunction (val) {
  19. return Object.prototype.toString.call(val) === '[object Function]'
  20. }
  21. /**
  22. * 根据value获取columns信息
  23. *
  24. * @export
  25. * @param {*} { value, list, mode, props, level }
  26. * @param {number} [type=2] 查询不到value数据返回数据类型 1空值null 2默认第一个选项
  27. * @returns
  28. */
  29. export function getColumns ({ value, list, mode, props, level }, type = 2) {
  30. let pickerValue = []
  31. let pickerColumns = []
  32. let selectValue = []
  33. let selectItem = []
  34. let columnsInfo = null
  35. switch (mode) {
  36. case 'selector':
  37. let index = list.findIndex(item => {
  38. return isObject(item) ? item[props.value] === value : item === value
  39. })
  40. if (index === -1 && type === 1) {
  41. columnsInfo = null
  42. } else {
  43. index = index > -1 ? index : 0
  44. selectItem = list[index]
  45. selectValue = isObject(selectItem)
  46. ? selectItem[props.value]
  47. : selectItem
  48. pickerColumns = list
  49. pickerValue = [index]
  50. columnsInfo = {
  51. index: pickerValue,
  52. value: selectValue,
  53. item: selectItem,
  54. columns: pickerColumns
  55. }
  56. }
  57. break
  58. case 'multiSelector':
  59. const setPickerItems = (data = [], index = 0) => {
  60. if (!data.length) return
  61. const defaultValue = value || []
  62. if (index < level) {
  63. const value = defaultValue[index] || ''
  64. let i = data.findIndex(item => item[props.value] === value)
  65. if (i === -1 && type === 1) return
  66. i = i > -1 ? i : 0
  67. pickerValue[index] = i
  68. pickerColumns[index] = data
  69. if (data[i]) {
  70. selectValue[index] = data[i][props.value]
  71. selectItem[index] = data[i]
  72. setPickerItems(data[i][props.children] || [], index + 1)
  73. }
  74. }
  75. }
  76. setPickerItems(list)
  77. if (!selectValue.length && type === 1) {
  78. columnsInfo = null
  79. } else {
  80. columnsInfo = {
  81. index: pickerValue,
  82. value: selectValue,
  83. item: selectItem,
  84. columns: pickerColumns
  85. }
  86. }
  87. break
  88. case 'unlinkedSelector':
  89. list.forEach((item, i) => {
  90. let index = item.findIndex(item => {
  91. return isObject(item)
  92. ? item[props.value] === value[i]
  93. : item === value[i]
  94. })
  95. if (index === -1 && type === 1) return
  96. index = index > -1 ? index : 0
  97. const columnItem = list[i][index]
  98. const valueItem = isObject(columnItem)
  99. ? columnItem[props.value]
  100. : columnItem
  101. pickerValue[i] = index
  102. selectValue[i] = valueItem
  103. selectItem[i] = columnItem
  104. })
  105. pickerColumns = list
  106. if (!selectValue.length && type === 1) {
  107. columnsInfo = null
  108. } else {
  109. columnsInfo = {
  110. index: pickerValue,
  111. value: selectValue,
  112. item: selectItem,
  113. columns: pickerColumns
  114. }
  115. }
  116. break
  117. }
  118. return columnsInfo
  119. }