Concatenate several fields to be displayed in autocomplete using AJAX in cakephp

综合技术 2018-05-27 阅读原文

currently my autocomplete works in displaying the first name of the user, but I want to concatenate the first name, last name and etc. How do I achieve this in the following code?

    $(function() {
        $(".suggest").autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: 'Html->url(array('controller' => 'searches', 'action' => 'suggestUser')); ?>',
                    dataType: "json",
                    data: {
                        //request.term is the value of the current textbox.
                        term: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                            return {
                                label: item.User.firstName,
                                value: item.User.firstName

                            }
                        }));
                    }
                });
            },
            minLength : 1
        });
    });

In my controller, the following codes below is my logic to search for that field.

function suggestUser() {

        if (isset($_GET["term"])) {
            $term = $_GET["term"];

            $result = $this->User->find('all', array(
                'conditions' => array(
                    'User.firstName LIKE' => $term . '%'
                ),
                'fields' => array(
                    'firstName'
                )
                    ));
            if ($term) {
                $this->set('results', $result);
                $this->view = 'Json';
                $this->set('json', 'results');
            }
        }
    }
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!