0

Below is the jsp page tag for input text element:

<input name="searchTextSpan" id="searchTextSpan" type="text"/>

below is the ajax call that loads on dcument.ready:

AUI().use("liferay-portlet-url", function(A) {
    var resourceURL = Liferay.PortletURL.createResourceURL();
    resourceURL.setPortletId("app_war_portlet");
    resourceURL.setResourceId(resourceId);
    require(["dojo/request", "dijit/registry", "dojo/on", "dojo/domReady!"], function(request){
          request.post(resourceURL.toString(), {
              query: ajaxData,
              handleAs: "json"
          }).then(function(data){
              if(resourceId == 'inputTextClick'){
                  AUI().use("liferay-portlet-url", function(A) {
                        var resourceURL =   Liferay.PortletURL.createResourceURL();
                            resourceURL.setPortletId("app_war_portlet");
                        if(data.cachetmpArr!=null && data.cachetmpArr.length>0){
                            var cacheList = JSON.stringify(data.cachetmpArr);
                            cacheList = cacheList.replace(/"/g, "'");
                            console.log('cacheList12 '+cacheList);//['106182233','206182233','306182233'];
                                     $('#searchTextSpan').autocomplete({
                                                width: 300,
                                                max: 10,
                                                delay: 100,
                                                minLength: 1,
                                                autoFocus: true,
                                                cacheLength: 1,
                                                scroll: true,
                                                highlight: false,
                                                source:cacheList,
                                             }).focus(function(){
                                             $(this).autocomplete("search", "");
                                             });
                        }
                  });

              }

          });
    })
})

the source attribute is not accepting the cacheList it throws 404 url error. can you please suggest

Sangeetha cg
  • 81
  • 5
  • 14
  • I don't think you are even getting the JSON. So you may need to debug why the call to server fails. – Sandeep Nayak May 16 '16 at 04:28
  • i do get the json object value console.log('cacheList12 '+cacheList);//['106182233','206182233','306182233']; this is what i get. in case i give the value like cacheList=['106182233','206182233','306182233'], it works! but passing the objec directly it doesnt pick up – Sangeetha cg May 16 '16 at 04:30
  • okay. Then you need to parse your JSON before you pass it to autocomplete. `JSON.parse(cacheList)`. Remember, JSON is a `string`, whereas autocomplete needs an `array` – Sandeep Nayak May 16 '16 at 04:31

1 Answers1

0

You need to parse JSON before you pass it to autocomplete.

Like this:

$('#searchTextSpan').autocomplete({                                               


 width: 300,
    max: 10,
    delay: 100,
    minLength: 1,
    autoFocus: true,
    cacheLength: 1,
    scroll: true,
    highlight: false,
    source:JSON.parse(cacheList), // parse JSON response
    }).focus(function(){
    $(this).autocomplete("search", "");
});
Sandeep Nayak
  • 4,649
  • 1
  • 22
  • 33
  • i dont get any error , but there is no dropdown visible for my previous searched records :( also if i log this console.log('vals '+JSON.parse(cacheList)); it give me blank.... – Sangeetha cg May 16 '16 at 04:41
  • Also, any reason you are using the `focus` method? can you try removing it? – Sandeep Nayak May 16 '16 at 04:50
  • yes i tried removing focus method as well, still there is no drop down visible for the previous searched values.. – Sangeetha cg May 16 '16 at 04:55
  • What do you mean by previous searched values? – Sandeep Nayak May 16 '16 at 04:57
  • console.log('cacheList12 '+cacheList);//['106182233','206182233','306182233']; this will have the values that has been searched, next time i type in my input text box like say 10.... i should get in the drop-down as 106182233 that will be there in my previous searched cachelist that is parsed to source. – Sangeetha cg May 16 '16 at 05:00