php - How to use widget kartik select2 with ajax & templates in yii2

724

I want to display more than 1 information when searching, so I use select2 ajax & templates. It use json. I change the url and I make function on my controller. But I have a problem. It can not show anything. What's the problem? This is my code: view

$formatJs = <<< 'JS'
 var formatProduct = function (product) {
  if (product.loading) {
  return product.text;
  }
 var markup =
  '<div >' + 
   '<div >' +
    '<b >' + product.name + '</b>' + 
   '</div>' +
   '<div ><i ></i> ' + product.ean_no + '</div>' +
   '<div ><i ></i> ' + product.desc + '</div>' +
  '</div>';
  return '<div >' + markup + '</div>';
 };
 var formatProductSelection = function (product) {
  return product.name || product.text;
 }
JS;

// Register the formatting script
$this->registerJs($formatJs, \yii\web\View::POS_HEAD);

// script to parse the results into the format expected by Select2
$resultsJs = <<< JS
  function (data, params) {
    params.page = params.page || 1;
    return {
      // Change `data.items` to `data.results`.
      // `results` is the key that you have been selected on
      // `actionJsonlist`.
      results: data.results
    };
  }
JS;

Select2

echo Select2::widget([
        'name' => 'kv-repo-template',
        'value' => '14719648',
        'initValueText' => 'kartik-v/yii2-widgets',
        'options' => ['placeholder' => 'Search for a repo ...'],
        'pluginOptions' => [
            'allowClear' => true,
            'minimumInputLength' => 1,
            'ajax' => [
                'url' => Url::to(['/bom/product/productlist']),
                'dataType' => 'json',
                'delay' => 250,
                'data' => new JsExpression('function(params) { return {q:params.term, page: params.page}; }'),
                'processResults' => new JsExpression($resultsJs),
                'cache' => true
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('formatProduct'),
            'templateSelection' => new JsExpression('formatProductSelection'),
        ],
    ]); 

controller

public function actionProductlist($search = NULL, $code = NULL)
{
    header('Content-type: application/json');
    $clean['more'] = false;

    $query = new \yii\db\Query;
    if(!is_Null($search))
    {
        $mainQuery = $query->select('code, name, description, volume')
                           ->from('product');
        $command = $mainQuery->createCommand();
        $rows = $command->queryAll();
        $clean['results'] = array_values($rows);
    }
    else
    {
        if(!is_null($code))
        {
            $clean['results'] = ['ean_no'=> $code, 'name' => Product::find($code)->nama,
                'description' => Product::find($code)->description, 'volume' => Product::find($code)->volume];
        }else
        {
            $clean['results'] = ['ean_no' => 123, 'name' => 'None found', 'description' => 'None found', 'volume' => 'None found'];
        }
    }
    echo \yii\helpers\Json::encode($clean);
    exit();
}

on mozilla when i open the inspect element console. there is an error message like this :

TypeError: data.slice is not a function S2
701

Answer

Solution:

Try to remove

'processResults' => new JsExpression($resultsJs),

People are also looking for solutions to the problem: get specific data values from html in php without using js

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.