dongrenzheng1619 2011-05-01 02:25
浏览 44
已采纳

数据库支持的输入字段的代理过滤器值

Background

For the country field, the system detects and defaults to Canada. For the city field, the user types a Canadian city name, populated Facebook-style.

Both the country list and city list are populated using AJAX and JSON, sourced from a remote database using a proxy (queried via PHP).

The country is defaulted to the user's country of origin using MaxMind's GeoIP PHP API.

When the user selects a different country, its two-letter ISO code must be passed as a parameter to the city input so that the query that drives it can search for cities in the selected country.

For example, a user whose IP address is in Canada can choose the United States. When this happens:

  1. the city field will be erased;
  2. the city field gains focus; and then
  3. only U.S. cities are displayed (as the user types).

Problem

The following PHP snippet proxies retrieval of the city list to a remote server:

echo file_get_contents( 'http://server/city.dhtml?q=' . 
  urlencode( $_GET['q'] ) );

The jQuery tokenInput function does not seem to provide a mechanism to pass additional content. The relevant jQuery snippets follow.

Acquire Countries

The following code gets a country code and a list of countries. It sets the user's country as the default country.

  // Where in the world?
  $.getJSON( 'geoip.dhtml', function( data ) {
    country = data[0].id;
  });

  // Select from countries that have significant amounts of data.
  $.getJSON( 'country.dhtml', function( data ) {
    var h = '';
    var len = data.length;

    for( var i = 0; i < len; i++ ) {
      var s = '';

      // Make the person's own country the default selection.
      if( data[i].id == country ) {
        s = '" selected="selected"';
      }

      h += '<option value="' + data[i].id + s + '">' + data[i].name + '</option>';
    }

    $('#country').html(h);
  });

Acquire Cities

The following code downloads the list of cities. The tokenInput function automatically passes the q parameter to the code inside the "local version" of city.dhtml (the PHP source code snippet is shown above). The q parameter is the text that the user types for the city.

  $('#city').tokenInput( 'city.dhtml', {
    hintText: "Type a city name.",
    tokenLimit: 1,
    classes: {
      tokenList: "token-input-list-facebook",
      token: "token-input-token-facebook",
      tokenDelete: "token-input-delete-token-facebook",
      selectedToken: "token-input-selected-token-facebook",
      highlightedToken: "token-input-highlighted-token-facebook",
      dropdown: "token-input-dropdown-facebook",
      dropdownItem: "token-input-dropdown-item-facebook",
      dropdownItem2: "token-input-dropdown-item2-facebook",
      selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
      inputToken: "token-input-input-token-facebook"
    }
  });

The city.dhtml must to filter the cities by country code.

Question

How would you provide the country code to city.dhtml, without using a cookie?

Ideas

The latest version of the token input supports JSONP, would that be useful?

Thank you!

  • 写回答

2条回答 默认 最新

  • duanri1985 2011-05-02 00:55
    关注

    The problem is that the tokenInput URL parameter cannot be changed once the input has been set. While perhaps possible to delete the existing tokenInput and recreate it each time a new country is selected, the solution would be a hack.

    The solution is to use a patch for the jQuery TokenInput that allows setting the URL parameter based on the result from a function call.

    https://github.com/loopj/jquery-tokeninput/pull/77

    Apply the patch and use the following code to change the query dynamically:

      function cityURL() {
        return 'city.dhtml?c=' + $('#country').val();
      }
    
      $('#city').tokenInput( cityURL, { /* ... */ });
    

    And disable caching in jquery.tokeninput.js (near line 650):

        //var cached_results = cache.get(query);
        var cached_results = false;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误