关于javascript:后端分页每页的数量在后端定死的情况下进行后端导出所有数据递归解决接口和分页数据放到excel流

40次阅读

共计 2160 个字符,预计需要花费 6 分钟才能阅读完成。

// 后端每页返回的数据拼接的数组

  eventsConcatResult = (events: any[]) => {let eventExcel:any[]=[]
        for (let j = 0; j < events.length; j++) {let array = [];
          array.push(events[j].camera['_id'])
          array.push(events[j].camera.alias)
          array.push((events[j].camera.parent && events[j].camera.parent['_id']) ? events[j].camera.parent['_id'] : "无")
          array.push((events[j].camera.parent.parent && events[j].camera.parent.parent['_id']) ? events[j].camera.parent.parent['_id'] : "无")
          array.push(events[j].category)
          switch (events[j].category) {
            case 0:
              array.push("产品")
              break;
            case 1:
              array.push("报警")
              break;
            case -1:
              array.push("未知")
              break;
            default:
              break;
          }
          array.push(events[j].code)
          switch (events[j].code) {
            case 1:
              array.push("人进入")
              break;
            case 2:
              array.push("人来到")
              break;
            case 3:
              array.push("人停留")
              break;
            case -1:
              array.push("未知")
              break;
            default:
              break;
          }
          array.push(`${this.utilsService.$server}${events[j].video_url}`)
          array.push(events[j].event_timeStamp)
          array.push(events[j].timeStamp)
          eventExcel.push(array)
        }
    return eventExcel;
  };

// 采纳递归的办法实现循环内异步调用接口,并且循环一页将这页拼接的数组放到 excel 流里。

  excel(condition: EventSearchCondition) {this.wb= XLSX.utils.book_new();
    this.ws = XLSX.utils.aoa_to_sheet(this.excelData);
    this.utilsService.$http.post<any>(this.utilsService.$server + '/api/events', {
      condition: condition,
      sort: this.sort.active || null,
      order: this.sort.direction || null,
      pageNumber: 0,
      entriesPerPage: this.entriesPerPage
    }).subscribe(async (events: any) => {let pagesTotalCount = Math.ceil(events.totalEvents / this.entriesPerPage)-1
      const fetchSinglePage = (pageNumber: number): void => {if (pageNumber>pagesTotalCount){console.log(this.ws)
          XLSX.utils.book_append_sheet(this.wb, this.ws, 'event');
          XLSX.writeFile(this.wb, '事件记录.xlsx');
          return;
        }
        this.utilsService.$http.post<any>(this.utilsService.$server + '/api/events', {
          condition: condition,
          sort: this.sort.active || null,
          order: this.sort.direction || null,
          pageNumber:pageNumber,
          entriesPerPage: this.entriesPerPage
        }).subscribe((item: any) => {XLSX.utils.sheet_add_aoa(this.ws, this.eventsConcatResult(item.eventsList),{origin: -1});
          // console.log(this.ws)
          fetchSinglePage(pageNumber + 1)
        })
      }
      fetchSinglePage(0)
    })

  }

// 调用接口

  async ExportExcel(){
    // 获取原生的所有 events
    await this.excel(this.searchCondition)
  }

// 前端按钮

<button mat-button color="primary" (click)="ExportExcel()">{{ 'Export' | translate}}</button>

正文完
 0