【问题标题】:Get ID but display Name获取 ID 但显示名称
【发布时间】:2014-05-19 18:47:27
【问题描述】:

我的表单中有一个自动完成字段,可让我选择联系人姓名并将其与列表关联。

然而,在编辑页面上,它将显示联系人 ID,而不是他的名字和姓氏。

我该怎么做?

这是ListingsController.php中的getEdit()方法

public function getEdit($id)
    {
    // get the nerd
    $listings = Listing::find($id);

        $listings->images = Image::where('listing_id', $id)->get(); 

        $ImagesFileName = ""; 

        $listings->imagesFiles = ""; 


        if ($listings->images) {

            foreach ($listings->images  as $CurImage) {

                $ImagesFileName[] = $CurImage['image_name']; 

            }

            $listings->imagesFiles = implode(",", $ImagesFileName) ;

        }


        $query = DB::table('contacts')->get();
        foreach ($query as $firstname)
        {
            $name[] = $firstname->firstname." ".$firstname->lastname;
        }
        $fullname = json_encode($name);

    // show the edit form and pass the nerd
    $this->layout->content = View::make('listings/edit')
        ->with('listings', $listings)
                ->with('getClientsByLetters', $fullname);
    }

这是ListingsController.php中的action_getClientsByLetters()

public function action_getClientsByLetters() {
        $term = Input::get('query'); 
        $data = array();
        $query = DB::query("
                SELECT * FROM contacts 
                WHERE MATCH (contact) 
                AGAINST('+".$term."*' IN BOOLEAN MODE)
                ");

        foreach ($query as $results => $contact) {
                $data[] = array(
                        'id' => $contact->id,
                        'value' => $contact->firstname
                );
        }
        return json_encode($data);
    }

这是views/listings/edit.blade.php中的字段

<p>{{ Form::text('contact_id', null, array('class'=>'form-control', 'placeholder'=>'Contact Name', 'id'=>'contact', 'onblur'=>'test()')) }}</p>

这是同一视图上的 javascript 代码

$(function() {
    var availableTags = <?php echo $getClientsByLetters; ?>;

    $( "#contact" ).autocomplete({
        source: availableTags,
        minLength: 2,
    });

    });

【问题讨论】:

    标签: php mysql laravel laravel-4 blade


    【解决方案1】:

    您的 action_getClientsByLetters 看起来很接近,但我认为它从未运行过。

    自动完成功能接受一个字符串数组或一个对象数组,其中每个对象同时具有 labelvalue,我认为这是您应该瞄准的目标。

    考虑到这一点,我将添加一个函数,其唯一职责是获取联系人并将它们放入自动完成所期望的所需形式,您可能会使用对象数组,因为它看起来像以后需要身份证。

    function getContacts()
    {
        $contacts = Contact::all();
        $autoComplete = array();
        foreach($contacts as $contact) {
            $autoComplete[]['label']  = $contact->first_name . ' ' . $contact->last_name;
            $autoComplete[]['value'] = $contact->id;
        }
        return json_encode($autoComplete);
    }
    

    然后你会调用视图...

    $this->layout->content = View::make('listings/edit')
        ->with('listings', $listings)
                ->with('getClientsByLetters', $this->getContacts());
    

    【讨论】:

      【解决方案2】:

      我认为这只是一个方法问题。通常我只是将这种字段设置为 SELECT 控件,使用 ID 作为关键字段,使用名称作为显示的选项值。在所有现代浏览器中,默认情况下,您会在 SELECT 控件上获得渐进式自动完成功能。然而,一个重要的问题是,您是否打算让用户能够向该字段添加任意新名称,或者是否打算只允许输入具有指定 ID 的现有名称。在后一种情况下,是的,我们可能需要弄清楚如何让您的自动完成文本字段起作用。

      【讨论】:

        猜你喜欢
        • 2020-04-24
        • 2016-06-19
        • 2023-04-01
        • 1970-01-01
        • 2021-04-06
        • 2020-03-02
        • 2017-02-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多