导出
This commit is contained in:
parent
53cad92a23
commit
15592ed2bd
|
|
@ -48,13 +48,21 @@ class Auditorder extends Backend
|
|||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
public function index($getArray = false, $page = 0, $input_limit = 0)
|
||||
{
|
||||
//当前是否为关联查询
|
||||
$this->relationSearch = true;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
if ($this->request->isAjax()) {
|
||||
|
||||
|
||||
if (false === $this->request->isAjax() && $getArray == false) {
|
||||
$res = $this->getSource();
|
||||
$this->view->assign("sources", $res);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
|
|
@ -62,7 +70,7 @@ class Auditorder extends Backend
|
|||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
|
||||
$list = $this->model
|
||||
$build = $this->model
|
||||
->scope('tab', Order::TAB_AUDIT)
|
||||
->alias('order')
|
||||
->with([
|
||||
|
|
@ -76,8 +84,17 @@ class Auditorder extends Backend
|
|||
}
|
||||
]])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
->order($sort, $order);
|
||||
|
||||
|
||||
if ($getArray) {
|
||||
$list = $build->paginate([
|
||||
'list_rows' => $input_limit,
|
||||
'page' => $page
|
||||
]);
|
||||
} else {
|
||||
$list = $build->paginate($limit);
|
||||
}
|
||||
|
||||
foreach ($list as $item) {
|
||||
if (isset($item->getRelation('source')->parent->title)) {
|
||||
|
|
@ -90,13 +107,10 @@ class Auditorder extends Backend
|
|||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||||
|
||||
return json($result);
|
||||
}
|
||||
$res = $this->getSource();
|
||||
$this->view->assign("sources", $res);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
|
|
@ -188,4 +202,42 @@ class Auditorder extends Backend
|
|||
$this->success();
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
$columns = json_decode($this->request->get('columns', ''), true);
|
||||
if (!$columns) {
|
||||
$sample = $this->index(true, 1, 1)->getData();
|
||||
|
||||
if (!isset($sample['rows'][0])) {
|
||||
return json(['code' => 0, 'msg' => '无数据可导出']);
|
||||
}
|
||||
$columns = [];
|
||||
foreach ($sample['rows'][0] as $key => $value) {
|
||||
$columns[] = ['field' => $key, 'title' => $key];
|
||||
}
|
||||
}
|
||||
|
||||
$generator = function () use ($columns) {
|
||||
$limit = 1000;
|
||||
$page = 1;
|
||||
$order = new \app\admin\model\Order();
|
||||
do {
|
||||
$result = $this->index(true, $page, $limit);
|
||||
$rows = $result->getData()['rows'] ?? [];
|
||||
if (!$rows) break;
|
||||
|
||||
foreach ($rows as $item) {
|
||||
$item->status = $order->getStatusList()[$item->status] ?? '';
|
||||
$item->amount_images = $this->dealImages($item->amount_images);
|
||||
$item->workerman->images = $this->dealImages($item->workerman->images);
|
||||
yield $item->toArray();
|
||||
}
|
||||
|
||||
$page++;
|
||||
} while (count($rows) === $limit);
|
||||
};
|
||||
|
||||
$this->streamCsv($columns, $generator(), '订单导出_' . date('Ymd_His') . '.csv');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@
|
|||
<div class="widget-body no-padding">
|
||||
<div id="toolbar" class="toolbar">
|
||||
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
|
||||
<a href="javascript:;" class="btn btn-success btn-export" id="btn-export">
|
||||
<i class="fa fa-download"></i> 导出
|
||||
</a>
|
||||
<!-- <a href="javascript:;" class="btn btn-success btn-add {:$auth->check('orders/auditorder/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
|
||||
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('orders/auditorder/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
|
||||
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('orders/auditorder/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
|
||||
|
|
|
|||
|
|
@ -784,7 +784,6 @@ class Backend extends Controller
|
|||
|
||||
$fields = array_column($columns, 'field');
|
||||
$titles = array_column($columns, 'title');
|
||||
|
||||
// 写入表头
|
||||
fputcsv($output, $titles);
|
||||
|
||||
|
|
@ -818,4 +817,18 @@ class Backend extends Controller
|
|||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function dealImages($image_str)
|
||||
{
|
||||
if (empty($image_str)){
|
||||
return '';
|
||||
}
|
||||
$images = explode(',', $image_str);
|
||||
$url = '';
|
||||
foreach ($images as $img) {
|
||||
if (empty($img)) continue;
|
||||
$url .= cdnurl($img) . ',';
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
|||
sortOrder: 'desc',
|
||||
fixedColumns: true,
|
||||
fixedRightNumber: 1,
|
||||
showExport: false,
|
||||
columns: [
|
||||
[
|
||||
// {checkbox: true},
|
||||
|
|
@ -132,6 +133,31 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
|
|||
]
|
||||
});
|
||||
|
||||
$('#btn-export').on('click', function () {
|
||||
var options = $("#table").bootstrapTable('getOptions');
|
||||
|
||||
// 提取列信息(不含 checkbox)
|
||||
var columns = [];
|
||||
$.each(options.columns[0], function (i, item) {
|
||||
if (item.field && !item.checkbox && !item.visible === false) {
|
||||
columns.push({
|
||||
field: item.field,
|
||||
title: item.title
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
var params = {
|
||||
columns: JSON.stringify(columns),
|
||||
filter: options.queryParams({}).filter || {},
|
||||
op: options.queryParams({}).op || {},
|
||||
sort: options.sortName,
|
||||
order: options.sortOrder
|
||||
};
|
||||
// console.log($.param(params))
|
||||
var url = '/admin/orders/auditorder/export?' + $.param(params);
|
||||
window.open(url); // 发起文件下载
|
||||
});
|
||||
// 为表格绑定事件
|
||||
Table.api.bindevent(table);
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user