[英]Yii: Customize the results of CAutoComplete
I need to make a dropdown list using CAutoComplete. 我需要使用CAutoComplete制作一个下拉列表。 Everything is set and works fine, here is my code of the action:
一切都设置好并且可以正常工作,这是我的操作代码:
<?php
public function actionSuggestCharacter() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
$name = $_GET['q'];
$criteria = new CDbCriteria;
$criteria->condition='`Character` LIKE :keyword';
$criteria->params=array(':keyword'=>"$name%");
$criteria->limit = 5;
$suggestions = zCharacter::model()->findAll($criteria);
$returnVal = '';
foreach($suggestions as $suggestion) {
$returnVal .= $suggestion->Character."\n";
}
if (isset($suggestion)) {
echo $returnVal;
}
$criteria->condition='`Character` LIKE :keyword';
$criteria->params=array(':keyword'=>"%$name%");
$criteria->limit = 5;
$suggestions = zCharacter::model()->findAll($criteria);
$returnVal = '';
foreach($suggestions as $suggestion) {
$returnVal .= $suggestion->Character."\n";
}
if (isset($suggestion)) {
echo $returnVal;
}
}
}
?>
What this code does is that it shows the first 5 matches with the keyword at the beginning and the next 5 matches are with the keyword in any place. 该代码的作用是,它在开头显示与关键字的前5个匹配项,然后在任何位置显示与关键字的下5个匹配项。
Example. 例。 Let's say a user types in the input field "pdd" (doesn't really matter, could be any text), so the results returned by autocomplete will look like:
假设用户在输入字段“ pdd”中输入(实际上并不重要,可以是任何文本),因此自动完成返回的结果如下所示:
1. pddtext...
2. pddtext...
3. pdd_some_other_text
4. pdd_text
5. pdd_text
1. text_text_pdd
2. text_pdd_text
3. etc...
The problem is I need to separate these two blocks by some kind of line ( <hr>
or <div>
with the border). 问题是我需要用某种线(带有边框的
<hr>
或<div>
)分隔这两个块。 How can I do this? 我怎样才能做到这一点?
Thank you. 谢谢。
Can't you do something like this? 你不能做这样的事吗?
<?php
public function actionSuggestCharacter() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
...
if (isset($suggestion)) {
echo $returnVal;
}
echo "Hey this is the delimiter\n";
$criteria->condition='`Character` LIKE :keyword';
....
}
}
?>
And then on the client side check for this string and when you encounter ""Hey this is the delimiter" replace it with your separator. 然后在客户端检查此字符串,并在遇到““嘿,这是分隔符”时将其替换为分隔符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.