kartik-v/yii2-grid
文档http://demos.krajee.com/grid
在win7下
composer require kartik-v/yii2-grid "@dev"
在vender下多了kartik-v,kartik-v下yii2-export,yii2-grid,yii2-krajee-base,yii2-mpdf
还修改了,vendor\yiisoft\extensions.php
'kartik-v/yii2-krajee-base' => array ( 'name' => 'kartik-v/yii2-krajee-base', 'version' => '1.8.1.0', 'alias' => array ( '@kartik/base' => $vendorDir . '/kartik-v/yii2-krajee-base', ), ), 'kartik-v/yii2-grid' => array ( 'name' => 'kartik-v/yii2-grid', 'version' => '2.7.0', 'alias' => array ( '@kartik/grid' => $vendorDir . '/kartik-v/yii2-grid', ), ), 'kartik-v/yii2-export' => array ( 'name' => 'kartik-v/yii2-export', 'version' => 'v1.2.0', 'alias' => array ( '@kartik/export' => $vendorDir . '/kartik-v/yii2-export', ), ), 'kartik-v/yii2-mpdf' => array ( 'name' => 'kartik-v/yii2-mpdf', 'version' => 'v3.3.0', 'alias' => array ( '@kartik/mpdf' => $vendorDir . '/kartik-v/yii2-mpdf', ), ),
但是当保存文件为PDF还会提示错误,它还需要mpdf
去github下载,mpdf,放到vender下
在vendor\composer\autoload_namespaces.php添加
'mPDF' => array($vendorDir . '/mpdf'),
(1)在在配置文件main.php(使用的是yii advance)添加
'modules' => [ 'gridview'=>[ 'class' => '\kartik\grid\Module', 'downloadAction' => 'gridview/export/download', 'i18n'=>[ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@kvgrid/messages', 'forceTranslation' => true ] ] ],
(2)使用
Orders、Custom通过uid关联
在Orders中
public function getCustom() { return $this->hasOne(Custom::className(), ['id' => 'uid']); }
去写OrderSearch去继承Orders表
class OrderSearch extends Orders
{
public $tel,$trade_type,$pay_type;
public function rules()
{
return [
//省略
['tel','safe'] //添加后才能搜索
];
}
//省略
public function search($params)
{
$query = Orders::find();
$query->joinWith(['custom']);//添加
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([ //添加-----排序
'attributes' => [
'tel' => [
'asc' => [Custom::tableName().'.tel' => SORT_ASC],
'desc' => [Custom::tableName().'.tel' => SORT_DESC],
'label' => '手机号'
],'create_time'=>[],
]
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'uid' => $this->uid,'create_time' => $this->create_time,
]);
//省略
$query->andFilterWhere(['like', Custom::tableName().'.tel', $this->tel]) ;//搜索 return $dataProvider;
}
}
Controller代码
public function actionIndex()
{
$searchModel = newOrderSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]);
}
View中
<?php
use yii\helpers\Html;
use kartik\grid\GridView;?>
<div class="user-index">
<?= GridView::widget([
'panel'=>['type'=>'primary', 'heading'=>'表头'],
'dataProvider' => $dataProvider,
'filterModel'=>$searchModel,
'showPageSummary'=>true, //显示统计
'columns' => [
['class'=>'kartik\grid\SerialColumn'],
[
'attribute' => 'tel',
'value' => 'custom.tel',
'filter'=>Html::activeTextInput($searchModel, 'tel',['class'=>'form-control']),//添加搜索框
'label'=>'手机号',
'pageSummary'=>'总计',
'pageSummaryOptions'=>['class'=>'text-right text-warning'],
],
[
'attribute'=>'paid',
'format'=>['decimal', 2],
'pageSummary'=>true,
],
],
'tableOptions'=>['class' => 'table table-hover'],
'toolbar'=>[
'{toggleData}',
'{export}',
],
'exportConfig'=>[
GridView::HTML => [],
GridView::PDF => [],
GridView::CSV => [],
], ]); ?> </div>
(3)PDF下载乱码问题
修改vendor\kartik-v\yii2-grid\controllers下ExportController.php
protected function generatePDF($content, $filename, $config = []) { unset($config['contentBefore'], $config['contentAfter']); $config['filename'] = $filename; $config['mode']=Pdf::MODE_UTF8;//需要核心字库 $config['methods']['SetAuthor'] = ['Krajee Solutions']; $config['methods']['SetHeader']=['页眉']; $config['methods']['SetFooter']=['页脚']; $config['methods']['SetCreator'] = ['Krajee Yii2 Grid Export Extension']; $config['content'] = $content; $config['options']=['title' => '中文', 'autoLangToFont' => true, //这几个配置加上可以显示中文 'autoScriptToLang' => true, //这几个配置加上可以显示中文 'autoVietnamese' => true, //这几个配置加上可以显示中文 'autoArabic' => true, //这几个配置加上可以显示中文 ]; $pdf = new Pdf($config); echo $pdf->render(); }
核心字库在vendor\mpdf\ttfonts下
(3)CSV下载乱码问题
public function actionDownload()
{
$type = empty($_POST['export_filetype']) ? 'html' : $_POST['export_filetype'];
//省略
if($type=='csv'){
$content="\xEF\xBB\xBF".$content; //添加BOM头
}
//省略
return $content;
}