V
V
Victor Umansky2017-06-18 18:48:03
Yii
Victor Umansky, 2017-06-18 18:48:03

Smart search, data in the field is not displayed?

Hello everyone, help with smart search or live search, the problem is this, after I start typing product names in the input field, the products found are displayed, but after I have selected the product, nothing is displayed in the search field, it’s just empty and when I send it by post to the controller in order to show the user what he found, then $ _POST is empty (((
b77168051f8d42cfa210c9050ba81dd8.png
main

<?php $template = '<div><span style="margin-right: 20px"><img style="width: 35px" src="{{srs}}"></span><span>{{title}}</span>&nbsp;<span style="background: #fef200;color: #000;font-weight: bold;padding: 4px">{{price}}&nbsp;грн.</span></div>'; ?>
        <?= Typeahead::widget([
            'name' => 'product',
            'options' => ['placeholder' => 'Поиск товаров'],
            'pluginOptions' => ['highlight' => true],
            'dataset' => [
                [
                    'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
                    'display' => 'value',
                    //'prefetch' => $baseUrl . '/samples/countries.json',
                    'remote' => [
                        'url' => Url::to(['/search/product-list']) . '?q=%QUERY',
                        'wildcard' => '%QUERY'
                    ],
                    'templates' => [
                        'notFound' => '<div class="text-danger" style="padding:0 8px">Ничего не найдено.</div>',
                        'suggestion' => new \yii\web\JsExpression("Handlebars.compile('{$template}')")
                    ]
                ]
            ],

        ]) ?>

controller
public function actionProductList($q = null)
    {
        $query = Product::find()
            ->where('title LIKE "%' . $q . '%"')
            ->orderBy('title');

        $out = [];
        foreach ($query->all() as $d) {
            /**
             * @var $d Product
             * @var $image ObjectFile
             */
            $image = ObjectFile::find()
                ->where(['id' => $d->img_id])
                ->one();

            $srs = null;
            if ($image) {
                $srs = '/web/uploads/products/' . Product::getImageName($image->id);
            }

            $out[] = [
                'title' => $d->title,
                'srs' => $srs,
                'price' => $d->price
            ];
        }
        echo Json::encode($out);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question