Yii2 select2 optGroup不使用ajax

时间:2017-07-17 14:10:36

标签: php ajax yii2 jquery-select2

我正在尝试在kartik Select2上运行ajax搜索,但我遇到了一个我无法解决的问题。

查看:

use \kartik\select2\Select2;
use yii\web\JsExpression;


echo Select2::widget([
    'initValueText' => '', // set the initial display text
    'name' => 'GlobalSearchHeader',
    'size' => Select2::SMALL,
    'theme' => Select2::THEME_KRAJEE, // this is the default if theme is not set
    'options' => ['placeholder' => 'Search...'],
    'pluginOptions' => [

        'allowClear' => true,
        'ajax' => [
            'url' => 'index.php?r=site%2Fglobal-search',
            'dataType' => 'json',
            'data' => new JsExpression('function(params) { return {q:params.term}; console.log(params);}'),
            'processResults' => new JsExpression('function(data,page) { return {results:data.results}; }'),
            'cache' => true
        ],
    ],
]);

控制器:

 public function actionGlobalSearch($q = null) {

        $Data = ['Pages' => [['id' => 'index.php', 'text' => 'Home']]];


        if (!is_null($q)) {
            $Categories = new ActiveDataProvider([
                'query' => \app\models\CategoryOfItems::find()->innerJoin('category_of_items_trans', 'category_of_items_trans.CATEGORY_OF_ITEM_ID=category_of_items.CATEGORY_OF_ITEM_ID')->where('category_of_items_trans.CATEGORY_OF_ITEM_TRANS LIKE \'%' . $q . '%\'  AND LANGUAGE_ID=1'),
//                ->innerJoin('agenda_periode_translation','agenda_periode_translation.AGENDA_PERIOD_ID = AGENDA_PERIODE_ID')->andFilterWhere(['=', 'agenda_periode_translation.LANGUAGE_ID', 2]),
            ]);
            if ($Categories != null) {
                $Categories = $Categories->getModels();
                $i = 0;
                if ($Categories != null && sizeof($Categories) > 0) {
                    foreach ($Categories as $Category) {

                        $Data['Categories'][$i] = ['id' => 'index.php?r=category-of-items%2Findex&Reg=C&CategoryID=' . $Category->CATEGORY_OF_ITEM_ID];

                        $Data['Categories'][$i] = ['text' => $Category->categoryOfItemsTrans[0]->CATEGORY_OF_ITEM_TRANS];
                        $i++;
                    }
                }
            }


        }
        Yii::error(print_r($Data, true));
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return ['results' => $Data];
    }

Controller功能可以正常工作。这是返回的结果:

 Array
    (
    [Pages] => Array
        (
            [0] => Array
                (
                    [id] => index.php
                    [text] => Home
                )

        )

[Categories] => Array
    (
        [0] => Array
            (
                [text] => Financial Services
            )

        [1] => Array
            (
                [text] => General Services
            )

        [2] => Array
            (
                [text] => Site decoration
            )

        [3] => Array
            (
                [text] => Food & Beverage
            )

        [4] => Array
            (
                [text] => Music & Entertainment
            )

        [5] => Array
            (
                [text] => Body & Beauty Preparation
            )

        [6] => Array
            (
                [text] => Others: Dance, Souvenirs…
            )

        [7] => Array
            (
                [text] => Accommodation
            )

        [8] => Array
            (
                [text] => Transportation
            )

        [9] => Array
            (
                [text] => Trip Preparation
            )

        [10] => Array
            (
                [text] => Domestic Destinations
            )

        [11] => Array
            (
                [text] => Egypt, Mid-East, Turkey
            )

        [12] => Array
            (
                [text] => Russia, Armenia, Central Asia
            )

        [13] => Array
            (
                [text] => South & South-East Asia
            )

        [14] => Array
            (
                [text] => Eastern Asia
            )

        [15] => Array
            (
                [text] => Eastern Europe
            )

        [16] => Array
            (
                [text] => Eastern Africa
            )

        [17] => Array
            (
                [text] => Western Africa
            )

        [18] => Array
            (
                [text] => Southern Africa
            )

        [19] => Array
            (
                [text] => Northern Africa
            )

    )

但是生成的错误是一个javascript错误,我认为问题是processResults选项。

这是产生的错误:

select2.full.js:4008 Uncaught TypeError: data.slice is not a function
    at DecoratedClass.HidePlaceholder.removePlaceholder (select2.full.js:4008)
    at DecoratedClass.removePlaceholder (select2.full.js:580)
    at DecoratedClass.HidePlaceholder.append (select2.full.js:3991)
    at DecoratedClass.append (select2.full.js:580)
    at Select2.<anonymous> (select2.full.js:1011)
    at Select2.Observable.invoke (select2.full.js:637)
    at Select2.Observable.trigger (select2.full.js:627)
    at Select2.trigger (select2.full.js:5472)
    at select2.full.js:5331
    at Object.options.transport.self.trigger.message (select2.full.js:3468)

请,我需要你的帮助,我无法找到解决方案。 先感谢您。

0 个答案:

没有答案