使用btn-addtabs新选项卡打开之后如何关闭或者返回 `返回上一层`
<a onclick="window.top.location.href = '你返回的路径url,或者用js事件获取上层路径'" href="javascript:"><i class="fa fa-arrow-left"></i></a>
如何导出下面的数据
添加按钮事件 class="btn-common-export"
<a href="javascript:;" class="btn btn-info btn-common-export" title="{:__('导出')}" ><i class="fa fa-upload"></i> {$title|default="导出"}</a>
JS部分,在require-table.js里新增这个
if ($(".btn-common-export", toolbar).size() > 0) {
const export_url = url//获取导出的路径,自己定义路径,就是你后端处理导出逻辑的路径
//自定义导出然后再导入相对格式
toolbar.on("click", ".btn-common-export", function () {
var ids = Table.api.selectedids(table); //当前页面选中的ID
var page = table.bootstrapTable("getData"); //当前页面的数据
var options = table.bootstrapTable("getOptions"); //获取table里面的元素
var params = options.queryParams({});//获取访问路径原来携带的参数
var all = options.totalRows;//获取总数量
var sort = options.sortName + " " + options.sortOrder;//获取原有排序
var filter = params.filter;//搜索过滤
var op = params.op;
// decodeURIComponent(filter);//解码%7B%22phone%22%3A%221%22%7D
var postData = {}; //post 传递值
postData["post_data"] = $("#post_data").val() ?? 0;//新增post传参
var getData = {}; // get 传递值
getData["sort"] = options.sortName;
getData["order"] = options.sortOrder;
getData["offset"] = !isNaN(options.pageSize)
? options.pageNumber * options.pageSize - options.pageSize
: 0;
getData["limit"] = !isNaN(options.pageSize)
? options.pageSize
: all;
getData["filter"] = filter;
getData["op"] = op;
// Object.assign(postData,getData)//post+get组合
var getUrl = export_url;
Layer.confirm("请选择导出的选项", {
title: "导出数据",
btn: [
"选中项(" + ids.length + "条)",
"本页(" + page.length + "条)",
"全部(" + all + "条)",
],
success: function (layero, index) {
$(".layui-layer-btn a", layero).addClass("toast-info");
},
yes: function (index, layero) {
if (ids.length == 0) {
Layer.alert("数据为空");
return true;
}
// 初始化为空
postData["ids"] = ids.join(",");
$.post(
export_url,
postData,
function (res) {
if (res.code == 1) {
Layer.alert(res.msg);//返回的信息
window.location.href = res.file;//返回的文件,使用了ajax就只能访问路径下载,之后后端可以弄个定时删除文件
Layer.close(index);//关闭弹窗
table.bootstrapTable("refresh");//刷新table,就是刷新页面
} else {
Layer.alert(res.msg);//返回的错误信息
}
},
"json"
);
return false;
},
btn2: function (index, layero) {
if (page.length == 0) {
Layer.alert("数据为空");
return true;
}
var queryString = $.param(getData);
getUrl += "?" + queryString;//携带fastadmin原有请求的参数
$.post(
getUrl,
postData,
function (res) {
if (res.code == 1) {
Layer.alert(res.msg);//返回的信息
window.location.href = res.file;//返回的文件,使用了ajax就只能访问路径下载,之后后端可以弄个定时删除文件
Layer.close(index);//关闭弹窗
table.bootstrapTable("refresh");//刷新table,就是刷新页面
} else {
Layer.alert(res.msg);//返回的错误信息
}
},
"json"
);
return false;
},
btn3: function (index, layero) {
layer.closeAll();//提前关闭弹窗,因为你如果导出数据量大的话会一直停在那里
if (page.length == 0) {
Layer.alert("数据为空");
return true;
}
getData["limit"] = all;//导出所有
var queryString = $.param(getData);
getUrl += "?" + queryString;
var loadIndex = layer.msg("正在导出...", {
icon: 16,
shade: 0.01,
time: 10000,
});
$.post(
getUrl,
postData,
function (res) {
if (res.code == 1) {
Layer.close(loadIndex);//关闭加载页面,就是一直转圈圈的那个
Layer.alert(res.msg);//弹出错误信息
window.location.href = res.file;//跳转下载路径
Layer.close(index);
table.bootstrapTable("refresh");
} else {
Layer.alert(res.msg);
Layer.close(loadIndex);
}
},
"json"
);
return false;
},
});
});
}
后端控制器代码,后端的还不够灵活,懒得弄
查看代码
// 导出数据
public function common_export()
{
// $name = $this->model->getTable();
// $alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
// $aliasName = $alias[$name] . '.';
if (false == $this->request->IsPost()) {
$this->error("请求导出错误~");
}
$this->request->filter(['strip_tags', 'trim']);//这个一定要,不然的话就过滤前端搜索无效
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
if($this->request->post("ids")){
$where_str['join.id'] = ["IN",$this->request->post("ids")];//指定ID导出
}
$list = $this->model
->field("id,weixin,phone,username")
->with(['join'=>function($query){
$query->withField('id,keywords,title');
}])
->where($where)
->where($where_str)
->order($sort, $order)
->paginate($limit);
$list = $list->items();
$data = Collection($list)->toArray();
$arrHeader = [
['field' => "id", "title" => "ID"],
['field' => "storetask.title", "title" => "标题"],
['field' => "username", "title" => "用户名"],
['field' => "phone", "title" => "手机号"],
['field' => "weixin", "title" => "微信"],
];
$sheet = new \app\admin\service\DataSpreadSheet();
$sheet->title = "导出标题";
$sheet->arrHeader = $arrHeader;//需要导出的Header或者就是字段
$sheet->data = $data;//导出的的数据
$export = $sheet->export($header = false, $download = false);//header 是否只显示头部,用户导入模本下载,download true : 强制下载 false : 保存到指定目录输出文件地址
if($export == false){
$this->error("数据为空~");
}
return json($export);
}
导出的代码,需要下载PhpOffice,
查看代码
<?php
/*
* @Date: 2023-07-28 13:44:53
* @LastEditors: JSYPHP
* @Author: JSYPHP@[email protected]
* @Description: 记得在杂乱无章的生活,偶尔带着一丝笑意
* QQ:[email protected]
*/
namespace app\admin\service;//自己处理逻辑的控制器
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use Exception;
use think\Controller;
use think\Db;
/**
* Class DataSpreadSheet
* @备注:导入导出
* @package app\admin\service
*/
class DataSpreadSheet extends Controller{
// 解析文件并且需要返回的数据
public $fieldArr = [];
// 导出数据
public $data = [];
// 表头
public $arrHeader;
// 导出标题
public $title = "项目汇总";
/**
* @作者: JSYPHP@[email protected]
* @备注: 获取文件解析并返回数据
* @return {*}
*/
public function readImportData($isJson = true){
if($this->request->isPost()){
$file = $this->request->request('file');
if (!$file) {
$this->error(__('Parameter %s can not be empty', 'file'));
}
$filePath = ROOT_PATH . DS . 'public' . DS . $file;
if (!is_file($filePath)) {
$this->error(__('No results were found'));
}
//实例化reader
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
if (!in_array($ext, ['xls', 'xlsx'])) {
$this->error(__('Unknown data format'));
}
if ($ext === 'xls') {
$reader = new Xls();
} else {
$reader = new Xlsx();
}
//加载文件
$insert = [];
try {
if (!$PHPExcel = $reader->load($filePath)) {
$this->error(__('Unknown data format'));
}
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
$fields = [];
for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$fields[] = $val;
}
}
$fieldArr = $this->fieldArr;
if(empty($fieldArr) && !is_array($fieldArr))
{
$this->error(__("field解析数据错误~"));
}
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
$values = [];
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$values[] = is_null($val) ? '' : $val;
}
$row = [];
$temp = array_combine($fields, $values);
foreach ($temp as $k => $v) {
if (isset($fieldArr[$k]) && $k !== '') {
if(isset($fieldArr[$k]['table'])){
$row[$fieldArr[$k]['field']] = Db::name($fieldArr[$k]['table'])->where($fieldArr[$k]['where'],$v)->value("id")??$fieldArr[$k]['default'];
}else{
$row[$fieldArr[$k]['field']] = $v;
}
}
}
if ($row) {
$insert[] = $row;
}
}
} catch (Exception $exception) {
$this->error($exception->getMessage());
}
if (!$insert) {
$this->error(__('No rows were updated'));
}
if($isJson){
return json(['data'=>$insert,'code'=>1,'msg'=>'数据读取成功~']);
}
return $insert;
}
$this->error("访问错误~");
}
/**
* 导出Excel方法
* header 是否只显示头部,用户导入模本下载
* download true : 强制下载 false : 保存到指定目录输出文件地址
*/
public function export($header = false , $download = true)
{
set_time_limit(0);
ini_set ('memory_limit', '512M');
if($header){
$arr = [];
}else{
if (!($arr = $this->data)) {
return false;
}
}
//实例化
$objExcel = new Spreadsheet();
//设置内容;
$objActSheet = $objExcel->getActiveSheet();
//设置文档属性
$objWriter = IOFactory::createWriter($objExcel, "Xlsx");
// 定义字母表头
$letter = explode(',', "A,B,C,D,E,F,G,H,I,J,K,L,M,N");
//设置表头
$arrHeader = $this->arrHeader;
// 设置标题
$objActSheet->setTitle($this->title);
//填充表头信息 A1:用户ID、B1:用户名、C1:昵称
foreach($arrHeader as $hk=>$hv){
$objActSheet->setCellValue("$letter[$hk]1", $hv['title']);
foreach ($arr as $k => $v){
$jk = $k;
$k += 2;
if(isset($hv['url'])){
if($v[$hv['field']]){
$objActSheet->setCellValue($letter[$hk] . $k, request()->domain().$v[$hv['field']]);
}
}else if(isset($hv['mulfield'])){
$objActSheet->setCellValue($letter[$hk] . $k, $v[$hv['field']]['title']);
}else if(isset($hv['is_img'])){
if($v[$hv['field']]){
$num = 10;
foreach ($v[$hv['field']] as $k1=>$v1){
if($v1 != ""){
$objDrawing[$k] = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$objDrawing[$k]->setName('');
$objDrawing[$k]->setDescription('');
//读取图片路径
$objDrawing[$k]->setPath(request()->domain().$v1);
$objDrawing[$k]->setWidth(80);
$objDrawing[$k]->setHeight(80);
$objDrawing[$k]->setCoordinates($letter[$hk] . $k);
$objDrawing[$k]->setOffsetX($num);
$objDrawing[$k]->setOffsetY(20);
$objDrawing[$k]->setWorksheet($objActSheet);
$objActSheet->getRowDimension($k)->setRowHeight(100);
$num = $num + 70; // 增加每张图之间的间距
}
}
}
}else{
//设置表格内容,这里是关联路径
if (strpos($hv['field'], '.') !== false) {
// 拆分字段字符串为数组
$fields = explode('.', $hv['field']);//这里默认只能输第二级。比如admin.user,如果想用admin.user.school,可以查看https://www.cnblogs.com/jsyphp/p/17617577.html
if (isset($v[$fields[0]][$fields[1]])) {
$objActSheet->setCellValue($letter[$hk] . $k, $v[$fields[0]][$fields[1]]);
}
}else{
if(isset($v[$hv['field']])){
$objActSheet->setCellValue($letter[$hk] . $k, $v[$hv['field']]);
}
}
}
//设置表格的宽度
$objActSheet->getColumnDimension($letter[$hk])->setWidth(40);
}
}
$outfile = $this->title . date("YmdHis",time()) . ".xlsx";//导出的名字
if($download){
// 清空输出缓冲区
ob_end_clean();
// 告诉浏览器强制下载
header("Content-Type: application/force-download");
// 二进制文件类型
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// 设置表名
header('Content-Disposition:inline;filename="' . $outfile . '"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
}else{
$file = ROOT_PATH . DS . 'public' . DS . 'uploads' . DS . 'xls' . DS . date("Y-m-d",time()) . DS;
if(!is_dir($file)){
mkdir($file, 0777, true);
}
$objWriter->save($file.$outfile);//保存到服务端再返回路径下载
$download_file = DS . 'uploads' . DS . 'xls' . DS . date("Y-m-d",time()) . DS . $outfile;
return ["code"=>1,'file' => $download_file,'msg'=>'导出成功'];
}
exit();
}//fun end
}
后续待开发。。。,后续会放出crm系统代码,功能比较少,你们参考里面的功来学习也行。。。。。
标签:基本,Layer,fastadmin,复杂,res,title,导出,field,file From: https://www.cnblogs.com/jsyphp/p/17701834.html