繁体   English   中英

在我的Yii2下拉列表中,我需要将“ id”列作为选项的值,并将name列作为用户在select选项中看到的内容

[英]In my Yii2 dropdown list I need my 'id' column to be the value of my options and the name column to be what the user sees in the select option

在我的Yii2 dorpdown列表中,我需要将'id'列作为我的选项的值,并将name列作为用户在select下拉列表中看到的内容

我的代码输出以下内容:

<select id="gatewayproviders-id" class="form-control" name="GatewayProviders[id]">
<optgroup label="0">
<option value="id">Authorize.net</option>
</optgroup>
<optgroup label="1">
<option value="id">NMI</option>
</optgroup>
</select>

但是我希望它输出以下内容:

<select id="gatewayproviders-id" class="form-control" name="GatewayProviders[id]">
<option value="1">Authorize.net</option>
<option value="2">NMI</option>
</select>

我的yii2代码生成了以下代码:

<?php
    $gatewayTypes = \app\models\GatewayProviders::find()->select('gateway_provider')->orderBy('gateway_provider')->asArray()->all();
    $gatewayProviders = new \app\models\GatewayProviders();
    ?>
    <?= $form->field($gatewayProviders, 'id')->dropDownList(\app\models\GatewayProviders::find()->select(['id' => 'gateway_provider'])->orderBy('id')->asArray()->all())?>

任何帮助,将不胜感激!

您应该使用ArrayHelper::map()创建类型映射(名称由ID索引):

<?php
$gatewayProviders = new \app\models\GatewayProviders();
$gatewayTypes = \app\models\GatewayProviders::find()
    ->select(['id', 'gateway_provider'])
    ->orderBy('gateway_provider')
    ->asArray()
    ->all();
$types = \yii\helpers\ArrayHelper::map($gatewayTypes, 'id', 'gateway_provider');
?>
<?= $form->field($gatewayProviders, 'id')->dropDownList($types)?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM