Fastadmin后台列表导出到表格

html中添加按钮

						<a href="javascript:;" class="btn btn-success btn-export" title="{:__('导出数据')}" ><i class="fa fa-cloud-download"></i> {:__('导出数据')}</a>

对应的js添加代码处理点击事件,添加到初始化表格前

// 修改导出按钮点击事件处理逻辑$(document).on('click', '.btn-export', function() {Layer.confirm('确认根据筛选条件导出数据?不选择创建时间默认筛选最近30天', {offset: 'auto',btn: ['确定', '取消']}, function(index) {// 获取表格对象var table = $('#table');// 获取表格的当前查询参数var queryParams = table.bootstrapTable('getOptions').queryParams;// 构建查询参数对象var params = {};if (typeof queryParams === 'function') {params = queryParams({page: 1, pageSize: 10});}// 添加location.search中的参数var searchParams = Fast.api.query(location.search);$.extend(params, searchParams);// 构建完整的导出URLvar baseUrl = 'order/export';var exportUrl = baseUrl + '?' + $.param(params);// 创建隐藏的a标签用于触发下载const downloadLink = document.createElement('a');downloadLink.style.display = 'none';document.body.appendChild(downloadLink);// 使用fetch API获取文件流fetch(exportUrl).then(response => {// 检查响应状态if (!response.ok) {throw new Error('服务器响应错误: ' + response.status);}// 检查响应内容类型const contentType = response.headers.get('content-type');if (!contentType || !contentType.includes('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) {// 如果不是Excel文件,尝试解析为JSON错误信息return response.json().then(errorData => {throw new Error(errorData.msg || '导出失败: 服务器返回无效内容');}).catch(() => {throw new Error('导出失败: 服务器返回无效内容');});}// 生成文件名const now = new Date();const year = now.getFullYear();const month = String(now.getMonth() + 1).padStart(2, '0');const day = String(now.getDate()).padStart(2, '0');const hours = String(now.getHours()).padStart(2, '0');const minutes = String(now.getMinutes()).padStart(2, '0');const filename = `订单导出_${year}-${month}-${day}_${hours}${minutes}.xlsx`;return response.blob().then(blob => ({ blob, filename }));}).then(({ blob, filename }) => {// 创建对象URL并触发下载const url = window.URL.createObjectURL(blob);downloadLink.href = url;downloadLink.download = filename;downloadLink.click();// 清理资源window.URL.revokeObjectURL(url);document.body.removeChild(downloadLink);Layer.close(index);}).catch(error => {console.error('导出失败:', error);Layer.alert('文件导出失败: ' + error.message, { title: '错误' });document.body.removeChild(downloadLink);Layer.close(index);});}, function(index) {Layer.close(index);});return false;});// 初始化表格table.bootstrapTable({url: $.fn.bootstrapTable.defaults.extend.index_url,templateView: true,pk: 'id',sortName: 'id',columns: [

