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!阅读原文】。感谢您的支持!

您可能感兴趣的

Make a valid W3C URL and work in the Ajax query I have a generic function that returns URLs. (It's a plugin function that returns URLs to resources within a plugin). ...
Jquery+Ajax+Bootstrap Paginator实现分页的拼接... 效果图如下 图片.png jsp页面引入bootstrap样式,jquery和bootstrap-paginator.js jsp页面代码,页面两个...
Sending multiple mp3 to SOX How do I send multiple requests using the same client? I'm writing an android application that nee...
Create Inline CRUD Using jQuery and AJAX These are the four actions that make up the significant part of the actions of a PHP project. By the time developers ...
position fixed not working in IE9 contents loded i... This is working fine in FF,Opera, Chrome. Problem is in IE only. I have a div in which I am loading below contents A...