如何使用小部件kartik select2与ajax& yii2中的模板

时间:2017-10-10 07:42:05

标签: php ajax yii2

我想在搜索时显示多个信息,所以我使用select2 ajax&模板。它使用json。我更改了网址并在控制器上创建了功能。但我有一个问题。它无法显示任何内容。有什么问题?这是我的代码: 查看

$formatJs = <<< 'JS'
 var formatProduct = function (product) {
   if (product.loading) {
    return product.text;
   }
 var markup =
    '<div class="row">' + 
      '<div class="col-sm-5">' +
        '<b style="margin-left:5px">' + product.name + '</b>' + 
      '</div>' +
      '<div class="col-sm-3"><i class="fa fa-code-fork"></i> ' + product.ean_no + '</div>' +
      '<div class="col-sm-3"><i class="fa fa-star"></i> ' + product.desc + '</div>' +
    '</div>';
    return '<div style="overflow:hidden;">' + 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;

选择二

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'),
        ],
    ]); 

控制器

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();
}
当我打开inspect元素控制台时,在mozilla上

。有这样的错误消息:

TypeError:data.slice不是函数 S2

1 个答案:

答案 0 :(得分:0)

尝试删除

SELECT * FROM TABLE_NAME WHERE status = 'Updated'
                     AND 
 TOTAL_WEEKDAYS(dateModified, NOW()) >=7;