<?= $form->field($model, 'template_id')->widget(CheckboxMultiple::className(), [
'dataAttribute' => 'template_id',
scriptOptions' => [450=>'abc',452=>'xyz'],
'placeholder' => Yii::t('app', 'Select ...'),
]) ?>
I try to implement this => https://github.com/diiimonn/yii2-widget-checkbox-multiple
but i am not getting the result. When I have done code I am getting blank page.
What is the mistake here ?
Are there any errors in js console? I think that problem seems to be with assets bundle of CheckboxMultiple widgets. There are missing depends property for JQuery Assets. Try to register jQuery manually before render widget. dataAttribute
property also seems to be unknown in latest version of this widget... This works for me:
$this->registerAssetBundle(yii\web\JqueryAsset::className());
echo $form->field($model, 'templates')->widget(CheckboxMultiple::className(), [
'attributeLabel' => 'templates',
'placeholder' => Yii::t('app', 'Select ...'),
'ajax' => [
'url' => Url::toRoute(['/site/templates']),
],
]);
Where templates attribute is relation in model, like this:
public function getTemplates()
{
return $this->hasMany(TemplateModel::className(), ['owner_id' => 'id']);
}
and in SiteController action templates:
public function actionTemplates()
{
Yii::$app->response->format = 'json';
$json = new \stdClass();
$query = new Query();
$query->select([
'id' => 'id',
'text' => 'name'
]);
$query->from(TemplateModel::tableName());
if ($search = Yii::$app->request->post('search', '')) {
$query->where(['like', 'name', $search]);
}
$query->orderBy([
'name' => SORT_ASC
]);
if ($itemsId = Yii::$app->request->post('itemsId', [])) {
$query->andWhere(['not in', 'id', $itemsId]);
}
$query->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$json->results = array_values($data);
return $json;
}
In this example I use table templates
with columns: id
, name
and owner_id
.
You should modify above script to yours names of models and attributes.