Buy now!

NinjaSuggest

Sample 02. Using ajax source with custom format text

HTML

                        

JS

                        $("#sampleSuggest").ninjaSuggest({
                            url: '/NinjaSuggest/Search',
                            key: 'id',
                            text: function (obj) { return obj.id + ' - ' + obj.name; },
                            minChars: 1,
                            maxChars: 5,
                            limitTo: 10,
                            onSelect: function (obj) {
                                alert("Name selected: " + obj.name);
                            }
                        });

                        //-- REQUEST --
                        //-------------
                        //-- Server method needs to accept two parameters (string key, string serach)
                        //-- key: used to find a especific record and setting a value to the suggest. Example: $("#sampleSuggest").val(15);
                        //-- search: used to find any match based on users input


                        //-- RESPONSE --
                        //--------------
                        //-- As response, ninjaSuggest expects a json array of objects:
                        //-- [{"id":3,"name":"Marry","email":"marry@test.com"},{"id":9,"name":"Martha","email":"martha@test.com"},{"id":21,"name":"Emma","email":"emma@test.com"}]