autocomplete Knockout.js using ajax

288 views
Skip to first unread message

avi....@gmail.com

unread,
Mar 26, 2015, 4:47:25 AM3/26/15
to knock...@googlegroups.com
Hi All

I havroblem with knockout autocomplete..its not working can anybady help me in solving the issue please. Using ajax call my URL.Action is nt getting fired

Avinash

Gunnar Liljas

unread,
Mar 26, 2015, 5:08:26 AM3/26/15
to knock...@googlegroups.com
You have to show code to get help.

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Avinash Balasani

unread,
Mar 26, 2015, 5:21:16 AM3/26/15
to knock...@googlegroups.com
 ko.bindingHandlers.autoComplete = {
                init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
                    var postUrl = allBindingsAccessor().source; // url to post to is read here
                    var selectedObservableArrayInViewModel = valueAccessor();

                    $(element).autocomplete({
                        minLength: 0,
                        autoFocus: true,
                        source: function (request, response) {
                            $.ajax({
                                url: postUrl,
                                data: { term: request.term },
                                dataType: "json",
                                type: "POST",
                                success: function (data) {
                                    response(data);
                                }
                            });
                        },
                        select: function (event, ui) {
                            var selectedItem = ui.item;

                            if (!_.any(selectedObservableArrayInViewModel(), function (item) { return item.id == selectedItem.id; })) { //ensure items with the same id cannot be added twice.
                                selectedObservableArrayInViewModel.push(selectedItem);
                            }
                        }
                    });
                }
            }; 




<input type="text" data-bind="value:CPT,autoComplete:myObservableArray, source : '@Url.Action("ListofPids", "CoderHome")'"  />






 [HttpPost]
        public ActionResult ListofPids(string term) 
        {

            var pids = dbContext.PatientDemographics.Where(x=>x.FirstName.ToLower().Contains(term.ToLower())).Select(x => x.FirstName).ToList();

            return Content(JsonConvert.SerializeObject(pids, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }));
        }
Reply all
Reply to author
Forward
0 new messages