如何在使用 cakephp 1.3 创建的 Web 应用程序中实现 jqgrid v3.7

发布于 2024-09-08 03:41:31 字数 3688 浏览 4 评论 0原文

我正在尝试在使用创建的网络应用程序中实现 jqgrid v3.7 cakephp v1.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 未定义索引:sord 等等等等

有没有人在基于 cakephp 的应用程序中包含 jqgrid ?

如何将 jqgrid 包含在我的应用程序中? 请帮我做这件事。

谢谢

I am trying to implement jqgrid v3.7 in my webapplication created using cakephp v1.3.

My controller code is as follows

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);
}

the view file contains the following code

$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>

Now when I load the index action I get a lot of errors

Undefined index: rows
Undefined index: sidx
Undefined index: sord
etc. etc.

Has anybody included jqgrid in a cakephp based application ?

How do I include the jqgrid in my application ?
Please help me do this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花落人断肠 2024-09-15 03:41:31

您收到的错误看起来像 PHP 错误。尝试将 debug($this->params); 语句放在视图文件的顶部,以查看控制器输出的内容。

The errors you're getting look like PHP errors. Try putting a debug($this->params); statement at the top of your view file to see what the controller is spitting out.

长伴 2024-09-15 03:41:31

选区模型的查找函数中分配了一些不正确的值。

这是正确的完整工作代码:

这是控制器代码:

1.) 将网格显示的函数保留为空白,如下所示

function index()
{

}

2.) 然后创建一个新函数来显示包含如下数据的网格:

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();
}

这是查看代码:

1.) 包含必要的文件

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

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

2.) 在 VIEW 文件中添加以下 javascript 代码

<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.) 最后是同一个视图文件中的 HTML

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

如果您仍然遇到上述代码的任何问题,请告诉我。

There was some incorrect values assigned in the find function for constituency model.

here is the correct complete working code:

THIS IS THE CONTROLLER CODE:

1.) Leave the function in which the grid is shown a blank like this

function index()
{

}

2.) Then create a new function for showing the grid with data like this :

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();
}

THIS IS THE VIEW CODE:

1.) include the necessary files

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

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

2.) add the following javascript code in your VIEW file

<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.) And finally the HTML in the same view file

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

If you still face any problems with the code above let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文