yii2 checkboxList custom class - yii2

Here is the sample code from Yii2 checkboxList, I want to add custom class for each Item in checkboxList but I don't know how and where can I add that!
Could you please help me please ..
$list = [0 => 'PHP', 1 => 'MySQL', 2 => 'Javascript'];
$list2 = [0,2];
echo Html::checkboxList('CuisineId',$list2,$list,array('class' => 'test' ));
Thanks in advance.

If you want to add the same class, you should use itemOptions :
echo Html::checkboxList('CuisineId', $list2, $list, ['itemOptions'=>['class' => 'test']]);
Or if you want a custom class for each item, you should use item callback :
echo Html::checkboxList('CuisineId', $list2, $list, ['item'=>function ($index, $label, $name, $checked, $value){
return Html::checkbox($name, $checked, [
'value' => $value,
'label' => $label,
'class' => 'any class',
]);
}]);
Read more : http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#checkboxList()-detail
EDIT : add example

Just in case you only need to change the label options:
<?= Html::checkboxList('CuisineId', $list, $items, [
'itemOptions' => [
'labelOptions' => [
'style' => 'font-weight: normal',
'class' => 'some-custom-class',
],
],
]) ?>
Note: Everything you put inside itemOptions will be passed to Html::checkbox() as its own options when creating each checkbox. It means you can pass class, style, label, labelOptions, etc.

Related

The Select/Unselect all are not working in Kartik select2 yii2

I have selecting, unselect and unselecting event. But when I click on select all or unselect all none of these events are called.
Every thing is working find except the select/Unslect all issue.Here is my code
echo $form->field($model, 'partnersIn')->widget(Select2::class, [
'name' => 'partnersIn[]',
'data' => Partner::PartnersData(),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => ['value' => $partners, 'placeholder' => Yii::t(
'app',
'Select the partners to show ad'
)],
'pluginOptions' => [
'tags' => true,
'allowClear' => true,
'multiple' => true,
],
])->label('Partners');
?>
Please run the composer update or composer install command.
Its work for me

How to allow DIV tags in Yii2 Editor

I installed a blog module that allows me to add blogs to my Yii2 app. Everything works well except the editor. I think Yii2 by default uses redactor.
The problem is that when I add code with using the code interface, all DIV tags are converted automatically to P tags.
I have checked through the redactor configuration but it does not look like there is a setting to adjust this:
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'uploadDir' => '#frontend/images/blog/upload',
'uploadUrl' => '/sites/eop/frontend/images/blog/upload',
'imageAllowExtensions' => ['jpg', 'png', 'gif', 'svg'],
],
Any idea on where else to look at?
You need to use the replaceDivs option, and set it to false under the client options. See the below code for an example
<?php echo \yii\redactor\widgets\Redactor::widget(
[
'model' => $model,
'attribute' => 'body',
'clientOptions' => [
'replaceDivs' => false
]
]
);
?>
if you are using an ActiveForm it should be like
<?php echo $form->field($model, 'body')->widget(
[
'clientOptions' => [
'replaceDivs' => false
]
]
);
?>

Yii2 kartik Form Builder + INPUT_WIDGET + NumberControl

I want to use widget NumberControl in kartik's Form Bulder. But when I try this code, i have an empty form and there no errors. Couse I can't understand where is my mistake.
Please tell me if someone use a NumberControl in kartik's Form Builder.
use kartik\builder\Form;
echo Form::widget([
'model' => $model,
'form' => $form,
'columns' => 6,
'columnSize' => 'md',
'attributes' => [
'oi_count' => [
'type' => Form::INPUT_WIDGET,
'widgetClass' => '\kartik\number\NumberControl',
],
]
]);
According to this:https://demosbs3.krajee.com/number by default the type is "hidden", you should set it to "text".

How to use Yii without jQuery?

Unfortunately, the company I work for already has a lot of client-side code written for Mootools, and they don't seem to like each other very much. :S
No matter what I do, I can't seem to stop jQuery getting included in every page. Any ideas?
By default Yii application templates make use of AppAsset
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
that depends on YiiAsset asset bundle, and Yii built-in widgets use jQuery heavily. Remove 'yii\web\YiiAsset' dependency if you don't plan to use Yii client-side features.
go to your AppAsset.php and remove yii\web\YiiAsset.
also, go to your main layout file, remove the default NavBar because it will call jquery.js to render
#remove the default main menu below
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']],
],
]);
NavBar::end();
This will remove jquery.js from Yii2. I would recommend you to switch from MooTools to Jquery if you can. It makes your life alot easier.
from docs you can disable the jQuery asset bundle by associating false to it :
return [
// ...
'components' => [
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => false,
],
],
],
];
But you need to consider that many jQuery built in widgets will stop working.
I don't know much about Mootools, but if it supports AJAX and you are building a complete fronted on top of it then you may also consider using Yii as a REST Service.

Listview widget yii2, how to change the index value

As you can use a index value inside the view when using a listview widget I wanted to change this index value as I use the same view on different places, so not every element that I use have the same index on the pages.
somepage.php
echo ListView::widget([
'summary' => false,
'viewParams' => [
'url' => '',
],
'index' => 'index + 4',//the idea
'options' => [
'class' => 'xxxx',
],
'itemOptions'=> [
'class' => 'xxxx',
],
'itemView' => '#frontend/views/product/_item_product_small',
'dataProvider' => $provider,
]);
frontend/views/product/_item_product_small.php
<div data-index="<?=$index?>">
// content
</div>
You need to create intermediate view for such cases:
somepage.php
echo ListView::widget([
// ...
'itemView' => '_view',
]);
_view.php
$this->render('#frontend/views/product/_item_product_small', ['index' => $index + 4]);