闫增涛
2024-09-04 bfdd1a936731cf9b5f0d2c78d363cbb0ec93d941
树结构排序优化
2个文件已修改
25 ■■■■■ 已修改文件
packageBookService/pages/bookServices/detail/components/brief/index.wxml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
packageBookService/pages/bookServices/detail/index.js 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
packageBookService/pages/bookServices/detail/components/brief/index.wxml
@@ -6,7 +6,7 @@
      </view>
      <view class="title-name">基本信息</view>
    </view>
    <rich-text nodes="{{content}}" style="{{richStyle}}"></rich-text>
    <rich-text nodes="{{content}}" style="{{richStyle}}"></rich-text>
  </view>
  <view class="basic" wx:if="{{content}}">
    <view class="basic-title">
packageBookService/pages/bookServices/detail/index.js
@@ -11,6 +11,9 @@
   * 页面的初始数据
   */
  data: {
    // 进页面默认切换  展开的id
    // defaultactiveTeachid  教学资源
    // defaultactiveLearnid  云学习
    bookId: "",
    barHeight: "",
    navBarHeight: "",
@@ -819,7 +822,7 @@
  },
  // 扁平化数据转换tree
  getTreeList(rootList, pathLength, newArr, path, addNum) {
  getTreeList(rootList, pathLength, newArr, path) {
    for (const item of rootList) {
      // 此处原本 item.productLinkPath.length == pathLength 但 productLinkPath 长度个别书存在4、5位交错
      if ((pathLength - item.productLinkPath.length >= 0 && pathLength - item.productLinkPath.length <= 3) && item.productLinkPath.includes(path)) {
@@ -837,7 +840,7 @@
        }
        // newArr.push(item)
        // 在插入过程中对数据进行排序
        newArr = this.insertAndSortObjectsByProductLinkPath(newArr, item, addNum)
        newArr = this.insertAndSortObjectsByProductLinkPath(newArr, item)
        // 插进数组后 从原数据中删除
        rootList = rootList.filter(nitem => nitem.id != item.id)
      }
@@ -847,7 +850,7 @@
    for (const i of newArr) {
      i.children = []
      let addnum1 = i.productLinkPath.length - path.length == 6 ? 6 : 7
      this.getTreeList(rootList, pathLength + addnum1, i.children, i.productLinkPath, addnum1)
      this.getTreeList(rootList, pathLength + addnum1, i.children, i.productLinkPath)
      if (i.children.length == 0) {
        delete i.children
      }
@@ -872,9 +875,9 @@
  },
  // 排序数组 按照productLinkPath
  insertAndSortObjectsByProductLinkPath(array, newObj, addNum) {
  insertAndSortObjectsByProductLinkPath(array, newObj) {
    // 查找新对象应该插入的位置  
    let insertIndex = array.findIndex(obj => Number(newObj.productLinkPath.substring(newObj.productLinkPath.length - addNum, newObj.productLinkPath.length)) < Number(obj.productLinkPath.substring(obj.productLinkPath.length - addNum, obj.productLinkPath.length)));
    let insertIndex = array.findIndex(obj => Number(newObj.productLinkPath.substring(newObj.productLinkPath.lastIndexOf('\\') + 1, newObj.productLinkPath.length)) < Number(obj.productLinkPath.substring(obj.productLinkPath.lastIndexOf('\\') + 1, obj.productLinkPath.length)));
    // 如果没有找到合适的位置,则放在数组末尾  
    if (insertIndex === -1) {
      insertIndex = array.length;
@@ -884,10 +887,10 @@
    // 测试6 正式5
    // 对数组进行排序  
    array.sort((a, b) => {
      if (Number(a.productLinkPath.substring(a.productLinkPath.length - addNum, a.productLinkPath.length)) < Number(b.productLinkPath.substring(b.productLinkPath.length - addNum, b.productLinkPath.length))) {
      if (Number(a.productLinkPath.substring(a.productLinkPath.lastIndexOf('\\') + 1, a.productLinkPath.length)) < Number(b.productLinkPath.substring(b.productLinkPath.lastIndexOf('\\') + 1, b.productLinkPath.length))) {
        return -1;
      }
      if (Number(a.productLinkPath.substring(a.productLinkPath.length - addNum, a.productLinkPath.length)) > Number(b.productLinkPath.substring(b.productLinkPath.length - addNum, b.productLinkPath.length))) {
      if (Number(a.productLinkPath.substring(a.productLinkPath.lastIndexOf('\\') + 1, a.productLinkPath.length)) > Number(b.productLinkPath.substring(b.productLinkPath.lastIndexOf('\\') + 1, b.productLinkPath.length))) {
        return 1;
      }
      // a must be equal to b  
@@ -987,7 +990,7 @@
        }
        const addNum = query.cmsPath.length > 10 ? 7 : query.cmsPath.length > 13 ? 8 : 6
        const num = query.cmsPath.length + addNum
        this.getTreeList(res.datas.cmsDatas[0].datas, num, list, '\\', addNum)
        this.getTreeList(res.datas.cmsDatas[0].datas, num, list, '\\')
        list = this.ensureTreeConsistency(list)
        list = this.changeResourceChildren(list)
        let result = [];
@@ -995,7 +998,7 @@
          this.findChildIds(list, result);
          // const newUSeList = this.deleteTreeData(list, result)
          // console.log('树剔除', result, newUSeList);
          // console.log('树加一', this.increaseTreeData(list, newUSeList, 16094));
          this.setData({
            openTeachids: result,
            loading: false,
@@ -1101,7 +1104,7 @@
              const addNum = query.cmsPath.length > 10 ? 7 : query.cmsPath.length > 13 ? 8 : 6
              const num = query.cmsPath.length + addNum
              that.getTreeList(data, num, list, '\\', addNum)
              that.getTreeList(data, num, list, '\\')
              list = that.ensureTreeConsistency(list)
              that.findChildIds(list[0].children, result);
              that.setData({