yii2-如何从视图到控制器下拉所选值

时间:2018-04-10 05:05:18

标签: yii2 dropdown yii2-advanced-app

我正在研究yii2。在我看来,我有一个按钮,可以执行pdf文件的下载。此外,我有下拉菜单,我想将选定的下拉列表value从我的视图传递给控制器​​。我的index视图中的下拉列表是。

[
            'label' => 'Sub Division Name',
            'value' => function ($d) {
                if(is_object($d->subdiv))
                return $d->subdiv->name;
             return ' - ';
            },
            'filter' => Html::activeDropDownList($searchModel, 'sub_div', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),

        ],

生成的HTML

<select id="meterssearch-sub_div" class="form-control" name="MetersSearch[sub_div]">
<option value="">Sub-Div</option>
<option value="37111" selected="">37111 - HYD SADDAR</option>
<option value="37112">37112 - GARI KHATA</option>
<option value="37113">37113 - SARFARAZ COLONY</option>
<option value="37115">37115 - LIAQAT COLONY</option>
<option value="37141">37141 - TANDO ALLAH YAR-I</option>
<option value="37142">37142 - TANDO ALLAH YAR-II</option>
<option value="37143">37143 - TANDO JAM</option>
<option value="37144">37144 - CHAMBER</option>
<option value="37151">37151 - HALI ROAD</option>
<option value="37152">37152 - RIZVI HOSPITAL</option>
<option value="37153">37153 - ALLAMA IQBAL</option>
<option value="37154">37154 - SH:UMAID ALI KHAN</option>
<option value="37155">37155 - MIRAN M.SHAH</option>
<option value="37181">37181 - QASIMABAD</option>
<option value="37182">37182 - CITIZEN COLONY</option>
<option value="37183">37183 - HIRABAD</option>
<option value="37221">37221 - KOTRI</option>
<option value="37222">37222 - JAMSHORO</option>
<option value="37223">37223 - SHAHBAZ</option>
<option value="37225">37225 - NOORIABAD</option>
<option value="37226">37226 - SEHWAN SHARIF</option>
<option value="37241">37241 - PARETABAD</option>
<option value="37242">37242 - PHULELI</option>
<option value="37243">37243 - ILYASABAD</option>
<option value="37244">37244 - MEMON HOSPITAL</option>
<option value="37251">37251 - T.MUHAMMAD KHAN-I</option>
<option value="37252">37252 - T.MUHAMMAD KHAN-II</option>
<option value="37253">37253 - MATLI</option>
<option value="37254">37254 - BULRISHAH KARIM</option>
<option value="37271">37271 - THATTA</option>
<option value="37272">37272 - SUJAWAL</option>
<option value="37273">37273 - MAKLI</option>
<option value="37281">37281 - BADIN</option>
<option value="37282">37282 - TALHAR</option>
<option value="37283">37283 - GOLARCHI</option>
<option value="37311">37311 - NAWAB SHAH-I</option>
<option value="37312">37312 - NAWAB SHAH-II</option>
<option value="37313">37313 - SOCIETY</option>
<option value="37314">37314 - DAUR</option>
<option value="37315">37315 - SAKRAND</option>
<option value="37316">37316 - SAEEDABAD</option>
<option value="37318">37318 - QAZI AHMED</option> 
<option value="37319">37319 - DAULAT PUR</option>
<option value="37331">37331 - TANDO ADAM-I</option>
<option value="37332">37332 - TANDO ADAM-II</option>
<option value="37333">37333 - ODERO LAL</option>
<option value="37334">37334 - MATIARI</option>
<option value="37335">37335 - HALA</option>
<option value="37336">37336 - BHIT SHAH</option>
<option value="37341">37341 - SANGHAR</option>
<option value="37342">37342 - JHOLE</option>
<option value="37343">37343 - SHAHDAD PUR-I</option>
<option value="37344">37344 - SHAHDAD PUR-II</option>
<option value="37345">37345 - KHIPRO</option>
<option value="37346">37346 - SHAHPUR CHAKAR</option>
<option value="37347">37347 - SINDHRI</option>
<option value="37411">37411 - MIPURKHAS CITY</option>
<option value="37412">37412 - SATELLITE TOWN</option>
<option value="37413">37413 - HIRABAD MPK</option>
<option value="37414">37414 - MIRWAH</option>
<option value="37421">37421 - DIGRI</option>
<option value="37422">37422 - JHUDO</option>
<option value="37423">37423 - NAUKOT</option>
<option value="37424">37424 - MITHI</option>
<option value="37431">37431 - UMERKOT</option>
<option value="37432">37432 - PITHORO</option>
<option value="37433">37433 - SAMARO</option>
<option value="37434">37434 - KUNRI</option>
</select>

