utils.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function getColumnValue(columnItem) {
  2. if (typeof columnItem === 'object')
  3. return columnItem.value;
  4. return columnItem;
  5. }
  6. export function getStrictMatchedItemByValue(columns, value, single) {
  7. if (single) {
  8. value = [value];
  9. }
  10. var matchedValues = [];
  11. var matchedColumn = [];
  12. var index = null;
  13. var _loop_1 = function (i) {
  14. var column = columns[i];
  15. var compareValue = (value || [])[i];
  16. index = column.findIndex(function (c) {
  17. var columnValue = getColumnValue(c);
  18. return columnValue === compareValue;
  19. });
  20. matchedColumn[i] = column[index];
  21. matchedValues[i] = getColumnValue(column[index]);
  22. };
  23. for (var i = 0; i < columns.length; i++) {
  24. _loop_1(i);
  25. }
  26. return {
  27. matchedColumn: single ? matchedColumn === null || matchedColumn === void 0 ? void 0 : matchedColumn[0] : matchedColumn,
  28. matchedValues: single ? matchedValues === null || matchedValues === void 0 ? void 0 : matchedValues[0] : matchedValues,
  29. };
  30. }
  31. // 如果找不到value对应的item项目,返回第一项
  32. export function getMatchedItemByValue(columns, value, single) {
  33. if (single) {
  34. value = [value];
  35. }
  36. var matchedValues = [];
  37. var matchedColumn = [];
  38. var index = null;
  39. var _loop_2 = function (i) {
  40. var column = columns[i];
  41. var compareValue = (value || [])[i];
  42. if (compareValue === undefined || compareValue === null) {
  43. index = 0;
  44. }
  45. else {
  46. index = column.findIndex(function (c) {
  47. var columnValue = getColumnValue(c);
  48. return columnValue === compareValue;
  49. });
  50. if (index === -1) {
  51. index = 0;
  52. } // 没有找到, 默认选择第一个
  53. }
  54. matchedColumn[i] = column[index];
  55. matchedValues[i] = getColumnValue(column[index]);
  56. };
  57. for (var i = 0; i < columns.length; i++) {
  58. _loop_2(i);
  59. }
  60. return {
  61. matchedColumn: single ? matchedColumn[0] : matchedColumn,
  62. matchedValues: single ? matchedValues[0] : matchedValues,
  63. };
  64. }
  65. export function getMatchedItemByIndex(columns, selectedIndex, single) {
  66. var _a;
  67. var matchedValues = [];
  68. var matchedColumn = [];
  69. var index = null;
  70. for (var i = 0; i < columns.length; i++) {
  71. var column = columns[i];
  72. var compareValue = selectedIndex[i];
  73. index = null;
  74. if (compareValue === undefined || compareValue === null) {
  75. index = 0;
  76. }
  77. else {
  78. index = compareValue;
  79. // 当column变化时, picker-view onChange 里抛出来的selectedIndex有可能不正确
  80. if (((_a = columns === null || columns === void 0 ? void 0 : columns[i]) === null || _a === void 0 ? void 0 : _a[compareValue]) === undefined) {
  81. index = 0;
  82. }
  83. if (index === -1) {
  84. index = 0;
  85. } // 没有找到, 默认选择第一个
  86. }
  87. matchedColumn[i] = column[index];
  88. matchedValues[i] = getColumnValue(column[index]);
  89. }
  90. return {
  91. matchedColumn: single ? matchedColumn[0] : matchedColumn,
  92. matchedValues: single ? matchedValues[0] : matchedValues,
  93. };
  94. }