[英]How to get a user to hide and unhide a few dropdown lists in a form in yii2
I am trying to create a search form where I want to have a few of the dropdownlist fields in the form hidden. 我正在尝试创建一个搜索表单,该表单中我想隐藏一些下拉列表字段。 And if the user chooses to they can unhide it (like advanced search options).
而且,如果用户选择取消隐藏,则可以取消隐藏(例如高级搜索选项)。 Is there a way to do this in yii2?
yii2中有没有办法做到这一点? or do I have to use like javascript or jquery?
还是我必须使用像javascript或jquery?
Also is there a way to define the size of the dropdown list field. 还有一种方法可以定义下拉列表字段的大小。 I know if you use
我知道你用
textInput([style'=>'width:100px']);
that will word for a input text field but what about for a drop down field 这将表示输入文本字段,但对于下拉字段呢?
<?php
$form = ActiveForm::begin([
'id' =>'-search-form-inline',
'formConfig'=>['showlabels' =>false],
'type'=> ActiveForm::TYPE_INLINE
]);
?>
<div>
<?php
echo $form->field($model, 'spec')
->dropDownList(
$specialities,
['prompt'=>'Any Speciality']
)->label('Speciality');
?>
<?php
echo $form->field($model, 'zipcode')
->textInput(['maxlength'=>10,'style'=>'width:100px']);
?>
</div>
<?php
echo $form->field($model, 'proc')
->dropDownList(
$procedures,
['prompt'=>'Any Procedures / Tests']
)->label('Procedures/Tests');
?>
<?php echo $form->field($model, 'minc')
->dropDownList(
$mincash,
['prompt'=>'Any Min']
)->label('Minimum')
->Input(['maxlength'=>10,'style'=>'width:100px']);
?>
<?php echo $form->field($model, 'maxc')
->dropDownList(
$maxcash,
['prompt'=>'Any Max']
)->label('Maximum');
?>
This is a first example. 这是第一个例子。 Here we using container div which is hidden by default.
在这里,我们使用默认情况下隐藏的div容器。
<?php
$this->registerJs("
$('#extended-search-lnk').on('click', function(e){
e.preventDefault();
$('#extended-search').toggle();
})
");
?>
<a href="#" id="extended-search-lnk">Extended Search</a>
<div id="extended-search" style="display: none;">
<?php echo $form->field($model, 'minc')
->dropDownList(
$mincash,
['prompt'=>'Any Min']
)->label('Minimum')
->Input(['maxlength'=>10,'style'=>'width:100px']);
?>
</div>
And this is the second. 这是第二个。 Here we toggle visibility of specified dropdowns
在这里,我们切换指定下拉列表的可见性
<?php
$minc_attr_id = Html::getInputId($model, 'minc');
$this->registerJs("
$('#extended-search-lnk').on('click', function(e){
e.preventDefault();
$('#".$minc_attr_id."').toggle();
})
");
?>
<a href="#" id="extended-search-lnk">Extended Search</a>
<?php echo $form->field($model, 'minc')
->dropDownList(
$mincash,
['prompt'=>'Any Min']
)->label('Minimum')
->Input(['maxlength'=>10,'style'=>'width:100px']);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.