Vishal Sharma
Vishal Sharma

Reputation: 99

typeahead ajax url dynamic

This is my code

$('#txt_street').typeahead({
  autoSelect:false,
  ajax: {
      url: "/index.php/MyCities/get_myCities/",
      timeout: 500,
      async : false,
      displayField: "city_name",
      valueField: 'city_id',
      triggerLength: 1,
      method: "get",
      loadingClass: "loading-circle",
      preDispatch: function (query) {
          return {
              search: query
          }

      },
       preProcess: function (data) {
        return data;
      }
  },
   onSelect: function (citySelect) {
   } //on select end
}); //city typeahead ends

I have two questions:

  1. How to pass more than one variable in GET? Here I can only pass "search" as variable. I need to pass another variable.
  2. My ajax url needs to be dynamic, not static which I have added here.

Upvotes: 0

Views: 1457

Answers (1)

P. Frank
P. Frank

Reputation: 5745

You can add another parameter in preDispatch and create your ajax URL before call typeahead()

For more get:

myUrl = "http://yoursite.com/" + yourdata;  //Here your custom url

$('#txt_street').typeahead({
  autoSelect:false,
  ajax: {
      url: myUrl ,  // Your custom url
      timeout: 500,
      async : false,
      displayField: "city_name",
      valueField: 'city_id',
      triggerLength: 1,
      method: "get",
      loadingClass: "loading-circle",
      preDispatch: function (query) {
          return {
              search: query,
              otherParam: 123 //Other parameter get you want
          }

      },
       preProcess: function (data) {
        return data;
      }
  },
   onSelect: function (citySelect) {
   } //on select end
}); //city typeahead ends

See more here : https://github.com/pwarelis/Ajax-Typeahead

Upvotes: 3

Related Questions