如何从一条记录在 Yii2 中进行下拉 [英] How to make dropdown in Yii2 from one record

查看:24
本文介绍了如何从一条记录在 Yii2 中进行下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像这样从一行表中的两列在 Yii2 中进行下拉:

Is it possible to make dropdown in Yii2 from two column in one row table like this:

我想要这样的 dropdown 列表:

I want dropdown list like this:

推荐答案

您尚未提供用于下拉列表的模型的任何详细信息,因此我将使用 Phone 模型和字段为idphonearea_code,仅供理解.

You haven't provided any details of the model you are using for the dropdown so I will demonstrate the example with Phone model and the fields are id, phone, area_code, just for the sake of understanding.

这是phone表的数据:

+----+-------------+-----------+
| id | phone       | area_code |
+----+-------------+-----------+
|  1 | 03214442021 | 021       |
|  2 | 03214452520 | 051       |
+----+-------------+-----------+

现在,如果您从数据库中选择一行并尝试从 下拉 内的单行中列出所有 columns,您可以使用 array_filter 将所有 attributes 过滤到一个数组中,然后将该数组分配给 drop-down 选项.

Now, if you are selecting a single row from the database and trying to list all the columns from the single row inside the drop-down you can use the array_filter to filter out all the attributes into an array and then assign that array to the drop-down options.

虽然您可以使用相同的方法并在 foreach 内使用 array_filter 如果您要从表中选择所有行,但区别在于放置的布局下来.

Although you can use the same approach and use array_filter inside the foreach if you are selecting all the rows from the table, the difference would be the layout of the drop down.

选择单行时,您将获得如下下拉列表:

When selecting a single row you will get the drop-down as follows:

如果您从表中选择所有行,下拉列表将如下所示:

And if you are selecting all the rows from the table the dropdown will look like below:

在您的控制器中,您可以执行以下操作:

In your controller, you can do the following:

public function actionPhone($id){
    $model = Phone::findOne($id);
    $rows = array_filter($model->attributes);
    return $this->render('drop-down', ['rows' => $rows]);
}

对于多行

public function actionPhone(){
    $phone = Phone::find()->all();
    foreach ($phone as $model) {
        $rows[] = array_filter($model->attributes);
    }
    return $this->render('drop-down', ['rows' => $rows]);
}

对于上述两种情况,视图部分保持不变,在您创建下拉列表的视图内,将行分配给选项.

The view part remains the same for both of the cases above, inside your view where you are creating the drop-down assign the rows to the options.

您可以使用 yii\helpers\HtmlActiveForm 构建下拉菜单.

You can build the dropdown using yii\helpers\Html or ActiveForm.

<?= yii\helpers\Html::dropDownList('my-options',null,$rows) ?>

注意:下拉选项textcolumn的值,下拉选项value列的值名称.表示下拉列表中的第一个选项 ['id'=>1] id 将是值, 1 将是文本显示选项.

Note: The dropdown option text is the value of the column and the dropdown option value is the column name. Means the first option in drop-down ['id'=>1] the id will be the value and 1 will be the text displayed for the option.

如果您需要同时使用选项和文本的值,您可能需要使用 \yii\helpers\ArrayHelper::map() 在这里你可以将闭包传递给 $from$to 表示匿名函数,这样您就可以将列值设为 keyvalue ,如下所示:

If you need to use the value for both the option and text you might need to use \yii\helpers\ArrayHelper::map() where you can pass closure to the $from and $to means an anonymous function that way you can have the column value as key and value both like below:

Array(
    [1]=>1,
    [03214442021]=>03214442021 ,
    [021]=>021
)

因此将您的操作更改为以下内容:

So change your action to the following:

public function actionPhone($id){
        $model = Phone::findOne($id);
        $rows=\yii\helpers\ArrayHelper::map(array_filter($model->attributes),function($item){
            return $item;
        },function($item){
            return $item;
        });
        return $this->render('drop-down', ['rows' => $rows]);
}

这篇关于如何从一条记录在 Yii2 中进行下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