00001 <?php
00010 class tpjqGrid extends Plugin
00011 {
00012 var $guid = 0;
00013
00017 function tpjqGrid() {
00018 $this->Plugin();
00019 $this->depend('html','form');
00020 }
00021
00022 function build($url, $params)
00023 {
00024 assert_type($params['columns'], 'array');
00025
00026
00027 $url = $this->depends->html->composite_url($url, array(
00028 '_ajax' => 1,
00029 'cols' => urlencode(implode('&', array_keys($params['columns'])))
00030 ));
00031
00032 $grid = array(
00033 'url' => $url,
00034 'datatype' => $this->_getparam($params, 'datatype', 'json'),
00035 'height' => $this->_getparam($params, 'height', ''),
00036 'width' => $this->_getparam($params, 'width', ''),
00037 'loadText' => __('Loading...'),
00038 'altRows' => true,
00039 'loadui' => 'block',
00040 'colNames' => array(),
00041 'colModel' => array(),
00042 'rowNum' => $this->_getparam($params, 'perpage', 10),
00043 'rowList' => array(10,50,200,1000),
00044 'viewrecords' => true,
00045 'caption' => $this->_getparam($params, 'caption', ''),
00046
00047 'prmNames' => array(
00048 'page' => 'p_p',
00049 'rows' => 'p_pp',
00050 'sort' => 's_f',
00051 'order' => 's_d'
00052 ),
00053 'toolbar' => array(true,'top'),
00054 );
00055
00056 foreach($params['columns'] as $name=>$col) {
00057 $grid['colNames'][] = $col['label'];
00058 $gcol = array(
00059 'name' => $name,
00060 'index' => $col['expr'] ? $col['expr'] : $name,
00061
00062
00063 );
00064
00065 $this->_optvar($col, 'width', $gcol);
00066 $this->_optvar($col, 'align', $gcol);
00067 $this->_optvar($col, 'date_format', $gcol, 'datefmt');
00068 $this->_optvar($col, 'editable', $gcol);
00069 $this->_optvar($col, 'search', $gcol, 'searchable');
00070 $this->_optvar($col, 'sortable', $gcol);
00071
00072 $grid['colModel'][] = $gcol;
00073 }
00074
00075 $tbl_id = $this->_getparam($params, 'grid_id', 'jqgrid'.++$this->guid);
00076 $pgr_id = $this->_getparam($params, 'pager_id', 'jqpager'.++$this->guid);
00077 $sch_id = $this->_getparam($params, 'search_id', 'jqsearch'.++$this->guid);
00078
00079 $html = '';
00080 $html .= '<div id="'.$sch_id.'" style="display:none"></div>';
00081 $html .= '<table id="'.$tbl_id.'" class="scroll" cellpadding="0" cellspacing="0"></table>';
00082 $html .= '<div id="'.$pgr_id.'" class="scroll" style="text-align:center"></div>';
00083
00084 $this->depends->html->js_load('jqgrid/jquery.jqGrid');
00085
00086 $this->depends->html->css_load('modal');
00087 $this->depends->html->js_load('jq/jquery.modal');
00088
00089 $js = 'var g = '.json_encode($grid).';';
00090
00091
00092 $js .= 'g.imgpath = "'.url('/img/jqgrid/basic').'";';
00093 $js .= 'g.pager = jQuery("#'.$pgr_id.'");';
00094
00095 $js .= 'jQuery("#'.$tbl_id.'").jqGrid(g)';
00096 $js .= '.navGrid("#'.$pgr_id.'", {edit:false, add:false, del:false})';
00097
00098 $js .= '.navButtonAdd("#'.$pgr_id.'", {caption:"Search", title:"Toggle Search", buttonimg:g.imgpath+"/find.gif", onClickButton:function(){ jQuery("#t_'.$tbl_id.'").show(); } })';
00099
00100
00101
00102 $js .= ';';
00103
00104
00105 $js .= 'jQuery("#t_'.$tbl_id.'").filterGrid("#'.$tbl_id.'", { gridModel:true, gridNames:false, gridToolbar:true, formtype:"horizontal", enableSearch:true, enableClear:false, autosearch:true, afterSearch:function() { jQuery(".HeaderButton").trigger("click"); jQuery("#'.$sch_id.'").css("display","none"); } } );';
00106 $js .= 'jQuery("#t_'.$tbl_id.'").hide();';
00107
00108
00109
00110
00111 $this->depends->html->js_run('', $js);
00112
00113
00114 $this->depends->html->css_load('jqgrid/basic');
00115
00116
00117 $html .= '<br><br><pre style="font-size:8pt" id="jsdbg"></pre>';
00118 $this->depends->html->js_load('beautify');
00119 $this->depends->html->js_run('', '$("#jsdbg").html(js_beautify(\''.str_replace("'", "\\'", $js).'\'));');
00120
00121
00122 return $html;
00123 }
00124
00130 function _optvar($src_arr, $src_key, &$dst_arr, $dst_key='')
00131 {
00132 if(empty($dst_key)) $dst_key = $src_key;
00133 if(isset($src_arr[$src_key])) $dst_arr[$dst_key] = $srt_arr[$src_key];
00134 }
00135
00140 function _getparam($params, $name, $default)
00141 {
00142 if(isset($params[$name])) {
00143 return $params[$name];
00144 }
00145 return $default;
00146 }
00147
00148 }
00149
00150 ?>