Yii2活动表单下拉列表有两个字段

时间:2019-12-08 13:14:36

标签: yii2

我想在Yii2中创建一个活动表单下拉列表:

<?= $form->field($model, 'country_id')->dropDownList(
        ArrayHelper::map(Country::find()->where(['flag'=>1])->asArray()->all(), 'id', 'country'),['prompt'=>' Select Country']
    )->label(false)?>

上面的示例仅显示国家名称。我想要括号内的国家名称。我该怎么办?

2 个答案:

答案 0 :(得分:3)

ArrayHelper::map()接受回调,因此您可以使用闭包为map生成值:

    import { AngularFireAuthGuard, redirectUnauthorizedTo, redirectLoggedInTo } from '@angular/fire/auth-guard';

    const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['login']);
    const redirectLoggedInToHome = () => redirectLoggedInTo(['welcome']);

    const routes: Routes = [
      { path: 'login', loadChildren: './login/login.module#LoginPageModule', canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectLoggedInToHome } },
      { path: 'welcome', loadChildren: './welcome/welcome.module#WelcomePageModule', canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectUnauthorizedToLogin } }
];

答案 1 :(得分:0)

您可以添加一个select方法来返回所需的concat字符串作为名称

  <?= $form->field($model, 'country_id')->dropDownList(
        ArrayHelper::map(Country::find()
        ->select(['id', 'concat(country, "(", id, ")" AS country ')])
        ->where(['flag'=>1])->asArray()->all(), 'id', 'country'),['prompt'=>' Select Country']
    )->label(false)?>