js控制台有错误吗?我认为这个问题似乎与 CheckboxMultiple 小部件的资产包有关。JQuery 资产缺少依赖属性。尝试在渲染小部件之前手动注册 jQuery。dataAttribute
在这个小部件的最新版本中,属性似乎也是未知的......这对我有用:
$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']),
],
]);
其中模板属性是模型中的关系,如下所示:
public function getTemplates()
{
return $this->hasMany(TemplateModel::className(), ['owner_id' => 'id']);
}
在 SiteController 动作模板中:
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;
}
在此示例中,我使用templates
带有列的表id
:name
和owner_id
。您应该将上述脚本修改为您的模型和属性名称。