有没有办法在yii2中动态更改kartik/select2上的maximumSelectionLength?
我正试图通过javascript更新select2字段(ListProduits)的maximumSelectionLength参数,该参数基于第二个select2(nom_id)的值,当它被更改时。 在加载表单之前,我首先将maxChildren值存储在js表中,以便在选择新的nom_id时可以在客户端访问它。 但下面的方法不起作用,因为它会弄乱ListProduits select2有没有办法在yii2中动态更改kartik/select2上的maximumSelectionLength?,yii2,jquery-select2,kartik-v,Yii2,Jquery Select2,Kartik V,我正试图通过javascript更新select2字段(ListProduits)的maximumSelectionLength参数,该参数基于第二个select2(nom_id)的值,当它被更改时。 在加载表单之前,我首先将maxChildren值存储在js表中,以便在选择新的nom_id时可以在客户端访问它。 但下面的方法不起作用,因为它会弄乱ListProduits select2 <script> var nomMaxChildren = <?= json_encode
<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,
]);?>
var nomMaxChildren=;
警报(nomMaxChildren[1]);
动态设置maximumSelectionLength
属性:
$('#demo-select').select2({ maximumSelectionLength: 1 });
谢谢你的回答,但我在这里就是这么做的:“select2:select”=>“function(){$('select[name=\“ListProduits[]\”])。select2({maximumSelectionLength:nomMaxChildren[\$(this).val()});}”,但这使得select2非常窄