Auto complete search textbox like google (multiple input & extra data)

I have posted another article like this. https://programmershandbook.wordpress.com/2015/11/16/auto-complete-search-textbox-like-google-in-a-simple-easy-way/.

But a different approach is used here. Previous article has single textbox with id selector and only the textbox text. But this time, we have multiple textboxes with class selectors and few extra data in the ajax request.

<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AutoComplete Box with jQuery</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
     
</head>
<body>
   <table data-employeeid="5">
     <tbody>
       <tr data-id="546">
         <td>
           <input type="text" class="suggestexam" />
          </td>
        </tr>
        <tr data-educationid="103">
         <td>
           <input type="text" class="suggestexam" />
          </td>
        </tr>
      </tbody>
    </table>
</body>
</html>


//Put this code in head tag (placing this at the bottom of body tag may not work). 
    $(function () {
        $('body').on('focus', ".suggestexam", function () {
            var extra = $(this).closest('tr').attr("data-id");
            $(this).autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/Employee/GetExamNameSuggestion",
                        data: JSON.stringify({ prefix: request.term, extradata: extra }),
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        //dataFilter: function (data) { return data; },
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                },
            });
        });
    });
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s