I am working on stock management system , with a large number of products and the customer wan to display the products in a Select2 widget exactly like kartik widget, and he want to represent the data more and more when scrolling down ,is it available or can be done using this widget or I have to create my own ? this is my code
<?php
$url = Url::to ( [ 'products-list' ] );
echo Select2::widget ( [
'name' => 'state_10' ,
'options' => [
'id' => 'select_product' ,
'placeholder' => 'Select Product...' ,
'multiple' => false ,
'class' => ''
] ,
'pluginOptions' => [
'allowClear' => true ,
'minimumInputLength' => 1 ,
'ajax' => [
'url' => $url ,
'dataType' => 'json' ,
'data' => new JsExpression ( 'function(params) { return {q:params.term}; }' )
] ,
'escapeMarkup' => new JsExpression ( 'function (markup) { return markup; }' ) ,
'templateResult' => new JsExpression ( 'function(product) { console.log(product);return product.text; }' ) ,
'templateSelection' => new JsExpression ( 'function (subject) { return subject.text; }' ) ,
] ,
] );
?>
and I have this action in my controller and every thing is working well but I need to start with displaying a number of products in select2 and then when scrolling start getting more and more
controller action :
public function actionProductsList($q = null, $id = null) {
Yii::$app->response->format = Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '', 'qtyLeft' => '', 'serialNumbers' => '']];
if (!is_null($q)) {
$query = new Query;
$query->select(['product_id as id', new Expression("CONCAT(product_name,' -- ',product_qty_left) AS text, product_qty_left as qtyLeft, s.serial_number as serialNumbers")])
->from('product')
->join('LEFT JOIN', 'serial_number s', 's.r_product_id = product.product_id')
->where(['like', 'product_name', $q]);
// ->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
} elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => AppProduct::find()->where(['product_id' => $id])->one()->product_name];
}
return $out;
}