后端处理导出的方法

    /*** 数据导出*/public function export(){//设置过滤方法$this->request->filter(['strip_tags', 'trim']);$filter = $this->request->get("filter", '');$filter = (array)json_decode($filter, true);$filter = $filter ? $filter : [];list($where, $sort, $order, $offset, $limit) = $this->buildparams();//订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=售后订单(已弃用),6=已完成,7=已取消 -1 待核销if (!empty($filter['company_id'])) {$this->model->whereRaw("FIND_IN_SET({$filter['company_id']}, company_id)");}$pid = [];if (!empty($filter['pid'])) {$uid = User::where('parent_user_id', $filter['pid'])->column('id');$pid = $uid;}$team = [];if (!empty($filter['team_id'])) {$ids = self::forGetUser($filter['team_id']);$team = $ids;}if (!empty($filter['createtime'])) {$time = explode(' - ', $filter['createtime']);$create_time = [strtotime($time[0]), strtotime($time[1])];}else{$create_time = [time()-(86400*30), time()];}$pay_time = [];if (!empty($filter['paymenttime'])) {$time = explode(' - ', $filter['paymenttime']);$pay_time = [strtotime($time[0]), strtotime($time[1])];}$is_jd = -1;if (!empty($filter['is_jd'])) {$is_jd = $filter['is_jd'];}$shop_type = $filter['shop_type'] ?? -1;$state = [3, 4, 5, 6, 7, 8, 9];$list = $this->model->with(['user', 'shop', 'ordergoods'])->where(function ($query) use ($filter, $state, $pid, $team, $create_time, $pay_time,$shop_type,$is_jd) {if (!empty($filter['state']) && $filter['state'] == '-1') {$query->where('type', 1)->where('hx_status', 1);}if (!empty($filter['state']) && $filter['state'] == '2') {$query->where('type', 2)->where('fa_wanlshop_order.state', 2);}if (!empty($filter['state']) && in_array($filter['state'], $state)) {$query->where('fa_wanlshop_order.state', $filter['state']);}if (!empty($filter['order_no'])) {$query->where('fa_wanlshop_order.order_no', $filter['order_no']);}if (!empty($pid)) {$query->whereIn('fa_wanlshop_order.user_id', $pid);}if (!empty($team)) {$query->whereIn('fa_wanlshop_order.user_id', $team);}if (!empty($filter['express_no'])) {$query->where('fa_wanlshop_order.express_no', $filter['express_no']);}if (!empty($create_time)) {$query->whereBetween('fa_wanlshop_order.createtime', $create_time);}if (!empty($pay_time)) {$query->whereBetween('fa_wanlshop_order.paymenttime', $pay_time);}if ($shop_type >= 0) {$query->where('fa_wanlshop_order.shop_type', $shop_type);}if ($is_jd > 0) {$ids = \db('wanlshop_order_goods')->where('is_jd',$is_jd)->column('order_id');$query->whereIn('fa_wanlshop_order.id', $ids);}})->order('id', $order)->select();// 收集所有订单ID$orderIds = array_column($list, 'id');// 一次查询所有支付信息$pays = model('app\admin\model\wanlshop\Pay')->whereIn('order_id', $orderIds)->field('order_id, type, pay_no, price, order_price, freight_price, discount_price, actual_payment')->select();// 构建支付信息映射:order_id => type => pay_info$payMap = [];foreach ($pays as $pay) {if (!isset($payMap[$pay['order_id']])) {$payMap[$pay['order_id']] = [];}$payMap[$pay['order_id']][$pay['type']] = $pay;}// 赋值支付信息foreach ($list as $row) {$row->getRelation('user')->visible(['username', 'nickname']);$row->getRelation('shop')->visible(['shopname']);// 根据订单类型获取对应的支付信息if ($row['live_id'] > 0) {$row->pay = isset($payMap[$row['id']]['live']) ? $payMap[$row['id']]['live'] : null;} else {if ($row['activity_id'] == 0) {$row->pay = isset($payMap[$row['id']]['goods']) ? $payMap[$row['id']]['goods'] : null;} else {$row->pay = isset($payMap[$row['id']]['seckill']) ? $payMap[$row['id']]['seckill'] : null;}}}// 生成Excel文件$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();// 设置表头$sheet->setCellValue('A1', '订单ID');$sheet->setCellValue('B1', '订单编号');$sheet->setCellValue('C1', '用户名称');$sheet->setCellValue('D1', '用户电话');$sheet->setCellValue('E1', '配送类型');$sheet->setCellValue('F1', '创建时间');$sheet->setCellValue('G1', '状态');$sheet->setCellValue('H1', '实际支付');$sheet->setCellValue('I1', '含运费');$sheet->setCellValue('J1', '店铺');// 填充数据$rowIndex = 2;foreach ($list as $order) {$sheet->setCellValue('A' . $rowIndex, $order['id']);// 将订单编号设置为文本格式$sheet->setCellValueExplicit('B' . $rowIndex, $order['order_no'], \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);$sheet->setCellValue('C' . $rowIndex, $order['user']['nickname'] ?? '');$sheet->setCellValueExplicit('D' . $rowIndex, $order['user']['mobile'] ?? '', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);$sheet->setCellValue('E' . $rowIndex, $order['type_text'] ?? '');$sheet->setCellValue('F' . $rowIndex, $order['createtime_text'] ?? '');$sheet->setCellValue('G' . $rowIndex, $order['state_text'] ?? '');$sheet->setCellValue('H' . $rowIndex, $order['pay']['order_price'] ?? '');$sheet->setCellValue('I' . $rowIndex, $order['pay']['freight_price'] ?? '');$sheet->setCellValue('J' . $rowIndex, $order['shop']['shopname'] ?? '');$rowIndex++;}// 设置响应头,返回Excel文件header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');header('Content-Disposition: attachment;filename="订单数据导出_' . date('YmdHis') . '.xlsx"');header('Cache-Control: max-age=0');$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);$writer->save('php://output');exit;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/95617.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/95617.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Nginx反向代理与缓存实现

1. Nginx反向代理核心配置解析 1.1 反向代理基础配置结构 Nginx反向代理的基础配置结构主要包括server块和location块的配置。一个典型的反向代理配置示例如下&#xff1a; server {listen 80;server_name example.com;location / {proxy_pass http://backend_servers;proxy_se…

第2节 如何计算神经网络的参数:AI入门核心逻辑详解

🎯 核心目标:找到最佳w和b! 上期咱们聊了神经网络就是复杂的"线性变换+激活函数套娃",今天的重头戏就是:怎么算出让模型完美拟合数据的w(权重)和b(偏置)!先从最简单的线性函数说起,一步步揭开神秘面纱 那么如何计算w和b呢?首先明确我们需要的w和b能够让…

AutoSar AP平台功能组并行运行原理

在 AUTOSAR Adaptive Platform&#xff08;AP&#xff09;中&#xff0c;同一个机器上可以同时运行多个功能组&#xff08;Function Groups&#xff09;&#xff0c;即使是在单核CPU环境下。其调度机制与进程调度既相似又存在关键差异&#xff0c;具体实现如下&#xff1a;功能…

linux服务器查看某个服务启动,运行的时间

一 查看服务启动运行时间1.1 查看启动时间查看启动时间&#xff08;精确到秒&#xff09;&#xff1a;ps -p <PID> -o lstart例子如下&#xff1a;ps -p 1234 -o lstart1.2 查询运行时长ps -p <PID> -o etimeps -p 1234 -o etime1.3 总结

【JS 性能】前端性能优化基石:深入理解防抖(Debounce)与节流(Throttle)

【JS 性能】前端性能优化基石&#xff1a;深入理解防抖&#xff08;Debounce&#xff09;与节流&#xff08;Throttle&#xff09; 所属专栏&#xff1a; 《前端小技巧集合&#xff1a;让你的代码更优雅高效》 上一篇&#xff1a; 【JS 语法】代码整洁之道&#xff1a;解构赋值…

线性代数 · 直观理解矩阵 | 空间变换 / 特征值 / 特征向量

注&#xff1a;本文为 “线性代数 直观理解矩阵” 相关合辑。 英文引文&#xff0c;机翻未校。 如有内容异常&#xff0c;请看原文。 Understanding matrices intuitively, part 1 直观理解矩阵&#xff08;第一部分&#xff09; 333 March 201120112011 William Gould Intr…

设计模式基础概念(行为模式):策略模式

概述 策略模式是一种行为设计模式&#xff0c; 它能让你定义一系列算法&#xff0c; 并将每种算法分别放入独立的类中&#xff0c; 以使算法的对象能够相互替换。 主要目的是通过定义相似的算法&#xff0c;替换if else 语句写法&#xff0c;并且可以随时相互替换 结构示例 策略…

功能组和功能组状态的概念关系和区别

在 AUTOSAR Adaptive Platform 中&#xff0c;功能组&#xff08;Function Group&#xff0c;FG&#xff09; 和 功能组状态&#xff08;Function Group State&#xff09; 是状态管理&#xff08;SM&#xff09;的核心概念&#xff0c;二者构成静态逻辑单元与动态行为模式的协…

力扣326:3的幂

力扣326:3的幂题目思路代码题目 给定一个整数&#xff0c;写一个函数来判断它是否是 3 的幂次方。如果是&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 整数 n 是 3 的幂次方需满足&#xff1a;存在整数 x 使得 n 3^x 思路 想要是三的幂次方的话将这个…

前瞻性技术驱动,枫清科技助力制造企业借助大模型完成生产力转化

麦肯锡于近期发布的《技术趋势展望2025》更清晰地定义了AI的角色与发展方向。报告在不止一个章节总结了基础模型加速小型化的趋势&#xff0c;多模态融合成为主流&#xff1a;企业的模型利用从追求“大而全”转向“小而精”&#xff0c;高效专用小模型成本降低90%的同时保持性能…

如何远程连接云服务器上mysql

一&#xff1a;使用系统命令查看端口占用# 查看MySQL进程及其端口sudo netstat -tlnp | grep mysql# 或者使用ss命令sudo ss -tlnp | grep mysql# 查看3306端口&#xff08;MySQL默认端口&#xff09;sudo netstat -tlnp | grep 3306出现如下信息&#xff0c;说明端口3306[root…

今日分享:C++模板(全详解)

&#x1f60e;【博客主页&#xff1a;你最爱的小傻瓜】&#x1f60e; &#x1f914;【本文内容&#xff1a;C模板 &#x1f60d; 】&#x1f914; -------------------------------------------------------------------------------------------------------------------…

ramdisk内存虚拟盘(一)——前世今生

1990 年代&#xff1a;前因——“硬盘太慢、驱动太多” 背景&#xff1a;早期 Linux 根文件系统要么在软盘、要么在 IDE 硬盘&#xff0c;内核把对应的软盘/IDE 驱动编进去即可顺利挂载。矛盾出现&#xff1a;随着 SCSI、PCMCIA、USB、RAID 控制器等百花齐放&#xff0c;如果把…

ETH持续上涨推动DEX热潮,交易活跃度飙升的XBIT表现强势出圈

BOSS Wallet 8月15日讯&#xff0c;随着ETH价格在过去24小时内强势拉升至4300美元&#xff0c;整个加密市场再度掀起涨势狂潮&#xff0c;链上交易活跃度空前高涨。其中&#xff0c;去中心化交易所平台迅速成为市场焦点&#xff0c;其平台活跃度与交易量双双上涨&#xff0c;吸…

Stand-In - 轻量级人物一致性视频生成 高保真视频人脸交换 ComfyUI工作流 支持50系显卡 一键整合包下载

Stand-In 是一个轻量级、即插即用的身份保护视频生成框架&#xff0c;只需要上传一张人物照片&#xff0c;加上一段提示词&#xff0c;即可生成高度一致性的高保真人物视频&#xff0c;人脸相似度和自然都几乎达到100%还原水平。 Stand-In 能把任何一张人脸&#xff08;甚至动漫…

vue3相关基础

1、ref和reactive的区别两者都是响应式数据的声明。Reactive只适用于非基本数据类型&#xff0c;如对象&#xff0c;数组等。而ref是兼容适用于reactive的的数据类型的以及其他数据&#xff0c;灵活性较高。ref声明的变量取值时需要.value。在<template></template>…

云手机存储和本地存储的区别

云手机存储通常指云存储&#xff0c;即数据存储在云端服务器&#xff0c;本地存储则是将数据存储在用户设备硬件中&#xff0c;主要区别体现在存储位置、访问方式、依赖条件等多个方面&#xff0c;具体如下&#xff1a;本地存储主要是将数据存储在用户自有设备的物理硬件中&…

【科研绘图系列】R语言绘制三维曲线图

文章目录 介绍 加载R包 数据下载 导入数据 数据预处理 画图 系统信息 参考 介绍 【科研绘图系列】R语言绘制三维曲线图 加载R包 library(tidyverse) library(ggsignif) library(RColorBrewer) library(dplyr) library(reshape2) library(grid

python常用包

以下按类别列举10个常用Python包&#xff0c;并以一句话概括其核心作用&#xff1a; 一、数据分析与科学计算 NumPy&#xff1a;提供高性能多维数组及数学运算&#xff0c;是数值计算的基础库。Pandas&#xff1a;通过DataFrame结构实现高效表格数据清洗、分析与处理。SciPy&am…

“ 船新版本 ”

在 GeeLark 最新版本中&#xff0c;增强了 AIGC 生成能力以及 AI 协助自定义任务开发功能&#xff0c;给用户优化构建从内容生产到运营自动化的完整技术链&#xff0c;为跨境电商及企业用户提供更完善的智能化解决方案&#xff0c;效率翻倍轻松出海。 AIGC 接入 MiniMax-Hailuo…