kartik-v / yii2-icons

Set of icon frameworks for easy use in Yii Framework 2.0
http://demos.krajee.com/icons
Other
71 stars 29 forks source link

BaseHtml encoding Problem when trying to set pagination icons as default #5

Closed Bhoft closed 9 years ago

Bhoft commented 9 years ago

Hi I wanted to set the default icons for all paginations for gridviews. Therefore i added this to my bootstrap.php

Yii::$container->set('yii\grid\GridView', [
    'tableOptions' => [
        'class' => 'table table-hover table-striped table-bordered',
    ],
    'pager' => [
        'firstPageLabel'=> kartik\icons\Icon::show('fast-backward', ['class' => ''], kartik\icons\Icon::BSG),
        'lastPageLabel'=> kartik\icons\Icon::show('fast-forward', ['class' => ''], kartik\icons\Icon::BSG),
        'prevPageLabel'=> kartik\icons\Icon::show('chevron-left', ['class' => ''], kartik\icons\Icon::BSG),
        'nextPageLabel'=> kartik\icons\Icon::show('chevron-right', ['class' => ''], kartik\icons\Icon::BSG),

        // 'prevPageLabel'=>'<',
        // 'nextPageLabel'=>'>',
        // 'firstPageLabel'=>'«',
        // 'lastPageLabel'=>'»',

        // 'firstPageLabel'=>'first',
        // 'lastPageLabel'=>'last',
    ],
]);

But then i get Notices which aren't from your code but from the yii src.

Notice: Trying to get property of non-object in D:\wamp\www\yii2\yii_apps\epr\vendor\yiisoft\yii2\helpers\BaseHtml.php on line 96

Because the Html::tag uses encoding and tries to get the Yii::$app->charset which isn't set at this position. I don't know if there is a better position instead of the bootstrap.php

Problem if I add the code in the layout.php its maybe not used when i have a different view file for ajax requests. And in yii1.x i could set global settings for classes with the widgetFactory.

do you have a suggestion?

regards Horizons

kartik-v commented 9 years ago

Yes you may want to check if there is a better way with the yii devs.

One alternative/workaround is to use a global helper function:

use yii\helpers\ArrayHelper;
class KvGlobal {
    public static function getGridOptions($options = []) {
        // you can set the default options in Yii::$app->params as well for easier config
        $defaultOptions = [
            'tableOptions' => [
                'class' => 'table table-hover table-striped table-bordered',
            ],
            'pager' => [
                'firstPageLabel'=> kartik\icons\Icon::show('fast-backward', ['class' => ''], kartik\icons\Icon::BSG),
                'lastPageLabel'=> kartik\icons\Icon::show('fast-forward', ['class' => ''], kartik\icons\Icon::BSG),
                'prevPageLabel'=> kartik\icons\Icon::show('chevron-left', ['class' => ''], kartik\icons\Icon::BSG),
                'nextPageLabel'=> kartik\icons\Icon::show('chevron-right', ['class' => ''], kartik\icons\Icon::BSG),
            ],
        ];
        return ArrayHelper::merge($defaultOptions, $options);
    }
}

Then use the above in your grid widgets at runtime:

use kartik\grid\GridView;
$options = KvGlobal::getGridOptions([
    'dataProvider' => $dataProvider,
    'columns' => $columns
]);

echo GridView::widget($options);