Browse Source

拉取合并master分支中的vendor/blobt文件夹

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
b7293f5ff7
  1. 124
      vendor/blobt/grid/GridView.php

124
vendor/blobt/grid/GridView.php

@ -43,7 +43,8 @@ use blobt\web\GridViewAsset;
* @email 380255922@qq.com * @email 380255922@qq.com
* @created Aug 13, 2019 * @created Aug 13, 2019
*/ */
class GridView extends BaseListView {
class GridView extends BaseListView
{
/** /**
* @var string 渲染列数据的类,默认是'yii\grid\DataColumn' * @var string 渲染列数据的类,默认是'yii\grid\DataColumn'
@ -221,7 +222,15 @@ class GridView extends BaseListView {
<div class="col-sm-3"> <div class="col-sm-3">
{batch} {batch}
<a href="create" class="btn btn-default"><i class="fa fa-plus"></i>添加</a> <a href="create" class="btn btn-default"><i class="fa fa-plus"></i>添加</a>
<button type="button" class="btn btn-default"><i class="fa fa-file-excel-o"></i>导出</button>
<!-- <a href="#" data-url='export' class="export btn btn-default"><i class="fa fa-file-excel-o"></i>导出</a>-->
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="fa fa-file-excel-o"></i>导出</button>
<ul class="dropdown-menu" role="menu">
<li> <a class="export-page" href="#" data-url="export">本页</a></li>
<li> <a class="export-all" href="#" data-url="export">全部</a></li>
</ul>
</div>
<!-- <button type="button" id="export" class="btn btn-default"><i class="fa fa-file-excel-o"></i>导出</button>-->
</div> </div>
<div class="col-sm-9"> <div class="col-sm-9">
{filter} {filter}
@ -262,7 +271,8 @@ HTML;
* 初始化必须的属性和每个列对象 * 初始化必须的属性和每个列对象
* @return * @return
*/ */
public function init() {
public function init()
{
parent::init(); parent::init();
if ($this->formatter === null) { if ($this->formatter === null) {
$this->formatter = Yii::$app->getFormatter(); $this->formatter = Yii::$app->getFormatter();
@ -276,21 +286,24 @@ HTML;
$this->initColumns(); $this->initColumns();
} }
public function run() {
public function run()
{
$view = $this->getView(); $view = $this->getView();
GridViewAsset::register($view); GridViewAsset::register($view);
$this->registerGridJs(); $this->registerGridJs();
$this->registerIcheckJs(); $this->registerIcheckJs();
$this->registerConfirmJs(); $this->registerConfirmJs();
$this->registerExportJs();
parent::run(); parent::run();
} }
/** /**
* 注册GridView Js * 注册GridView Js
*/ */
protected function registerGridJs() {
protected function registerGridJs()
{
$options = Json::htmlEncode(['filterUrl' => Url::to(Yii::$app->request->url), $options = Json::htmlEncode(['filterUrl' => Url::to(Yii::$app->request->url),
'filterSelector' => $this->filterSelector]);
'filterSelector' => $this->filterSelector]);
$id = $this->options['id']; $id = $this->options['id'];
$this->getView()->registerJs("jQuery('#$id').yiiGridView($options);"); $this->getView()->registerJs("jQuery('#$id').yiiGridView($options);");
} }
@ -298,7 +311,8 @@ HTML;
/** /**
* 注册icheck Js * 注册icheck Js
*/ */
protected function registerIcheckJs() {
protected function registerIcheckJs()
{
$js = <<<SCRIPT $js = <<<SCRIPT
$('.dataTable input[type="checkbox"]').iCheck({ $('.dataTable input[type="checkbox"]').iCheck({
checkboxClass: 'icheckbox_flat-blue', checkboxClass: 'icheckbox_flat-blue',
@ -324,7 +338,8 @@ SCRIPT;
/** /**
* 注册批量操作js * 注册批量操作js
*/ */
protected function registerBatchJs() {
protected function registerBatchJs()
{
$js = <<<SCRIPT $js = <<<SCRIPT
$("a.batch_item").click(function(){ $("a.batch_item").click(function(){
var url = $(this).data("url"); var url = $(this).data("url");
@ -356,7 +371,8 @@ SCRIPT;
$this->getView()->registerJs($js); $this->getView()->registerJs($js);
} }
protected function registerConfirmJs() {
protected function registerConfirmJs()
{
$js = <<<SCRIPT $js = <<<SCRIPT
$("a[alertify-confirm]").click(function(){ $("a[alertify-confirm]").click(function(){
var message = $(this).attr('alertify-confirm'); var message = $(this).attr('alertify-confirm');
@ -381,11 +397,35 @@ SCRIPT;
$this->getView()->registerJs($js); $this->getView()->registerJs($js);
} }
protected function registerExportJs()
{
$js = <<<SCRIPT
$("a.export-all").click(function(url){
var url = $(this).data("url");
if(!location.search){
window.location.replace(url+"?page-type=all");
}else{
window.location.replace(url+location.search+"&page-type=all");
}
});
$("a.export-page").click(function(url){
var url = $(this).data("url")+location.search;
if(!location.search){
window.location.replace(url+"?page-type=page");
}else{
window.location.replace(url+location.search+"&page-type=page");
}
});
SCRIPT;
$this->getView()->registerJs($js);
}
/** /**
* 渲染局部 * 渲染局部
* @return string|bool * @return string|bool
*/ */
public function renderSection($name) {
public function renderSection($name)
{
switch ($name) { switch ($name) {
case '{summary}': case '{summary}':
return $this->renderSummary(); return $this->renderSummary();
@ -408,7 +448,8 @@ SCRIPT;
* 渲染表格的html真实table * 渲染表格的html真实table
* @return string * @return string
*/ */
public function renderItems() {
public function renderItems()
{
$tableHeader = $this->showHeader ? $this->renderTableHeader() : false; $tableHeader = $this->showHeader ? $this->renderTableHeader() : false;
$tableBody = $this->renderTableBody(); $tableBody = $this->renderTableBody();
@ -424,7 +465,8 @@ SCRIPT;
* 初始化每列 * 初始化每列
* @throws InvalidConfigException * @throws InvalidConfigException
*/ */
protected function initColumns() {
protected function initColumns()
{
if (empty($this->columns)) { if (empty($this->columns)) {
throw new InvalidConfigException('The "columns" property must be set.'); throw new InvalidConfigException('The "columns" property must be set.');
} }
@ -434,9 +476,9 @@ SCRIPT;
$column = $this->createDataColumn($column); $column = $this->createDataColumn($column);
} else { } else {
$column = Yii::createObject(array_merge([ $column = Yii::createObject(array_merge([
'class' => $this->dataColumnClass ?: DataColumn::className(),
'grid' => $this,
], $column));
'class' => $this->dataColumnClass ?: DataColumn::className(),
'grid' => $this,
], $column));
} }
if (!$column->visible) { if (!$column->visible) {
unset($this->columns[$i]); unset($this->columns[$i]);
@ -451,7 +493,8 @@ SCRIPT;
* 渲染表头 * 渲染表头
* @return string * @return string
*/ */
public function renderTableHeader() {
public function renderTableHeader()
{
$cells = []; $cells = [];
foreach ($this->columns as $column) { foreach ($this->columns as $column) {
/* @var $column Column */ /* @var $column Column */
@ -466,7 +509,8 @@ SCRIPT;
* 渲染表格体 * 渲染表格体
* @return string * @return string
*/ */
public function renderTableBody() {
public function renderTableBody()
{
$models = $this->dataProvider->getModels(); $models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys(); $keys = $this->dataProvider->getKeys();
$rows = []; $rows = [];
@ -507,7 +551,8 @@ SCRIPT;
* @param int $index * @param int $index
* @return string * @return string
*/ */
public function renderTableRow($model, $key, $index) {
public function renderTableRow($model, $key, $index)
{
$cells = []; $cells = [];
foreach ($this->columns as $column) { foreach ($this->columns as $column) {
$cells[] = $column->renderDataCell($model, $key, $index); $cells[] = $column->renderDataCell($model, $key, $index);
@ -517,7 +562,7 @@ SCRIPT;
} else { } else {
$options = $this->rowOptions; $options = $this->rowOptions;
} }
$options['data-key'] = is_array($key) ? json_encode($key) : (string) $key;
$options['data-key'] = is_array($key) ? json_encode($key) : (string)$key;
//TODO 各行变色放到这里不合理 //TODO 各行变色放到这里不合理
if ($index % 2 == 0) { if ($index % 2 == 0) {
@ -539,7 +584,8 @@ SCRIPT;
* 渲染摘要显示 * 渲染摘要显示
* @return string * @return string
*/ */
public function renderSummary() {
public function renderSummary()
{
$count = $this->dataProvider->getCount(); $count = $this->dataProvider->getCount();
if ($count <= 0) { if ($count <= 0) {
return ''; return '';
@ -558,20 +604,21 @@ SCRIPT;
} }
return Yii::$app->getI18n()->format($this->summary, [ return Yii::$app->getI18n()->format($this->summary, [
'begin' => $begin,
'end' => $end,
'count' => $count,
'totalCount' => $totalCount,
'page' => $page,
'pageCount' => $pageCount,
'select' => $this->renderCountSelect()
], Yii::$app->language);
'begin' => $begin,
'end' => $end,
'count' => $count,
'totalCount' => $totalCount,
'page' => $page,
'pageCount' => $pageCount,
'select' => $this->renderCountSelect()
], Yii::$app->language);
} }
/** /**
* 渲染批量操作 * 渲染批量操作
*/ */
public function renderBatch() {
public function renderBatch()
{
if (empty($this->batch) && !is_array($this->batch)) { if (empty($this->batch) && !is_array($this->batch)) {
return ""; return "";
} }
@ -592,7 +639,8 @@ SCRIPT;
* 渲染表格的页数select * 渲染表格的页数select
* @return string * @return string
*/ */
protected function renderCountSelect() {
protected function renderCountSelect()
{
$items = [ $items = [
"20" => 20, "20" => 20,
"50" => 50, "50" => 50,
@ -614,7 +662,8 @@ SCRIPT;
* 渲染表格的筛选部分 * 渲染表格的筛选部分
* @return string * @return string
*/ */
protected function renderFilter() {
protected function renderFilter()
{
return $this->filter; return $this->filter;
} }
@ -624,17 +673,18 @@ SCRIPT;
* @return DataColumn 实例 * @return DataColumn 实例
* @throws InvalidConfigException * @throws InvalidConfigException
*/ */
protected function createDataColumn($text) {
protected function createDataColumn($text)
{
if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) { if (!preg_match('/^([^:]+)(:(\w*))?(:(.*))?$/', $text, $matches)) {
throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"'); throw new InvalidConfigException('The column must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
} }
return Yii::createObject([ return Yii::createObject([
'class' => $this->dataColumnClass ?: DataColumn::className(),
'grid' => $this,
'attribute' => $matches[1],
'format' => isset($matches[3]) ? $matches[3] : 'text',
'label' => isset($matches[5]) ? $matches[5] : null,
'class' => $this->dataColumnClass ?: DataColumn::className(),
'grid' => $this,
'attribute' => $matches[1],
'format' => isset($matches[3]) ? $matches[3] : 'text',
'label' => isset($matches[5]) ? $matches[5] : null,
]); ]);
} }

Loading…
Cancel
Save