Fastadmin大数据导出方法(自带的JS超过400条无法导出)
3097阅读
0评论
0点赞
Fastadmin自带导出功能,数据超过400条后,导出功能容易失效。 在此记录,基于后台的导出方法~ 后台需要导出的控制器中添加导出方法
public function export() { cookie('assets_export_time',time()); if ($this->request->isPost()) { set_time_limit(0); ini_set('memory_limit','2048M'); $search = $this->request->post('search'); $ids = $this->request->post('ids'); $filter = $this->request->post('filter'); $op = $this->request->post('op'); $columns = $this->request->post('columns'); //优先获取一条数据 $info = $this->model->find(); if(!$info) return $this->error('没有需要导出的数据'); //获取表字段 $columns = explode(',',$columns); $fields = implode(',',array_intersect($columns,array_keys($info->getData()))); $excel = new Spreadsheet(); $excel->getProperties() ->setCreator("文档编辑人") ->setLastModifiedBy("上次修改人") ->setTitle("文档标题") ->setSubject("Subject"); $excel->getDefaultStyle()->getFont()->setName('Microsoft Yahei'); $excel->getDefaultStyle()->getFont()->setSize(12); $excel->getDefaultStyle()->applyFromArray( array( 'fill' => array( 'type' => Fill::FILL_SOLID, 'color' => array('rgb' => '000000') ), 'font' => array( 'color' => array('rgb' => "000000"), ), 'alignment' => array( 'vertical' => Alignment::VERTICAL_CENTER, 'horizontal' => Alignment::HORIZONTAL_CENTER, 'indent' => 1 ), 'borders' => array( 'allborders' => array('style' => Border::BORDER_THIN), ) )); $worksheet = $excel->setActiveSheetIndex(0); $worksheet->setTitle('导出名称'); $whereIds = $ids == 'all' ? '1=1' : ['id' => ['in', explode(',', $ids)]]; $this->request->get(['search' => $search, 'ids' => $ids, 'filter' => $filter, 'op' => $op]); list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $line = 1; $list = []; $this->model ->field($fields) ->where($where) ->where($whereIds) ->chunk(100, function ($items) use (&$list, &$line, &$worksheet,&$columns) { $styleArray = array( 'font' => array( 'color' => array('rgb' => '000000'), 'size' => 12, 'name' => 'Verdana' )); $list = $items = collection($items)->toArray(); foreach ($items as $key => $v) { foreach ($v as $k => $ele) { $tmparray = explode("_text",$k); if(count($tmparray)>1){ if(array_key_exists($tmparray[0],$items[$key])){ $items[$key][$tmparray[0]] = $ele; }else if(array_key_exists($tmparray[0].'_id',$items[$key])){ $items[$key][$tmparray[0].'_id'] = $ele; }else{ if(!in_array($k,$columns)){ unset($items[$key][$k]); } continue; } unset($items[$key][$k]); } if(!in_array($k,$columns)){ unset($items[$key][$k]); } } } if($line === 1){ $first = array_keys($items[0]); $col = 1; foreach ($first as $index => $item) { $worksheet->setCellValueByColumnAndRow($col, 1,__($item)); $col++; } } foreach ($items as $index => $item) { $line++; $col = 1; foreach ($item as $field => $value) { $worksheet->setCellValueByColumnAndRow($col, $line, $value); $worksheet->getStyleByColumnAndRow($col, $line)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT); $worksheet->getCellByColumnAndRow($col, $line)->getStyle()->applyFromArray($styleArray); $col++; } } }); $excel->createSheet(); // Redirect output to a client’s web browser (Excel2007) $title = '导出名称'; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $title . '.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $objWriter = IOFactory::createWriter($excel, 'Xlsx'); $objWriter->save('php://output'); return; } }前台要导出的列表JS中的index方法中,加入JS代码
/** 自定义服务端导出 **/ var submitForm = function (ids, layero,index) { var options = table.bootstrapTable('getOptions'); console.log(options); var columns = []; $.each(options.columns[0], function (i, j) { if (j.field && !j.checkbox && j.visible && j.field != 'operate') { columns.push(j.field); } }); var search = options.queryParams({}); $("input[name=search]", layero).val(options.searchText); $("input[name=ids]", layero).val(ids); $("input[name=filter]", layero).val(search.filter); $("input[name=op]", layero).val(search.op); $("input[name=columns]", layero).val(columns.join(',')); $("form", layero).submit(); $('.layui-layer-content > div',layero).html("正在导出,请稍后~"); var old_export_time = $.cookie('assets_export_time'); var load_index = Layer.load(0); var timeIndex = setInterval(function (){ var export_time = $.cookie('assets_export_time'); if(old_export_time != export_time){ clearInterval(timeIndex); Layer.close(index); Layer.close(load_index); } },1000); }; var submitShow = function (){ var ids = Table.api.selectedids(table); var page = table.bootstrapTable('getData'); var all = table.bootstrapTable('getOptions').totalRows; console.log(ids, page, all); Layer.confirm("", { title: '导出数据', btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"], success: function (layero, index) { $(".layui-layer-btn a", layero).addClass("layui-layer-btn0"); } , yes: function (index, layero) { submitForm(ids.join(","), layero,index); return false; } , btn2: function (index, layero) { var ids = []; $.each(page, function (i, j) { ids.push(j.id); }); submitForm(ids.join(","), layero,index); return false; } , btn3: function (index, layero) { submitForm("all", layero,index); return false; } }) } //重新绑定导出点击事件,绑定自定义导出方法 $('div.export .dropdown-menu li').unbind().click(function (event){ event.stopPropagation(); submitShow(); return false; }); 由于JS方法中用到了jqurey的cookie插件,因此JS的模块引入中添加cookie模块,如下:请选择导出的选项
define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form,undefined) {
评论(0)
暂无评论