views:

278

answers:

1

I tried to make jqgrid work with codeigniter, but I could not do it, I only want to show the data from the table in json format... but nothing happens.. but i dont know what i am doing wrong, i cant see the table with the content i am calling.

my controller

class Grid extends Controller
{

  public function f()
  {

       $this->load->model('dbgrid');
       $var['grid'] =  $this->dbgrid->getcontentfromtable();

       foreach($var['grid'] as $row) {
        $responce->rows[$i]['id']=$row->id;
        $responce->rows[$i]['cell']=array($row->id,$row->id_catalogo);

       }
     $json = json_encode($responce);
       $this->load->view('vgrid',$json);



  } 


  function load_view_grid()
  {

    $this->load->view('vgrid');


  }


}

my model

class Dbgrid extends Model{

function getcontentfromtable()
{


  $sql = 'SELECT * FROM anuncios';
  $query = $this->db->query($sql);
  $result = $query->result();


  return $result;   
}

my view(script)

$(document).ready(function() { 
 jQuery("#list27").jqGrid({
        url:'http://localhost/sitio/index.php/grid/f',
        datatype: "json",
        mtype: "post",
        height: 255,
        width: 600,
        colNames:['ID','ID_CATALOGO'],
        colModel:[
            {name:'id',index:'id', width:65, sorttype:'int'},
            {name:'id_catalogo',index:'id_catalogo', sorttype:'int'}


        ],
        rowNum:50,
        rowTotal: 2000,
        rowList : [20,30,50],
        loadonce:true,
        rownumbers: true,
        rownumWidth: 40,
        gridview: true,
        pager: '#pager27',
        sortname: 'item_id',
        viewrecords: true,
        sortorder: "asc",
        caption: "Loading data from server at once" 
    });

}); 

hope someone help me

+2  A: 

The data produced by the server which you post in the comment

{"rows":{"":{"id":"11","cell":["11","225101503"]}}} 

have wrong format. The output should looks like

{ 
  "total": "xxx", 
  "page": "yyy", 
  "records": "zzz",
  "rows" : [
    {"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
    {"id" :"2", "cell":["cell21", "cell22", "cell23"]},
      ...
  ]
}

(see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data). So it should be at least like

{"rows":[{"id":"11","cell":["11","225101503"]}]}

In general if you define a jsonReader, you will be able to read almost any data. The data which you produce can be readed only by jsonReader defined with the functions (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#jsonreader_as_function and http://stackoverflow.com/questions/2835957/jquery-with-asp-net-mvc-calling-ajax-enabled-web-service/2836817#2836817). The simplest way for you will be to change your server code to produce the data in a standard formet (see above), which can be readed by the standard jsonReader.

And one more small remark. Using of sorttype has no effect for datatype: "json". Parameter sorttype works only with sorting of local data. In case of datatype: "json" the server will be responsible for correct data sorting. jqGrid send to the server only the name of column, which user choose for the data sorting.

Oleg
Hi Oleg, I can generate the correct format of the output, but I still cant see the result in the table This is what I get {"rows":[ {"id":"1","cell":["1","225101503"]}, {"id":"3","cell":["3","225101503"]}, {"id":"4","cell":["4","225101503"]}, {"id":"6","cell":["6","225101503"]}, {"id":"5","cell":["5","225101503"]}, {"id":"7","cell":["7","452101500"]}, {"id":"8","cell":["8","330000000"]}, {"id":"9","cell":["9","110101511"]}, {"id":"10","cell":["10","110101502"]}, {"id":"11","cell":["11","225101503"]} ]}I can see the table with the data, but outside the model(MVC).
Ivan
There are something important which you don't write. Your code and data are not perfect, they have no information about page, total and records, but all should work. I saved your json data in json.txt file and created a small html file with your JavaScript: ivan.htm. To make all easy I just replaced post to get. So try this one http://www.ok-soft-gmbh.com/jqGrid/ivan0.htm. It works. To fix problem with negative row numbers I set `rowNum:0` and we can see http://www.ok-soft-gmbh.com/jqGrid/ivan.htm. So you have your problem somewhere else. Compare my html code with your code as browser source.
Oleg
Hi Oleg, Thanks for your support. I found what was the problem and when I figured it out, I wanted to kill myself. I forgot to include a js file.And I have 'get' in mtype, because CI and jqGrid have different posting methods, CodeIgniter(POST) and jqGrid(GET).Thank You.
Ivan
You welmome! One say: "It is very difficult to find a black cat in a dark room. Espetially if the cat is not here." In software development we spend frequently a lot of our time for searching a wrong reason. And find out at the end a real reason of our problem somewhere else. At the end it's only important, that the problem is solved. One should try to be philosophical in such situations. Good luck!
Oleg