Welcome Guest, Not a member yet? Register   Sign In
hi to all (about jqGrid + CI)
#1

[eluser]kikz4life[/eluser]
hi to all, just want some update on how everyone been doing in integrating jqGrid to CI.
hope i can start the discussion about CI again.

thanks
-Dean
#2

[eluser]attos[/eluser]
I use two views. One for the page itself and another with the grid contents.
In the controller I have two functions. One generates the page view and the other generates the grid cells.

These are snippets of a users grid. No model code and the code is not complete. You can figure out the missing pieces. (I cannot divulge all the source code).

In the controller:

Code:
/**
     * Retrieve the list of users
     */
    public function index()
    {
        $users = $this->user_model->get_all();
        $this->data['users'] = $users;
        $this->data['content_title'] = 'Users Management';
        this->load->view('users_view', $data);
    }

    public function user_list()
    {
        $users = $this->user_model->get_all();
        $this->load->view('user_list_xml', array('users'=>$users));
    }

The view for the cells (user_list_xml):
Code:
<php  if (!defined('BASEPATH')) exit('No direct script access allowed');
$cells = array('user_id', 'user_username', 'user_name', 'user_email');
view_xml_headers();
echo "<?xml version=\"1.0\" encoding=\"utf-8\" \x3F>\n";
?>
<rows>
    <page>1</page>
    <total>1</total>
    <records><php echo count($users)?></records>
    <php foreach($users as $user): ?>
    <row id="_row_<php echo $user['user_id'];?>">
        <php foreach($cells as $cell):?>
        <cell><![CDATA[<php echo $user[$cell];?>]]></cell>
        <php endforeach;?>
    </row>
    <php endforeach; ?>
</rows>

The page (users_view):
Code:
<script type="text/javascript">
    var reload = false;
    var mode = 'browse';
    var saved = false;
    var selected_user_id = null;
    // utility functions
    function set_selected_user_id(id) {
        selected_user_id = id;
        $('#selected_user_id_text').val(selected_user_id);
    }
    // Comms functions
    function loadError(data) {
        var message = data.responseText;
        showError(message);
    }
    // event handlers
    // BEGIN jQueryGrid stuff
    function clearLoadSelect(){
        var grid = $("#grid");
        grid.clearGridData();
        if (reload) {
            grid.trigger("reloadGrid");
        }
    }
    function gridSelectRow() {
        var grid = $("#grid");
        grid.resetSelection();
        if (selected_user_id != null) {
            grid.setSelection('_row_'+selected_user_id);
        }
    }
    function gridRowSelected(rowId) {
        var user_id = $("#grid").getCell(rowId, 'user_id');
        set_selected_user_id(user_id);
        loadUser(selected_user_id, 'get');
        $('#btn_edit').removeAttr("disabled");
    }
    function gridLoadComplete() {
        var grid = $("#grid");
        records = 1 + parseInt(grid.getGridParam("records"));
        grid.setGridParam({rowNum : records});
    }
    function gridInit(){
        $("#grid").jqGrid(
            { url: '<php echo view_generate_url('admin/users/user_list')?>'
            , datatype: 'xml'
            , mtype: 'POST'
            , colNames: ['Id', 'Username', 'Name', 'Email']
            , colModel:
                [ {name: 'user_id', index: 'user_id', hidden:true}
                , {name: 'user_username', index: 'user_username', width: 150}
                , {name: 'user_name', index: 'user_name', width: 350}
                , {name: 'user_email', index: 'user_email', width: 250}
                ]
            , rowNum: <php echo count($users)."\n"?>
            , sortname: 'user_username'
            , sortorder: 'desc'
            , viewrecords: true
            , caption: 'Users'
            , hidegrid: false
            , width: 750
            , height: 300
            // Events
            , onSelectRow: gridRowSelected
            , gridComplete: gridSelectRow
            , loadComplete: gridLoadComplete
            }
        );
        clearLoadSelect();
    }
   // END jQueryGrid stuff
    $(document).ready(function(){
        <php if (isset($debug)&&$debug) { ?>$('#debug_div').show();
        <php } else { ?>$('#debug_div').hide();<php } ?>
        $('#ajax_message').hide();
        $('#btn_close_message').click(function(){$('#ajax_message').hide()});
        $('#data_entry_panel').hide();
        $('#data_grid_panel').show();
        $('#selected_user_id_text').val(selected_user_id);

        gridInit();
    }
</script>
</head>
<body>
<div id="debug_div">
    <script type="text/javascript" language="javascript">
        //<![CDATA[
        function test_select() {
            set_selected_user_id($("#selected_user_id_text").val());
            gridSelectRow();
        }
        //]]>
    </script>
    <input type="button" id="btn_test_reload" value="Reload users" onclick="[removed]clearLoadSelect()" /><br />
    <label for="selected_user_id_text">Selected UserId:</label><input type="text" id="selected_user_id_text" /><br />
    <input type="button" id="btn_test_select" value="Select" onclick="[removed]test_select()" /><br />
</div>
<div id="data_grid_panel">
    <table id="grid"></table>
    <div id="grid_pager"></div>
    <br />
    <div id="grid_buttons_div">
        <input type="button" id="btn_edit" value="Edit User" />
        <input type="button" id="btn_new" value="Add a User" />
    </div>
</div>




Theme © iAndrew 2016 - Forum software by © MyBB