ui自动完成格式化结果在2行
我在http://jqueryui.com/demos/autocomplete/#remote中使用ui autocomplete在search.php中使用PHP来返回结果。
我试图得到我的自定义输出
Company Name | Contact Name
这来自以下代码:
if(is_array($rs) && count($rs) > 0){ foreach ($rs as $item) { //format: "Name Surname=>cid_uid" $json = array(); $json['id'] = $item['parentCompanyId'].'_'.$item['uid']; $json['label'] = $item['companyName'] . ' | ' . $item['name'] . ' ' . $item['surname']; $data[] = $json; } }
这非常好用,但是为了更容易阅读结果,我想在
Contact Name
Company Name | Department Name
我尝试过以下方法:
$json['label'] = $item['name'] . ' ' . $item['surname'] . '\n' .$item['companyName'];
和
$json['label'] = $item['name'] . ' ' . $item['surname'] . '
' .$item['companyName'];
和
$json['label'] = $item['name'] . ' ' . $item['surname'] . '\\n' .$item['companyName'];
所有尝试都会导致列表显示实际的
标记或\n
而不是推送到下一行。
使用firebug查看源代码显示Name<br>Company
不确定是否发生这种情况是因为:
header("Content-type: application/json"); echo json_encode($data);
请注意,我的问题与通过autocomplete / php / json获取HTML输出在
钽
覆盖_renderItem
方法:
$("#autocomplete").autocomplete() .data( "autocomplete" )._renderItem = function( ul, item ) { return $( "" ) .data( "item.autocomplete", item ) .append( item.label ) .appendTo( ul ); };
文档中的这个演示做了同样的事情: http : //jqueryui.com/demos/autocomplete/#custom-data
默认情况下, .append( item.label )
是.text( item.label )
,这就是为什么你的
被替换为<br />