Perl DBI 查询 -> JSON-> JQuery 自动完成

发布于 2024-11-03 15:48:28 字数 807 浏览 0 评论 0原文

我一直在尝试阅读如何实现 JSON 解决方案,以便使用 JQueryUI 的自动完成功能< /a>.我正在尝试使用自动完成功能在数据库中搜索名称,然后在选择后将 ID 填充到隐藏对象。我在网上看到了很多例子,但还没有找到实现这一点的最佳方法。数据库不会经常更改,因此我不确定如何最好地实现这种性能。

后端:

#!/usr/bin/perl 
use CGI;
use DBI;
use strict;
use warnings;

my $cgi = CGI->new;
my $dbh = DBI->connect('dbi:mysql:hostname=localhost;database=test',"test","test") or die $DBI::errstr;

my $sth = $dbh->prepare(qq{select id, name from test;}) or die
$dbh->errstr;
$sth->execute() or die $sth->errstr;
my $json = undef;
while(my @user = $sth->fetchrow_array()) {
   $json .= qq{{"$user[0]" : "$user[1]"}};
}

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

I've been trying to read up on how to implement a JSON solution in order to use JQueryUI's autocomplete functionality. I am trying to use autocomplete to search a database on for a name and after selection populate the ID to a hidden object. I've seen alot of examples around the web, but haven't found the best way to implement this. The database doesn't change that often, so I'm not sure how to best approach this performance wise.

Backend:

#!/usr/bin/perl 
use CGI;
use DBI;
use strict;
use warnings;

my $cgi = CGI->new;
my $dbh = DBI->connect('dbi:mysql:hostname=localhost;database=test',"test","test") or die $DBI::errstr;

my $sth = $dbh->prepare(qq{select id, name from test;}) or die
$dbh->errstr;
$sth->execute() or die $sth->errstr;
my $json = undef;
while(my @user = $sth->fetchrow_array()) {
   $json .= qq{{"$user[0]" : "$user[1]"}};
}

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

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

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

发布评论

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

评论(3

提笔书几行 2024-11-10 15:48:28

jQuery 自动完成需要与 json 结果一起返回的“值”或“标签”字段。如果不包含它,jquery 自动完成将不起作用:

自动完成的基本功能适用于分配给“标签”和“值”字段的查询结果。 jQuery UI 站点对“label”和“value”字段的解释:

“本地数据可以是一个简单的字符串数组,也可以包含数组中每个项目的对象,具有 label 或 value 属性或两者兼而有之。标签属性显示在建议菜单中。当用户从菜单中选择某些内容后,该值将被插入到输入元素中。如果只指定一个属性,它将用于两个属性,例如。如果您仅提供值属性,则该值也将用作标签。”

链接到完整示例: http ://www.jensbits.com/2011/05/09/jquery-ui-autocomplete-widget-with-perl-and-mysql/

The jQuery autocomplete needs a "value" or "label" field returned with the json result. If you do not include it, the jquery autocomplete will not work:

The basic functionality of the autocomplete works with the results of the query assigned to the ‘label’ and ‘value’ fields. Explanation on the ‘label’ and ‘value’ fields from the jQuery UI site:

“The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.”

Link to full example: http://www.jensbits.com/2011/05/09/jquery-ui-autocomplete-widget-with-perl-and-mysql/

你好,陌生人 2024-11-10 15:48:28

您需要从 CPAN 获取 JSON 包,而不是执行以下操作例如

my $json = undef;
while(my @user = $sth->fetchrow_array()) {
   $json .= qq{{"$user[0]" : "$user[1]"}};
}

,对于 JSON,它看起来像这样:

use JSON;
my $json = {};
while(my @user = $sth->fetchrow_array()) {
   $json->{$user[0]} = $user[1];
}
print JSON::to_json($json);

JSON 包将自动从您提供的任何 Perl 数据结构构造一个有效的 JSON 字符串。我们在 Melody 上到处使用它,事实证明它是一个真正的救星,可以将结构明智地转换为有效的 JSON。

You need to grap the JSON package from CPAN instead of doing this:

my $json = undef;
while(my @user = $sth->fetchrow_array()) {
   $json .= qq{{"$user[0]" : "$user[1]"}};
}

For example, with JSON it'd look like this:

use JSON;
my $json = {};
while(my @user = $sth->fetchrow_array()) {
   $json->{$user[0]} = $user[1];
}
print JSON::to_json($json);

The JSON package will automatically construct a valid JSON string from any Perl data structure you provide it. We use it all over the place on Melody and it's proved to be a real life saver for sanely converting a structure into valid JSON.

攀登最高峰 2024-11-10 15:48:28

这里我要说的是性能。

您可以设置一些触发器来提高性能,客户端您可以设置发送请求之前所需的最小字符数。
您还可以设置发送请求之前输入的两个字符之间的“超时”。

如果您的数据库表确实很大,我建议您对检索的结果设置限制。
首先是为了避免长时间的请求处理,但也是因为像 IE6 这样的一些客户端并不能真正快速地处理超过一百个结果(并不是说,它也不是真正的用户友好)。

在使用 IE6 的项目中,我们将返回的元素限制为 100 个。如果用户无法将搜索减少到 100 个元素,我们假设他/她不知道他/她在寻找什么。

希望它能有所帮助。

Here I'm talking about performance.

There is some trigger you can set to improve performance, client side you can set the minimum number of characters required before the request is sent.
You can also set the "timeout" between two characters typing before the request is sent.

If your database table is really huge, I suggest you put a LIMIT on results you retrieve.
First to avoid long request processing, but also because some clients like IE6 arent't really fast handling more than a hundred results (Not to say, it's also not really user friendly).

On a project using IE6, we limited the elements returned to 100. If the user can't reduce the search to 100 elements, we presume he/she doesn't know what he/she is looking for.

Hope it helps a bit.

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