Skip to content

Instantly share code, notes, and snippets.

@haifahrul
Created August 23, 2019 03:40
Show Gist options
  • Save haifahrul/2277d119eeb0b1d0274b2891d488a9cd to your computer and use it in GitHub Desktop.
Save haifahrul/2277d119eeb0b1d0274b2891d488a9cd to your computer and use it in GitHub Desktop.
Yii2 Grid Jeasyui - Controller
public function actionGetData()
{
$request = Yii::$app->request;
Yii::$app->getResponse()->format = 'json';
$query = (new \yii\db\Query())
->select('m_unit.*, m_people.*')
->from("m_people")
->leftJoin('m_unit', 'm_unit.id = unit_id');
// searching
if (($q = $request->post('q'))) {
}
if (($request->post('searchtype') == 'str')) {
$query->andFilterWhere(['like', 'LOWER(' . $request->post('searchdata') . ')', strtolower($request->post('searchvalue'))]);
} else if (($request->post('searchtype') == 'int')) {
$query->andFilterWhere([$request->post('searchdata') => $request->post('searchvalue')]);
}
// sorting
if (($sort = $request->post('sort'))) {
$order = $request->post('order', 'asc');
$query->orderBy([$sort => $order == 'asc' ? SORT_ASC : SORT_DESC]);
} else {
}
// paging
$limit = $request->post('rows', 15);
$page = $request->post('page', 1);
$total = $query->count();
$query->offset(($page - 1) * $limit)->limit($limit);
return [
'total' => $total,
'rows' => $query->all(),
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment