使用“远程"时,Twitter提前键入不正确匹配,但使用“本地"时, JSON格式 [英] Twitter typeahead not matching correctly when using "remote", but working with "local" JSON

查看:150
本文介绍了使用“远程"时,Twitter提前键入不正确匹配,但使用“本地"时, JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 twitter typeahead 在Laravel 4中创建自动填充功能,但是匹配过程中出现问题.

I'm creating an autocomplete in Laravel 4 using twitter typeahead, but I'm having problems in the matching process.

JS 代码:

$('#autocomplete').typeahead({
  limit: 20,
  name: 'destinatari',
  prefetch: '{{URL::to("alumni/posta/utenti")}}',
  remote: '{{URL::to("alumni/posta/utenti")}}'
});

模型像这样填充数组:

public static function jsonList($idLogged)
{
  $users = DB::table('users_data')
             ->where('userid', '!=', $idLogged)
             ->select('nome', 'nome_2', 'cognome', 'cognome_2', 'userid')
             ->orderBy('cognome', 'asc')
             ->orderBy('nome','asc')
             ->get();
  $i = 0; 
  $ordered = array();
  foreach($users as $u) {
     $ordered[$i]['value'] = $u->nome.' '.$u->nome_2.' '.$u->cognome.' '.$u->cognome_2;
     $ordered[$i]['tokens'] = array($u->nome, $u->cognome);
     $ordered[$i]['userid'] = $u->userid;
     $i++;
  } 
  return $ordered;       
}

还有我的控制器:

return Response::json( Users::jsonList($this->userdata->id) );

返回的json (我在Firebug中看到了它)看起来像:

The returned json (I see it in Firebug) looks like:

[{"value":"Silvia Miriam Abeyta Carretero","tokens":["Silvia","Abeyta"],"userid":"246"},
{"value":"Malak Julia Abreu Garrido","tokens":["Malak","Abreu"],"userid":"198"},{"value":"Aina  Aguado ","tokens":["Aina","Aguado"],"userid":"243"},
{"value":"Jordi  Alarc\u00f3n ","tokens":["Jordi","Alarc\u00f3n"],"userid":"308"},
{"value":"Aaron Nerea Alejandro ","tokens":["Aaron","Alejandro"],"userid":"49"},
{"value":"Alexia  Alem\u00e1n ","tokens":["Alexia","Alem\u00e1n"],"userid":"306"},
{"value":"Salma  Almaraz ","tokens":["Salma","Almaraz"],"userid":"54"},
{"value":"Alma  Almonte Nev\u00e1rez","tokens":["Alma","Almonte"],"userid":"101"},
{"value":"Daniela  Almonte ","tokens":["Daniela","Almonte"],"userid":"184"}
,....other similar results....]

问题是我输入的所有字母只有列表中的名字会自动完成("Silvia Miriam Abeyta Carretero"),但是当我完整输入该字段没有填写任何其他名称(例如,"Daniela Almonte"),并且下拉列表始终显示全部20条结果,而没有进行任何处理.

The problem is that any letter I type only the first name in the list gets autocompleted ("Silvia Miriam Abeyta Carretero") in the input field, but when I type out in full any other name (say, "Daniela Almonte") the field isn't completed and the dropdown keeps showing the whole 20 results, without processing of any kind.

不过,我必须说,如果单击一个名称,它将被正确选择(我正在记录 datum userid 属性),但仍然自动补全功能不起作用.

I must say, though, that if I click on a name it gets selected correctly (I'm logging the userid property of the datum ), but still the autocompletion isn't working.

让我感到困惑的是,如果我直接将整个列表 复制/粘贴到JS中(从Firebug中按 获取),作为local属性>(而不是remote),一切正常.

What puzzles me is that If I copy/paste the whole list directly in the JS (taken as is from Firebug), as local property of typeahead() (instead of remote), everything works fine as it should.

可能是什么原因造成的?是Laravel回应问题吗?还是remote网址的typeahead处理器(我相信它在"transport.js"源文件中,使用jQuery $.ajax())?

What could be causing this? Is it a Laravel response problem? Or is it the typeahead processor (I believe it's in "transport.js" source file, uses jQuery $.ajax()) of the remote url?

似乎返回的JSON被视为单个条目,我不知道为什么,因为它对我来说看起来是正确的...

It seems like the returned JSON is considered as a single entry, i don't know why since it looks correct to me...

推荐答案

摘录自文档:

$('input.twitter-search').typeahead({
    name: 'accounts',
    prefetch: 'https://twitter.com/network.json',
    remote: 'https://twitter.com/accounts?q=%QUERY'
  });

如您所见,远程参数必须包含URL 和查询参数. 在PHP方面,您必须测试是否设置了参数"q",然后调整您的请求(例如,使用LIKE语句)以正确过滤列表.

As you can see, the remote parameter must contains the URL with the query parameter. PHP side, you have to test if the parameter "q" is set and then adapt your request (with a LIKE statement by example) to correctly filter the list.

否则,您可以尝试删除此远程参数,也许仅适用于prefetch属性.

Otherwise, you can try to remove this remote parameter, maybe it would work with only the prefetch attribute.

这篇关于使用“远程"时,Twitter提前键入不正确匹配,但使用“本地"时, JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