更新1

我的index观看代码是

两个按钮

<p>
    <?= Html::a('<i></i> Create Meters', ['create'], ['class' => 'btn btn-success']) ?>
    <a href="<?= URL::toRoute('meter/report') ?>" type="submit" class="btn btn-primary">Download PDF</a>// on this button click the actionReport() is called

</p>

GridView的

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'description',
        'meter_msn',
        //'meter_type',
        [
            'label' => 'Meter Type',
            'value' => function ($d) {
                if(is_object($d))
                    return $d->meter_type;
                return ' - ';
            },
            'filter' => Html::activeDropDownList($searchModel, 'meter_type', \app\models\Meters::getMeterTypeValues(), ['prompt' => "Meter Type", 'class' => 'form-control']),

        ],
        //'sub_div',
        [
            'label' => 'Sub Division Name',
            'value' => function ($d) {
                if(is_object($d->subdiv))
                return $d->subdiv->name;
             return ' - ';
            },
            'filter' => Html::activeDropDownList($searchModel, 'sub_div', \common\models\SurveyHescoSubdivision::toArrayList(), ['prompt' => "Sub-Div", 'class' => 'form-control']),

        ],


        //'meter_status',
        [
            'label' => 'Meter Status',
            'value' => function ($d) {
                    return $d->meter_status;
            },
            'filter' => Html::activeDropDownList($searchModel, 'meter_status', \app\models\Meters::getMeterStatus(), ['prompt' => "Meter Status", 'class' => 'form-control']),

        ],
        //'status',
        [
            'label'=>'Status',
            'value'=>function($a){
                    return $a->getStatusvalue();
            }
        ],
        [
            'label' => 'Issued User',
            'value' => function ($d) {
                if(is_object($d->user))
                    //return $d->user->name;
                    return $d->issued_user == '' ? '' : $d->user->username;
                return ' - ';
               // return $d->user->name;

            },
            'filter' => Html::activeDropDownList($searchModel, 'issued_user', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),

        ],
        [
            'label' => 'Created By',
            'value' => function ($data) {
                if (is_object($data))
                    return $data->created->name;
                return ' - ';
            },

        ],
        'created_at:date',
        'historic',
        'comments',
        ['class' => 'yii\grid\ActionColumn'],

    ],
]); ?>

点击按钮,会调用一个动作控制器,在其中,我正在尝试访问选择value的{​​{1}}。

sub_div

但它给了我空视图,没有显示任何值。

我该如何访问它?任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

您需要在报表控制器中使用过滤器值。为此,您需要使用查询字符串传递值。

您可以在视图索引页面中获取过滤器值,如下所示。

$sub_div = isset($_REQUEST['MetersSearch']['sub_div'])?$_REQUEST['MetersSearch']['sub_div']:'';

现在可以将此值传递给控制器​​。

<a href="<?= URL::toRoute('meter/report').'?sub_div='.$sub_div ?>" type="submit" class="btn btn-primary">Download PDF</a>

报告控制器中,您将获得 sub_div

function actionReport()
{
   $sub_div = $_REQUEST['sub_div'];
   print_r($sub_div);
   die();
}