2010-07-01 23 views
0

我試圖在我使用cakephp v1.3創建的web應用程序中實現jqgrid v3.7如何在使用cakephp 1.3創建的web應用程序中實現jqgrid v3.7 1.3

我的控制器代碼如下

function admin_index() 
{ 
    // get how many rows we want to have into the grid - rowNum parameter in the grid 
    $limit = $this->params['url']['rows']; 

    // get index row - i.e. user click to sort. At first time sortname parameter - 
    // after that the index from colModel 
    $sidx = $this->params['url']['sidx']; 

    // sorting order - at first time sortorder 
    $sord = $this->params['url']['sord']; 

    // if we not pass at first time index use the first column for the index or what you want 
    if(!$sidx) $sidx = 1; 

    // calculate the number of rows for the query. We need this for paging the result 
    $row = $this->Constituency->find('count'); 
    $count = $row; 

    // calculate the total pages for the query 
    if($count > 0) 
    { 
     $total_pages = ceil($count/$limit); 
    } 
    else 
    { 
     $total_pages = 0; 
    } 

    // if for some reasons the requested page is greater than the total 
    // set the requested page to total page 
    if($page > $total_pages) $page = $total_pages; 

    // calculate the starting position of the rows 
    $start = $limit * $page - $limit; 

    // if for some reasons start position is negative set it to 0 
    // typical case is that the user type 0 for the requested page 
    if($start < 0) $start = 0; 

    // the actual query for the grid data 
    $limit_range = $start . "," . $limit; 
    $sort_range = $sidx . " " . $sord; 
    //$result = $this->Constituency->findAll(null, "id,name", $sort_range, $limit_range, 1, null); 
    $this->Constituency->recursive = -1; 
    $result = $this->Constituency->find('all', array(
     'fields' => array('id', 'name'), 
     'order' => $sidx, 
     'limit' => $start .",". $limit_range 
    )); 

    $i=0; 
    $response->page = $page; 
    $response->total = $total_pages; 
    $response->records = $count; 

    foreach($result as $result) 
    { 
     $response->rows[$i]['id'] = $result['Constituency']['id']; 
     $responce->rows[$i]['cell']=array($result['Constituency']['id'],$result['Constituency']['name']); 
     $i++; 
    } 

    echo json_encode($response); 
} 

視圖文件包含以下代碼

$this->Html->css('ui.jqgrid'); 
$this->Html->script('jquery.jqGrid.min'); 

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#list").jqGrid(
     { 
      url:'<?php echo $this->Html->url(array("controller" => "constituencies", "action" => "index")); ?>', 
      datatype: "json", 
      colNames:['Id','Name'], 
      colModel:[ 
       {name:'id',index:'id', width:55}, 
       {name:'name',index:'name', width:90}, 
      ], 
      rowNum:10, 
      rowList:[10,20,30], 
      pager: jQuery('#pager'), 
      sortname: 'id', 
      viewrecords: true, 
      sortorder: "desc", 
      caption:"Constituencies" 
     }); 
     $("#list").navGrid("#pager",{edit:false,add:false,del:false}); 
    }) 
</script> 

<div class="constituencies index"> 
    <h2><?php __('Constituencies'); ?></h2> 
    <table id="list" class="scroll"></table> 
    <div id="pager" class="scroll" ></div> 
</div> 

現在,當我加載索引動作我得到了很多錯誤的

未定義索引:行 未定義索引:sidx 未定義索引:索引 等。

有沒有人在基於cakephp的應用程序中包含jqgrid?

如何在我的應用程序中包含jqgrid? 請幫我做這個。

感謝

回答

1

在選區模型的find函數中存在一些不正確的值。

這裏是正確的完整的工作代碼:


這是控制器CODE:


1)留在其中的格示出了坯件這樣

功能
function index() 
{ 

} 


2.)Th烯創建一個新的功能,用於表示網格數據是這樣的:

