Yii: Customize the results of CAutoComplete

为君一笑 提交于 2019-12-11 16:20:00

问题


I need to make a dropdown list using 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.

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:

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). How can I do this?

Thank you.


回答1:


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.



来源:https://stackoverflow.com/questions/1923355/yii-customize-the-results-of-cautocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!