You are viewing revision #3 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
Bootstrap Extension Widgets ¶
In Yii 2.0, the yii widgets that use Twitter Bootstrap CSS & JS directly, are part of the yii2-bootstrap extension, starting with yii/bootstrap namespace. For customizing bootstrap extension specific widget assets, you could configure your own assets (CSS/JS) using the new Yii Asset Manager in your Yii Config file. Something like:
'components' => [
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapAsset' => [
'sourcePath' => 'your-path',
'css' => ['css/bootstrap.css', 'path/to/custom.css']
],
],
],
],
Customizing Other Widgets ¶
For other Yii widgets that are not part of an inbuilt Yii extension (like bootstrap or JUI), the widget itself does not have any CSS inbuilt. Rather the widget uses Bootstrap HTML markup as default which you can override.
An example of customizing the Yii GridView is mentioned here. You can easily override Bootstrap or other styling defaults by changing the GridView default options.
Option 1: Editing Object Config ¶
The simpler direct approach for you would be to override defaults for Yii's classes in your Yii index.php or in Yii config - something like:
\Yii::$objectConfig = [
'yii\grid\GridView' => [
'tableOptions' => [
'class' => 'table table-striped table-bordered',
],
],
'yii\widgets\LinkPager' => [
'options' => [
'class' => 'pagination',
],
],
];
Option 2: Extend Yii Class ¶
You can alternatively create custom classes extending Yii's inbuilt ones so you can use them across your app. For example:
namespace frontend\widgets;
class MyGridView extends \yii\grid\GridView
{
// change the following line
public $tableOptions = ['class' => 'table table-striped table-bordered'];
// override styling of your data columns
public $dataColumnClass = 'frontend\widgets\MyDataColumn';
// override styling of your pager
public $pager = [
'options' => ['class' => 'pagination'],
'firstPageCssClass' => 'first',
'lastPageCssClass' => 'last',
'prevPageCssClass' => 'prev',
'nextPageCssClass' => 'next',
'activePageCssClass' => 'active',
'disabledPageCssClass' => 'disabled'
];
// override styling of your sorter
public $sorter = ['options' => ['class' => 'sorter']];
// override other styles through these options
public $options = ['class' => 'grid-view'];
public $headerRowOptions = [];
public $footerRowOptions = [];
public $rowOptions = [];
}
Your extended DataColumn can look like:
namespace frontend\widgets;
class MyDataColumn extends \yii\grid\DataColumn
{
// customize by changing your styles
public $filterInputOptions = ['class' => 'form-control', 'id' => null];
// customize by changing your styles
public $sortLinkOptions = [];
}
There is no \Yii::$objectConfig anymore
use \Yii::$container->set() instead.
http://www.yiiframework.com/doc-2.0/yii-baseyii.html#$container-detail
Updated wiki for Yii object configuration
The wiki is updated for the change in which you can configure Yii objects globally.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.