useFlattenRecords.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = useFlattenRecords;
  6. var _vue = require("vue");
  7. // recursion (flat tree structure)
  8. function flatRecord(record, indent, childrenColumnName, expandedKeys, getRowKey, index) {
  9. const arr = [];
  10. arr.push({
  11. record,
  12. indent,
  13. index
  14. });
  15. const key = getRowKey(record);
  16. const expanded = expandedKeys === null || expandedKeys === void 0 ? void 0 : expandedKeys.has(key);
  17. if (record && Array.isArray(record[childrenColumnName]) && expanded) {
  18. // expanded state, flat record
  19. for (let i = 0; i < record[childrenColumnName].length; i += 1) {
  20. const tempArr = flatRecord(record[childrenColumnName][i], indent + 1, childrenColumnName, expandedKeys, getRowKey, i);
  21. arr.push(...tempArr);
  22. }
  23. }
  24. return arr;
  25. }
  26. /**
  27. * flat tree data on expanded state
  28. *
  29. * @export
  30. * @template T
  31. * @param {*} data : table data
  32. * @param {string} childrenColumnName : 指定树形结构的列名
  33. * @param {Set<Key>} expandedKeys : 展开的行对应的keys
  34. * @param {GetRowKey<T>} getRowKey : 获取当前rowKey的方法
  35. * @returns flattened data
  36. */
  37. function useFlattenRecords(dataRef, childrenColumnNameRef, expandedKeysRef, getRowKey) {
  38. const arr = (0, _vue.computed)(() => {
  39. const childrenColumnName = childrenColumnNameRef.value;
  40. const expandedKeys = expandedKeysRef.value;
  41. const data = dataRef.value;
  42. if (expandedKeys === null || expandedKeys === void 0 ? void 0 : expandedKeys.size) {
  43. const temp = [];
  44. // collect flattened record
  45. for (let i = 0; i < (data === null || data === void 0 ? void 0 : data.length); i += 1) {
  46. const record = data[i];
  47. temp.push(...flatRecord(record, 0, childrenColumnName, expandedKeys, getRowKey.value, i));
  48. }
  49. return temp;
  50. }
  51. return data === null || data === void 0 ? void 0 : data.map((item, index) => {
  52. return {
  53. record: item,
  54. indent: 0,
  55. index
  56. };
  57. });
  58. });
  59. return arr;
  60. }