使用 $.map 函数的 jQuery UI 自动完成缓存

发布于 2024-10-07 05:46:01 字数 1738 浏览 4 评论 0原文

我正在尝试使用 jQuery UI 自动完成来实现缓存。 我正在使用 jQuery 1.4.4 和 UI 1.8.6

这是我工作的基本代码:

$('#searchbox').autocomplete({
    source: function(request, response) {
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

这是我通过查看示例来使缓存正常工作的尝试:

var cache = {},
    lastXhr;
$('#searchbox').autocomplete({
    source: function(request, response) {
        var term = request.term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }));
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = $.map(data, function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }); 
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

有接受者吗?

I am trying to implement caching using jQuery UI autocomplete.
I am using jQuery 1.4.4 and UI 1.8.6

Here is the basic code that I got working:

$('#searchbox').autocomplete({
    source: function(request, response) {
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

Here is my attempt to get caching to work from looking at the example:

var cache = {},
    lastXhr;
$('#searchbox').autocomplete({
    source: function(request, response) {
        var term = request.term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }));
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = $.map(data, function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }); 
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

Any takers out there?

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

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

发布评论

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

评论(2

浮光之海 2024-10-14 05:46:01

这是我使用 cache 的 jQuery UI 自动完成的工作示例。希望它有帮助:

    var cache = {};
    $("#textbox").autocomplete({
      source: function(request, response) {
       if (request.term in cache) {
        response($.map(cache[request.term].d, function(item) {
         return { value: item.value, id: item.id }
        }))
        return;
       }
       $.ajax({
        url: "/Services/AutoCompleteService.asmx/GetEmployees",  /* I use a web service */
        data: "{ 'term': '" + request.term + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function(data) { return data; },
        success: function(data) {
         cache[request.term] = data;
         response($.map(data.d, function(item) {
          return {
           value: item.value,
           id: item.id
          }
         }))
        },
        error: HandleAjaxError  // custom method
       });
      },
      minLength: 3,
      select: function(event, ui) {
       if (ui.item) {
        formatAutoComplete(ui.item);   // custom method
       }
      }
     });

如果您现在还没有这样做,请获取Firebug。它是 Web 开发的宝贵工具。您可以在此 JavaScript 上设置断点,看看会发生什么。

Here's my working example of jQuery UI's autocomplete using cache. Hope it helps:

    var cache = {};
    $("#textbox").autocomplete({
      source: function(request, response) {
       if (request.term in cache) {
        response($.map(cache[request.term].d, function(item) {
         return { value: item.value, id: item.id }
        }))
        return;
       }
       $.ajax({
        url: "/Services/AutoCompleteService.asmx/GetEmployees",  /* I use a web service */
        data: "{ 'term': '" + request.term + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function(data) { return data; },
        success: function(data) {
         cache[request.term] = data;
         response($.map(data.d, function(item) {
          return {
           value: item.value,
           id: item.id
          }
         }))
        },
        error: HandleAjaxError  // custom method
       });
      },
      minLength: 3,
      select: function(event, ui) {
       if (ui.item) {
        formatAutoComplete(ui.item);   // custom method
       }
      }
     });

If you're not doing so by now, get Firebug. It's an invaluable tool for web development. You can set a breakpoint on this JavaScript and see what happens.

债姬 2024-10-14 05:46:01

当我尝试将 $.map 函数放入其中时,问题出在我的缓存[term] 中,因为不需要它。

cache[term] = $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                });

因此,对于那些仍然遇到问题的人来说,这是我的最终脚本:
我还保留了所有选项以避免任何混淆。

var cache = {},
 lastXhr;

$('#searchbox').autocomplete({
    source: function(term, response) {
        var term = term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    /*Format autocomplete to display name and job title, e.g., Smith, John, Web Developer*/
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    /*Replace the searched value with the value selected.*/
                    value: item.NAME
                };
            }))
            /*I happened to leave this out, which I think what one of the main cause my caching did not work.*/
            return;
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = data;
            if (xhr === lastXhr) {
                response($.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

The problem lies in my cache[term] when I was trying to throw my $.map function in it because it is not needed.

cache[term] = $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                });

So here is my final script for those who are still having trouble:
I also left all option out of this to avoid any confusion.

var cache = {},
 lastXhr;

$('#searchbox').autocomplete({
    source: function(term, response) {
        var term = term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    /*Format autocomplete to display name and job title, e.g., Smith, John, Web Developer*/
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    /*Replace the searched value with the value selected.*/
                    value: item.NAME
                };
            }))
            /*I happened to leave this out, which I think what one of the main cause my caching did not work.*/
            return;
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = data;
            if (xhr === lastXhr) {
                response($.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文