简体   繁体   English

使用SODA数据源预先引导

[英]Bootstrap typeahead with SODA data source

I'm attempting to use the bootstrap typahead, with a SODA endpoint for a datasource. 我正在尝试将引导式冒号与SODA端点一起用于数据源。 The SODA endpoint returns a JSON array, and can use simple query strings to query it. SODA端点返回一个JSON数组,并且可以使用简单的查询字符串对其进行查询。

Example of an endpoint: https://soda.demo.socrata.com/resource/earthquakes.json?region=Washington Taken from: http://dev.socrata.com/consumers/getting-started/ 端点示例: https://soda.demo.socrata.com/resource/earthquakes.json?region=Washington : //soda.demo.socrata.com/resource/earthquakes.json https://soda.demo.socrata.com/resource/earthquakes.json?region=Washington region https://soda.demo.socrata.com/resource/earthquakes.json?region=Washington Washington来自: http : //dev.socrata.com/consumers/getting-started/

In this case, Washington is what the user might type in the input. 在这种情况下, Washington可能是用户可能输入的内容。

Example of JSON returned using Washington as an example: [ { "region" : "Washington", "source" : "uw", "location" : { "needs_recoding" : false, "longitude" : "-120.0137", "latitude" : "47.3452" }, "magnitude" : "3.3", "number_of_stations" : "38", "datetime" : "2012-09-13T17:33:45", "earthquake_id" : "60451342", "depth" : "12.70", "version" : "1" } , { "region" : "Washington", "source" : "uw", "location" : { "needs_recoding" : false, "longitude" : "-122.4432", "latitude" : "46.5543" }, "magnitude" : "1.1", "number_of_stations" : "31", "datetime" : "2012-09-13T11:52:57", "earthquake_id" : "60451197", "depth" : "16.60", "version" : "2" } ] Washington为例返回的JSON示例: [ { "region" : "Washington", "source" : "uw", "location" : { "needs_recoding" : false, "longitude" : "-120.0137", "latitude" : "47.3452" }, "magnitude" : "3.3", "number_of_stations" : "38", "datetime" : "2012-09-13T17:33:45", "earthquake_id" : "60451342", "depth" : "12.70", "version" : "1" } , { "region" : "Washington", "source" : "uw", "location" : { "needs_recoding" : false, "longitude" : "-122.4432", "latitude" : "46.5543" }, "magnitude" : "1.1", "number_of_stations" : "31", "datetime" : "2012-09-13T11:52:57", "earthquake_id" : "60451197", "depth" : "16.60", "version" : "2" } ]

Sorry if formatting of the JSON is weird. 抱歉,如果JSON格式很奇怪。

So far, I have been unable to get the typeahead to work, nor could find sufficient documentation on how to retrieve such data. 到目前为止,我无法提前进行输入,也找不到足够的文档来检索此类数据。

if you want to call the url when user choose from the options, you can use updater 如果要在用户从选项中进行选择时调用url,则可以使用updater

$("#sourceInput").typeahead({
   source:function(query,process){
      var url = 'https://soda.demo.socrata.com/resource/earthquakes.json?region=' + query;
      $.getJSON(url,function(resp){
        process(resp)
      });
   },
   updater: function(item){
      var url = 'https://soda.demo.socrata.com/resource/earthquakes.json?region=' + item;
      $.getJSON(url,function(resp){
        //do something with json response
      });
      //return the item for the input
      return item;
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM