如何使用查询字符串将数据填充到extjs组件?

时间:2010-02-03 11:22:52

标签: javascript url extjs query-string

例如,如果我有extjs ComboBox,并且我有像http://localhost:8080?param=value1,value2,value3

这样的网址

如何在ExtJS中访问此<< param >> value,以便我可以将其转换为Ext.data.Array或....

3 个答案:

答案 0 :(得分:5)

使用框架解码查询字符串:

var params = Ext.urlDecode(location.search.substring(1));
var array = [];
for (var name in params) {
    array.push([name, params[name]]);
}

现在array适合用作dataArrayStore的字段namevalue

答案 1 :(得分:2)

我有这个(基于上面的代码@Lloyd):

var the_query_string = new Object();
(function() {
 var qs = location.search.substr(1).replace(/\+/g, ' ').split('&');
 for (var i = 0; i < qs.length; i++) {
  qs[i] = qs[i].split('=');
  if (qs[i][0])
   the_query_string[qs[i][0]] = decodeURIComponent(qs[i][1]);
 }
})();

所以我可以全局访问任何querystring变量。例如当
location.search = '?search=hello+world&foo=bar%3Dbar'
然后
the_query_string.search = 'hello world'
the_query_string.foo = 'bar=bar'

答案 2 :(得分:1)

我使用前一段时间发现的JavaScript文件:

/* Client-side access to querystring name=value pairs
    Version 1.2.3
    22 Jun 2005
    Adam Vandenberg
*/

function QueryString(qs) { // optionally pass a querystring to parse
    this.params = new Object()
    this.get = QueryString_get

    if (qs == null)
        qs=location.search.substring(1,location.search.length)

    if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ')
    var args = qs.split('&') // parse out name/value pairs separated via &

// split out each name=value pair
    for (var i = 0, len = args.length; i < len; i++) {
        var value;
        var pair = args[i].split('=')
        var name = unescape(pair[0])

        if (pair.length == 2)
            value = unescape(pair[1])
        else
            value = name

        this.params[name] = value
    }
}

function QueryString_get(key, default_) {
    // This silly looking line changes UNDEFINED to NULL
    if (default_ == null) default_ = null;

    var value=this.params[key]
    if (value==null) value=default_;

    return value
}

你做的是:

var param = new QueryString().get("param");
var values = param.split(",");

值将是您的数组。