Skip to content

Instantly share code, notes, and snippets.

@wisaruthk
Last active January 15, 2017 10:39
Show Gist options
  • Save wisaruthk/b2a4afa0dcd96ccdeb57688b194132ee to your computer and use it in GitHub Desktop.
Save wisaruthk/b2a4afa0dcd96ccdeb57688b194132ee to your computer and use it in GitHub Desktop.
Example Kartik gridview with select2 filter column
/**
* For Kartic select2 widget
* @param type $q
* @param type $id
* @return type
*/
public function actionLotList($q = null, $id = null) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = ['results' => ['id' => '', 'text' => '']];
if (!is_null($q)) {
$query = new Query;
$query->select('id, name AS text')
->from('stock_lot')
->where(['like', 'name', $q])
->limit(20);
$command = $query->createCommand();
$data = $command->queryAll();
$out['results'] = array_values($data);
}
elseif ($id > 0) {
$out['results'] = ['id' => $id, 'text' => StockLot::find($id)->name];
}
return $out;
}
[
'attribute'=>'lot_id',
'value'=>'lot.name',
'label'=>'Control No.',
'filterType'=> GridView::FILTER_SELECT2,
'filterWidgetOptions'=>[
'initValueText'=>'hello',
'options' => ['placeholder' => 'Search for a city ...'],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 1,
'language' => [
'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
],
'ajax' => [
'url' => Url::to(['/stock/stock-lot/lot-list']),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(lot) { return lot.text; }'),
'templateSelection' => new JsExpression('function (lot) { return lot.text; }'),
],
]
],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment