index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. Component({
  2. /**
  3. * 组件的属性列表
  4. */
  5. properties: {
  6. columnGap: {
  7. type: String,
  8. value: '20rpx'
  9. }
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. data: [],
  16. leftData: [],
  17. rightData: []
  18. },
  19. attached() {
  20. this._init();
  21. },
  22. pageLifetimes: {
  23. show() {
  24. this._init();
  25. }
  26. },
  27. /**
  28. * 组件的方法列表
  29. */
  30. methods: {
  31. _init() {
  32. wx.lin = wx.lin || {};
  33. wx.lin.renderWaterFlow = (data = [], refresh = false, success) => {
  34. if (Object.prototype.toString.call(data) !== '[object Array]') {
  35. console.error('[data]参数类型错误,渲染失败');
  36. return false;
  37. }
  38. // 绑定data,判断data是否为[]
  39. this.setData({ data });
  40. if (refresh) {
  41. this.data.leftData = [];
  42. this.data.rightData = [];
  43. }
  44. this._select(data, refresh).then(() => {
  45. success && success();
  46. });
  47. };
  48. },
  49. _select(data, refresh) {
  50. const query = wx.createSelectorQuery().in(this);
  51. this.columnNodes = query.selectAll('#left, #right');
  52. return new Promise((resolve) => {
  53. this._render(data, 0, refresh, () => {
  54. resolve();
  55. });
  56. });
  57. },
  58. _render(data, i, refresh, success) {
  59. if ((data.length > i || refresh) && this.data.data.length !== 0) {
  60. this.columnNodes.boundingClientRect().exec(res => {
  61. const rects = res[0];
  62. this.data.leftHeight = rects[0].height;
  63. this.data.rightHeight = rects[1].height;
  64. if (this.data.leftHeight <= this.data.rightHeight || refresh) {
  65. this.data.leftData.push(data[i]);
  66. } else {
  67. this.data.rightData.push(data[i]);
  68. }
  69. this.setData({
  70. leftData: this.data.leftData,
  71. rightData: this.data.rightData
  72. }, () => {
  73. this._render(data, ++i, false, success);
  74. });
  75. });
  76. } else {
  77. success && success();
  78. }
  79. }
  80. }
  81. });