Advertisement
dropbox1349

flexigrid_helper.php

Mar 10th, 2015
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.13 KB | None | 0 0
  1. <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3.  * Flexigrid CodeIgniter implementation
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @category  CodeIgniter
  8.  * @package   Flexigrid CI
  9.  * @author    Frederico Carvalho (frederico@eyeviewdesign.com)
  10.  * @version   0.3
  11.  * Copyright (c) 2008 Frederico Carvalho  (http://flexigrid.eyeviewdesign.com)
  12.  * Dual licensed under the MIT (MIT-LICENSE.txt)
  13.  * and GPL (GPL-LICENSE.txt) licenses.
  14. */
  15. if (! function_exists('build_grid_js'))
  16. {
  17.     /**
  18.      * Build Javascript to display grid
  19.      *
  20.      * @param   grid id, or the div id
  21.      * @param   url to make the ajax call
  22.      * @param   array with colModel info:        
  23.      *          * 0 - display name
  24.      *          * 1 - width
  25.      *          * 2 - sortable
  26.      *          * 3 - align
  27.      *          * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
  28.      *          * 5 - hidden (TRUE or FALSE, default is FALSE. This index is optional.)
  29.      * @param   array with button info:    
  30.      *          * 0 - display name
  31.      *          * 1 - bclass
  32.      *          * 2 - onpress
  33.      * @param   default sort column name
  34.      * @param   default sort order
  35.      * @param   array with aditional parameters
  36.      * @return  string
  37.      */
  38.     function build_grid_js($grid_id,$url,$colModel,$sortname,$sortorder,$gridParams = NULL,$buttons = NULL)
  39.     {
  40.         //Basic propreties
  41.         $grid_js = '<script type="text/javascript">$(document).ready(function(){';
  42.         $grid_js .= '$("#'.$grid_id.'").flexigrid({';
  43.         $grid_js .= "url: '".$url."',";
  44.         $grid_js .= "dataType: 'json',";
  45.         $grid_js .= "sortname: '".$sortname."',";
  46.         $grid_js .= "sortorder: '".$sortorder."',";
  47.         $grid_js .= "usepager: true,";
  48.         $grid_js .= "useRp: true,";
  49.        
  50.         //Other propreties
  51.         if (is_array($gridParams))
  52.         {
  53.             //String exceptions that dont have ' '. Must be lower case.
  54.             $string_exceptions = array("rpoptions");
  55.            
  56.             //Print propreties
  57.             foreach ($gridParams as $index => $value) {
  58.                 //Check and print with or without ' '
  59.                 if (is_numeric($value)) {
  60.                     $grid_js .= $index.": ".$value.",";
  61.                 }
  62.                 else
  63.                 {
  64.                     if (is_bool($value))
  65.                         if ($value == true)
  66.                             $grid_js .= $index.": true,";
  67.                         else
  68.                             $grid_js .= $index.": false,";
  69.                     else
  70.                         if (in_array(strtolower($index),$string_exceptions))
  71.                             $grid_js .= $index.": ".$value.",";
  72.                         else
  73.                             $grid_js .= $index.": '".$value."',";
  74.                 }
  75.             }
  76.         }
  77.        
  78.         $grid_js .= "colModel : [";
  79.        
  80.         //Get colModel
  81.         foreach ($colModel as $index => $value) {
  82.             $grid_js .= "{display: '".$value[0]."', ".($value[2] ? "name : '".$index."', sortable: true," : "")." width : ".$value[1].", align: '".$value[3]."'".(isset($value[5]) && $value[5] ? ", hide : true" : "")."},";  
  83.            
  84.             //If item is searchable
  85.             if ($value[4] != 0)
  86.             {
  87.                 //Start searchitems var
  88.                 if (!isset($searchitems))
  89.                     $searchitems = "searchitems : [";
  90.                
  91.                 $options = '';             
  92.                 if (isset($value['options'])) {
  93.                     if (isset($value['options']['type'])) {
  94.                         $options = ", type : '".$value['options']['type']."'";
  95.                         switch($value['options']['type']) {
  96.                             case 'select'   : $options .= ", editoptions : { value : '" . $value['options']['edit_options'] . "' }"; break;
  97.                             case 'date'     :
  98.                             case 'radio'    :
  99.                             case 'checkbox' :
  100.                             default         :
  101.                         }
  102.                     }
  103.                 }
  104.                
  105.                 if ($value[4] == 2)
  106.                     $searchitems .= "{display: '".$value[0]."', name : '".$index."', isdefault: true " .$options. "},";
  107.                 else if ($value[4] == 1)
  108.                     $searchitems .= "{display: '".$value[0]."', name : '".$index."' " .$options. "},";
  109.             }
  110.                
  111.         }
  112.         //Remove the last ","
  113.         $grid_js = substr($grid_js,0,-1).'],';
  114.         $searchitems = substr($searchitems,0,-1).']';
  115.        
  116.         //Add searchitems to grid
  117.         $grid_js .= $searchitems;
  118.  
  119.         //Get buttons
  120.         if (is_array($buttons))
  121.         {
  122.             $grid_js .= ",buttons : [";
  123.             foreach ($buttons as $index => $value) {
  124.                 if ($value[0] == 'separator')
  125.                     $grid_js .= "{separator: true},";
  126.                 else
  127.                     $grid_js .= "{name: '".$value[0]."', bclass : '".$value[1]."', onpress : ".$value[2]."},";
  128.             }
  129.             //Remove the last ","
  130.             $grid_js = substr($grid_js,0,-1).']';
  131.         }
  132.        
  133.         //Finalize
  134.         $grid_js .= "}); })</script>";
  135.        
  136.         return $grid_js;
  137.     }
  138. }
  139.  
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement