Last active
January 15, 2017 10:39
-
-
Save wisaruthk/b2a4afa0dcd96ccdeb57688b194132ee to your computer and use it in GitHub Desktop.
Example Kartik gridview with select2 filter column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
'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