Welcome Guest, Not a member yet? Register   Sign In
Jqgrid Problem
#1

[eluser]phpserver[/eluser]
I'm in a situation where i am needed to choose a table(will be a form with a select box) and display the data on an editable grid.Let me explain that in code:Here is my jqgrid code,notice how we select all the data from the table 'stetbl':

Grid.php - Controller

Code:
function ciGridTest()
{
    $data['title'] = 'jQGrid Codeigniter Integration Demo'; //Page Title
    $data['main'] = 'jqgcid';    //view script file name i.e. jqgcid.php in system/application/view folder
    $this->load->vars($data);
    $this->load->view('template1');    //the default template you are going to use
}

function gridServerPart()
{
    $page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
    $limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid
    $sidx = isset($_POST['sidx'])?$_POST['sidx']:'state'; // get index row - i.e. user click to sort
    $sord = isset($_POST['sord'])?$_POST['sord']:''; // get the direction

    if(!$sidx) $sidx =1;

    $SQL = "SELECT * FROM `stetbl`";
        
    $result = $this->DB_Func->executeCustomQuery( $SQL );

    $count = count($result);

    if( $count > 0 ) {
        $total_pages = ceil($count/$limit);    //calculating total number of pages
    } else {
        $total_pages = 0;
    }
    if ($page > $total_pages) $page=$total_pages;

    $start = $limit*$page - $limit; // do not put $limit*($page - 1)
    $start = ($start<0)?0:$start;  // make sure that $start is not a negative value

    $SQL = "SELECT * FROM `stetbl` ORDER BY {$sidx} {$sord} LIMIT {$start}, {$limit}";
    $result = $this->DB_Func->executeCustomQuery( $SQL );  //here DB_Func is a model handling DB interaction

    if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
        header("Content-type: application/xhtml+xml;charset=utf-8");
    } else {
        header("Content-type: text/xml;charset=utf-8");
    }
    $et = ">";

    echo "&lt;?xml version='1.0' encoding='utf-8'?$et\n";
    echo "<rows>";
    echo "<page>".$page."</page>";
    echo "<total>".$total_pages."</total>";
    echo "<records>".$count."</records>";
    // be sure to put text data in CDATA
    foreach($result as $row) {
        echo "<row id='".$row['state']."'>";
        echo "<cell>&lt;![CDATA[".$row['state']."]]></cell>";
        echo "<cell>&lt;![CDATA[".$row['meaning']."]]></cell>";
        echo "</row>";
    }
    echo "</rows>";
}

jqgcid.php - The View

Code:
&lt;?
    $ci =& get_instance();
    $base_url = base_url();
?&gt;
<table id="list1"></table> &lt;!--Grid table--&gt;
<div id="pager1"></div>  &lt;!--pagination div--&gt;

[removed]
jQuery().ready(function (){
jQuery("#list1").jqGrid({
       url:'&lt;?=$base_url.'gridServerPart'?&gt;',      //another controller function for generating XML data
    mtype : "post",             //Ajax request type. It also could be GET
    datatype: "xml",            //supported formats XML, JSON or Arrray
       colNames:['State','Meaning'],       //Grid column headings
       colModel:[
           {name:'state',index:'state', width:20, align:"left"},
           {name:'meaning',index:'meaning', width:55, align:"left"},
      ],
       rowNum:10,
       width: 450,
    height: 150,
       rowList:[10,20,30],
       pager: '#pager1',
       sortname: 'state',
    viewrecords: true,
    caption:"jQGrid with Codeigniter"
}).navGrid('#pager1',{edit:false,add:false,del:false});
});
[removed]

Finally,template1.php - View

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
        &lt;link href="&lt;?=base_url();?&gt;css/ui.all.css" rel="stylesheet" type="text/css" /&gt;
        &lt;link href="&lt;?=base_url();?&gt;css/ui.jqgrid.css" rel="stylesheet" type="text/css" /&gt;
        &lt;link href="&lt;?=base_url();?&gt;css/redmond/jquery-ui-1.7.1.custom.css" rel="stylesheet" type="text/css" /&gt;
        &lt;link href="&lt;?=base_url();?&gt;css/ui.multiselect.css" rel="stylesheet" type="text/css" /&gt;

[removed]
    // &lt;![CDATA[
        base_url = '&lt;?=base_url()?&gt;';
    //]]>
[removed]
        [removed][removed]
        [removed][removed]
        [removed][removed]
        [removed][removed]
    &lt;/head&gt;
    &lt;body&gt;
        &lt;?php $this->load->view($main);?&gt;
    &lt;/body&gt;
&lt;/html&gt;

I have tried using post values and it worked but did not work when i refreshed the page.I am thinking of disabling the refresh button when the grid is rendered but i am yet to know how to go about it.If you have ideas on how i can sort my problem,that'wld be nice.




Theme © iAndrew 2016 - Forum software by © MyBB