hasontableUtils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const handsonUtils = {
  2. /**
  3. * 获取被排序后的数组
  4. *
  5. * @param changeData 发生改变的数据
  6. * @param source 数组
  7. *
  8. * @return array 经过排序后或者经过搜索后的数组
  9. */
  10. getParam: function(changeData, source, hot, trimmedArr) {
  11. function arrayUnique(arr, name){ //去重
  12. var hash = {};
  13. return arr.reduce(function(item, next) {
  14. hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
  15. return item;
  16. }, []);
  17. }
  18. let newData = arrayUnique(changeData, 0)
  19. let param = "";
  20. //被筛选过后的数组
  21. // let trimmedArr = this.trimmedRows();
  22. //是否启用了排序
  23. let isSort = hot.getPlugin("columnSorting").isSorted();
  24. if (trimmedArr.length && isSort) {
  25. //排序后的数组
  26. let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
  27. param = newData.map(item => {
  28. return hot.getSourceDataAtRow(trimmedArr[sortArr[item[0]]]);
  29. });
  30. } else if (isSort) {
  31. //排序后的数组
  32. let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
  33. param = newData.map(item => {
  34. return hot.getSourceDataAtRow(sortArr[item[0]]);
  35. });
  36. } else if (trimmedArr.length) {
  37. param = newData.map(item => {
  38. return hot.getSourceDataAtRow(trimmedArr[item[0]]);
  39. });
  40. } else {
  41. param = newData.map(item => {
  42. return hot.getSourceDataAtRow(item[0]);
  43. });
  44. }
  45. return param;
  46. },
  47. /**
  48. *
  49. * @param {handsontable修改参数} changeData
  50. * @param {*} source
  51. * @param {handsontabele实例} hot
  52. * @param {排序数组} trimmedArr
  53. *
  54. * @return 修改数值的前一个对象
  55. */
  56. getUnshiftParam: function(changeData, source, hot, trimmedArr) {
  57. //是否启用了排序
  58. let isSort = hot.getPlugin("columnSorting").isSorted();
  59. if (trimmedArr.length && isSort) {
  60. //排序后的数组
  61. let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
  62. return hot.getSourceDataAtRow(trimmedArr[sortArr[changeData[0][0] - 1]])
  63. } else if (isSort) {
  64. //排序后的数组
  65. let sortArr = hot.getPlugin("columnSorting").rowsMapper.__arrayMap;
  66. return hot.getSourceDataAtRow(sortArr[changeData[0][0] - 1])
  67. } else if (trimmedArr.length) {
  68. //进行了筛选
  69. return hot.getSourceDataAtRow(trimmedArr[changeData[0][0] - 1])
  70. } else {
  71. //没有进行排序和筛选
  72. return hot.getSourceDataAtRow(changeData[0][0] - 1);
  73. }
  74. },
  75. }
  76. export default handsonUtils