通过引用传递/存储数组?

发布于 2024-11-30 12:49:40 字数 1503 浏览 0 评论 0原文

我对 javascript 还很陌生。我下载了一些代码来为搜索框制作自动完成器,但它没有按我的预期工作。

这是我下载的代码:

Autocompleter.Local = new Class({

    Extends: Autocompleter,

    initialize: function (element, tokens, options) {
        this.parent(element, options);
        console.log(tokens); // I added this
        this.tokens = tokens;
    },

    query: function () {
        console.log(this.tokens); // and this
        this.update(this.filter());
    }

});

为了使用此代码,我这样做:

var Activities = ['aa', 'bb', 'cc', 'abcd'];

function InitializePage() {

    new Autocompleter.Local('txtSearch', Activities, {
        'minLength': 1, // We wait for at least one character
        'overflow': false, // Overflow for more entries
        'selectMode': 'type-ahead'
    });
}

window.addEvent('domready', function () {
   InitializePage();
});

初始化函数的 tokens 参数是自动完成程序可能建议的所有可能内容的数组。 filter() 函数会过滤这些内容,以便仅向用户显示相关的内容。我想要做的是在不同时间更改 tokens 数组,以便向用户建议不同的内容。

我尝试这样做:

new Request.JSON({ method: 'get', url: 'handler/GetActivities.ashx', autoCancel: true, urlEncoded: false, secure: false,
    headers: { "Content-type": "application/json" },
    onSuccess: function (_json, _jsonText) {
        Activities = _json;
    }
}).send();

我已确认此请求正在更新 Activities 变量。但是,自动完成器类中的 tokens 变量仍保留为其初始化时的数组。

如何获取它以使 tokens 变量指向与 activities 变量相同的数据?

I'm fairly new to javascript. I downloaded some code to make an autocompleter for search boxes, but it's not working as I expected it.

This is the code that I downloaded:

Autocompleter.Local = new Class({

    Extends: Autocompleter,

    initialize: function (element, tokens, options) {
        this.parent(element, options);
        console.log(tokens); // I added this
        this.tokens = tokens;
    },

    query: function () {
        console.log(this.tokens); // and this
        this.update(this.filter());
    }

});

To make use of this code, I do this:

var Activities = ['aa', 'bb', 'cc', 'abcd'];

function InitializePage() {

    new Autocompleter.Local('txtSearch', Activities, {
        'minLength': 1, // We wait for at least one character
        'overflow': false, // Overflow for more entries
        'selectMode': 'type-ahead'
    });
}

window.addEvent('domready', function () {
   InitializePage();
});

The tokens parameter for the initialize function is an array of all the possible things that the autocompleter might suggest. the filter() function filters these so only relevant ones are displayed to the user. What I want to do is change the tokens array at various times so different things are suggested to the user.

I tried doing this:

new Request.JSON({ method: 'get', url: 'handler/GetActivities.ashx', autoCancel: true, urlEncoded: false, secure: false,
    headers: { "Content-type": "application/json" },
    onSuccess: function (_json, _jsonText) {
        Activities = _json;
    }
}).send();

I have confirmed that the Activities variable is being updated by this request. However, the tokens variable inside the autocompleter class remains as the array that it was initialized as.

How do I get it so that the tokens variable points to the same data as the activities variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2024-12-07 12:49:40

问题是您正在覆盖数组。您应该替换这些元素。您可以使用 splice [MDN]

Activities.splice(0, Activities.length, _json[0], _json[1], ...);

这将删除所有元素(从 0Activities.length)并添加每个元素_json 数组。

但是 _json 可能包含未知数量的元素,像我们上面那样对其进行硬编码是非常糟糕的。幸运的是,我们可以使用 apply [MDN] 方法调用具有可变数量参数的方法,以数组形式提供:

var params = [0, Activities.length].concat(_json);
Activities.splice.apply(Activities, params);

这与上面的调用等效,但添加了 _json

参考: concat 拼接申请

The problem is that you are overriding the array. You should replace the elements instead. You can add and remove elements in-place with splice [MDN]:

Activities.splice(0, Activities.length, _json[0], _json[1], ...);

This would remove all elements (from 0 to Activities.length) and add each element from the _json array.

But _json might contain an unknown number of elements, and hardcoding it like we did above is very bad. Fortunately, we can use the apply [MDN] method to call a method with a variable number of arguments, provided as array:

var params = [0, Activities.length].concat(_json);
Activities.splice.apply(Activities, params);

This is the equivalent to the call above, but adding all elements from _json.

Reference: concat, splice, apply

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文