function admin_showGrid() 
{ 
    $this->autoRender = false; 

    // get how many rows we want to have into the grid - rowNum parameter in the grid 
    $limit = $this->params['url']['rows']; 

    // get index row - i.e. user click to sort. At first time sortname parameter - 
    // after that the index from colModel 
    $sidx = $this->params['url']['sidx']; 

    // sorting order - at first time sortorder 
    $sord = $this->params['url']['sord']; 

    $page = $this->params['url']['page']; 

    // if we not pass at first time index use the first column for the index or what you want 
    if(!$sidx) $sidx = 1; 

    // calculate the number of rows for the query. We need this for paging the result 
    $row = $this->{Model_Name}->find('count'); 
    $count = $row; 

    // calculate the total pages for the query 
    if($count > 0) 
    { 
     $total_pages = ceil($count/$limit); 
    } 
    else 
    { 
     $total_pages = 0; 
    } 

    // if for some reasons the requested page is greater than the total 
    // set the requested page to total page 
    if($page > $total_pages) $page = $total_pages; 

    // calculate the starting position of the rows 
    $start = $limit * $page - $limit; 

    // if for some reasons start position is negative set it to 0 
    // typical case is that the user type 0 for the requested page 
    if($start < 0) $start = 0; 

    // the actual query for the grid data 
    $limit_range = $start . "," . $limit; 
    $sort_range = $sidx . " " . $sord; 
    //$result = $this->{Model_Name}->findAll(null, "id,name", $sort_range, $limit_range, 1, null); 
    $this->{Model_Name}->recursive = -1; 
    $result = $this->{Model_Name}->find('all', array(
     'fields' => array('id', 'name'), 
     'order' => $sort_range, 
     'limit' => $limit_range 
    )); 

    $i = 0; 
    $response->page = $page; 
    $response->total = $total_pages; 
    $response->records = $count; 

    foreach($result as $result) 
    { 
     $response->rows[$i]['id'] = $result['{Model_Name}']['id']; 
     $response->rows[$i]['cell'] = array($result['{Model_Name}']['id'], $result['{Model_Name}']['name']); 
     $i++; 
    } 

    echo json_encode($response); 

    //writing exit() is necessary. 
    exit(); 
} 




這是視圖CODE:


1)包括必要的文件

echo $this->Html->css('ui.jqgrid'); 

echo $this->Html->script('grid.locale-en'); 
echo $this->Html->script('jquery.jqGrid.min'); 

, 2。)在您的VIEW文件

<script type="text/javascript"> 
jQuery(document).ready(function(){ 
    jQuery("#list").jqGrid(
    /* '#list' is the ID of the table in which you want populated results */ 
    { 
     url:'<?php echo $this->Html->url(array("controller" => "{Controller_Name}", "action" => "{Action_Name}")); ?>', 
     datatype: "json", 
     mtype: "GET", 
     colNames:['Id','Name'], 
     colModel:[ 
      {name:'id',index:'id', width:55}, 
      {name:'name',index:'name', width:90}, 
     ], 
     rowNum:10, 
     rowList:[10,20,30], 
     pager: jQuery('#pager'), /* id of the pagination element */ 
     sortname: 'id', 
     viewrecords: true, 
     sortorder: "asc", 
     caption:"Enter table Heading or the name you want to show for the table", 
     height:"auto", 
     autowidth: true 
    }); 
    jQuery("#list").navGrid("#pager",{edit:false,add:false,del:false}); 
}) 
</script> 


3.在同一視圖文件中添加下面的JavaScript代碼),最後HTML

<table id="list" style="height:auto;"></table> 
<div id="pager"></div> 


如果你仍然面臨的任何問題上面的代碼讓我知道。

+0

複製/粘貼使用Cake 2.3.1,jquery 1.9和jqgird 4.4.5,它仍然工作很好 – user2004338 2013-04-18 19:10:10

1

你得到的錯誤看起來像PHP錯誤。嘗試將debug($this->params);聲明放在視圖文件的頂部,以查看控制器吐出的內容。

+0

已經做到了。數據即將到來。謝謝你的幫助。 – 2010-07-02 07:54:45