yii2 - 有没有办法在 yii2 中动态更改 kartik/select2 上的 maximumSelectionLength?
问题描述
我试图通过javascript更新select2字段(listeProduits)的maximumSelectionLength参数,基于第二个select2(nom_id)更改时的值。在加载表单之前,我首先将 maxChildren 值存储在 js 表中,以便在选择新的 nom_id 时可以在客户端访问它。下面的方法不起作用,因为它弄乱了 listeProduits select2。
<script>
var nomMaxChildren = <?= json_encode(ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'max_children')); ?>;
alert(nomMaxChildren[1]);
</script>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'nom_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'nom'),
'options' => ['placeholder' => Yii::t('app','Select a name...')],
'pluginEvents' => [
"select2:select" => "function() {
$('select[name =\"listeProduits[]\"]').select2({
maximumSelectionLength: nomMaxChildren[\$(this).val()]
});
}",
]
]);?>
<label class="control-label"><?= Yii::t('app', 'Produits')?></label>';
<?= Select2::widget([
'attribute' => 'listeProduits',
'name' => 'listeProduits',
'data' => ArrayHelper::map(Produit::find()->asArray()->where(['statut_id' => 2])->andWhere(['boite_id' => null])->orWhere(['boite_id' => $model->id])->all(), 'id', 'numero_de_serie'),
'value' => (is_null($model->id)) ? null : ArrayHelper::map(Produit::find()->asArray()->where(['boite_id' => $model->id])->all(), 'id', 'id'),
'options' => ['placeholder' => Yii::t('app','Select products')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'maximumSelectionLength' => (isset($model->nom)) ? $model->nom->max_children : null,
],
'showToggleAll' => false,
]);?>
此外,如果您对如何验证 maximumSelectionLength 在客户端是可以的这一事实有任何想法,那就太好了:D
解决方案
动态设置maximumSelectionLength
属性:
$('#demo-select').select2({ maximumSelectionLength: 1 });
推荐阅读
- python - shutil copyfile 在我的目标文件夹中提供 Errno 2
- gremlin - 在 gremlin_python 中更新属性值而不添加附加值
- jquery - 平滑滚动到顶部到 jquery 中的无效文本字段
- c# - C# - 添加两个带有随机数的整数变量
- php - 使用 Api 平台,自动将用户分配给 Object (OneToMany)
- css - Rails:试图让 Bootstrap 的偏移类工作
- caching - Redis 写入速度较慢
- javascript - 遇到烦人的问题:momentjs 使用 mat-datepicker 针对不同时区请一天假?
- maven - 我可以通过设置 RelPath 来拥有一个 Maven 父项目吗
- c - 如何识别堆栈溢出的错误点是在 MikroC 代码中强制设备重置错误