Skip to content

Instantly share code, notes, and snippets.

@augustyip
Created April 9, 2014 08:45
Show Gist options
  • Save augustyip/10242921 to your computer and use it in GitHub Desktop.
Save augustyip/10242921 to your computer and use it in GitHub Desktop.
Drupal Views data to jqGrid ajax data mapping
<?php
function views_to_jqgrid_ajax_data($view_name){
$result = array();
$_GET['page'] = intval($_REQUEST['page']) > 0
? intval($_REQUEST['page']) - 1 : intval($_REQUEST['page']);
$_GET['order'] = $_REQUEST['sidx'];
$_GET['sort'] = $_REQUEST['sord'];
$view = views_get_view($view_name, TRUE);
if (! $view)
return $output;
if (isset($_REQUEST['rows']) && intval($_REQUEST['rows']) > 0) {
$view->set_items_per_page(trim(intval($_REQUEST['rows'])));
}
if (isset($_REQUEST['_search']) && $_REQUEST['_search'] == 'true'){
foreach ($_GET as $key => $value) {
$view->exposed_input[$key] = $value;
}
}
$view->execute();
$items_per_page = $view->get_items_per_page();
$_REQUEST['rows'] = $items_per_page;
$field_alias = array();
foreach ($view->display_handler->handlers['field'] as $key => $value) {
array_push($field_alias, $value->field_alias);
}
$result['page'] = $_REQUEST['page'];
$result['total'] = ceil( $view->total_rows / $items_per_page);
$result['records'] = $view->total_rows;
$view_result = $view->result;
$result['rows'] = array();
foreach ($view->result as $r) {
$temp = array();
$temp['id'] = $r->sid;
$temp['cell'] = array();
foreach ($field_alias as $alias) {
array_push($temp['cell'], $r->{$alias});
}
array_push($result['rows'], $temp);
}
if (sizeof(module_implements('jqgird_data_alter')) > 0) {
$result = module_invoke_all('jqgird_data_alter', $result);
}
echo json_encode($result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment