如何在yii2中将两列连接成一个自动完成字段?

scyqe7ek  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(153)

我的视图中有一个自动完成字段,其代码如下

<?php
            $data = Members::find()
            ->select(['first_name as value', 'first_name as  label','id as id'])
            ->asArray()
            ->all();

            echo 'Name' .'<br>';
            echo AutoComplete::widget([
                 'name' => 'member_name',    
                 'clientOptions' => [
                    'source' => $data,
                    'minLength'=>'3', 
                    'autoFill'=>true,
                    'select' => new JsExpression("function( event, ui ) {
                    $('#receipt-member_id').val(ui.item.id);//#receipt-member_id is the id of hiddenInput.
                 }")],
                 ]);
            ?>

自动完成工作正常,它显示所有'成员'的名字。但可以有很多人有相似的名字,所以我想要的是连接的名字和姓氏。在正常的下拉菜单,它可以这样做

<?php
    $models1 = Members::find()->all();
    $data = array();
    foreach ($models1 as $model1)
        $data[$model1->id] = $model1->first_name . ' '. $model1->last_name;

    echo $form->field($model, 'member_id')->dropDownList(
                                $data,
                                ['prompt'=>'Select...']);
?>

如何使用自动完成小部件完成此操作?

vatpfxk5

vatpfxk51#

试试这条路

$data = Members::find()
        ->select(['concat(first_name,last_name) as value', 'first_name as  label','id as id'])
        ->asArray()
        ->all();
ruoxqz4g

ruoxqz4g2#

如果两个字段之间需要空格,请使用:

$data = Members::find()
    ->select(['concat(first_name, SPACE(1), last_name) as name', 'first_name as  label','id as id'])
    ->asArray()
    ->all();

SPACE()方法将雅阁您输入的特定数字为您生成空格:D

相关问题