在Codeigniter中{J 44grid中显示动态数据

时间:2017-07-05 09:50:15

标签: javascript php jquery json codeigniter

我正在开发CodeIgniter项目。在项目中我使用的是JQgrid表。我想使用JQgrid显示数据。我从数据库中获取数据,但是我无法在JQgrid中显示数据,但我可以在TextArea中显示数据。

模型文件

public function getmodeldata()
{
    $query=$this->db->get('model');
    $data = json_encode($query->result());
    return $data;
}

控制器文件

public function model()
{
  $data['brand']=$this->Admin_model->getbranddata();
  $data['getmodel']=$this->Admin_model->getmodeldata();
  $this->load->view('layouts/header');
  $this->load->view('Admin/model',$data);
  $this->load->view('layouts/footer');
}

查看文件

<textarea id="data"><?php echo $getmodel?></textarea>

<script src="assets/js/jquery-2.1.4.min.js"></script>
<script src="assets/js/jquery.jqGrid.min.js"></script>
<script src="assets/js/grid.locale-en.js"></script>
<script type="text/javascript">
    var grid_data = jQuery('#data').val();

    var subgrid_data = [];
    jQuery(function($) {
        var grid_selector = "#grid-table";
        var pager_selector = "#grid-pager";


        var parent_column = $(grid_selector).closest('[class*="col-"]');
        $(window).on('resize.jqGrid', function () {
            $(grid_selector).jqGrid( 'setGridWidth', parent_column.width() );
    })

        $(document).on('settings.ace.jqGrid' , function(ev, event_name, collapsed) {
            if( event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed' ) {
                //setTimeout is for webkit only to give time for DOM changes and then redraw!!!
                setTimeout(function() {
                    $(grid_selector).jqGrid( 'setGridWidth', parent_column.width() );
                }, 20);
            }
        })

        jQuery(grid_selector).jqGrid({
            subGrid : false,
            subGridOptions : {
                plusicon : "ace-icon fa fa-plus center bigger-110 blue",
                minusicon  : "ace-icon fa fa-minus center bigger-110 blue",
                openicon : "ace-icon fa fa-chevron-right center orange"
            },
            subGridRowExpanded: function (subgridDivId, rowId) {
                var subgridTableId = subgridDivId + "_t";
                $("#" + subgridDivId).html("<table id='" + subgridTableId + "'></table>");
                $("#" + subgridTableId).jqGrid({
                    datatype: 'local',
                    data: subgrid_data,
                    colNames: ['No','Item Name','Qty'],
                    colModel: [
                        { name: 'id', width: 50 },
                        { name: 'name', width: 150 },
                        { name: 'qty', width: 50 }
                    ]
                });
            },
            data: grid_data,
            datatype: "json",
            height: 250,
            colNames:['Action', 'ID','Name', 'Created Date'],
            colModel:[
                {name:'myac',index:'', width:80, fixed:true, sortable:false, resize:false,
                    formatter:'actions', 
                    formatoptions:{ 
                        keys:true,
                        delOptions:{recreateForm: true, beforeShowForm:beforeDeleteCallback},
                    }
                },
                {name:'id',index:'id', width:60, sorttype:"int", editable: false},
                {name:'model_name',index:'model_name', width:150,editable: true,editoptions:{size:"20",maxlength:"30"}},
                {name:'created_at',index:'created_at', width:150, sortable:false,editable: false} 
            ], 

            viewrecords : true,
            rowNum:10,
            rowList:[10,20,30],
            pager : pager_selector,
            altRows: true,      
            multiselect: true,
            multiboxonly: true,

            loadComplete : function() {
                var table = this;
                setTimeout(function(){
                    styleCheckbox(table);

                    updateActionIcons(table);
                    updatePagerIcons(table);
                    enableTooltips(table);
                }, 0);
            },

            editurl: "./dummy.php",
            caption: "jqGrid with inline editing"

        });
        $(window).triggerHandler('resize.jqGrid');//trigger window resize to make the grid get the correct size


</script>

1 个答案:

答案 0 :(得分:0)

如果您的数据与json字符串一样,则需要替换

data: grid_data,
datatype: "json",

datastr: grid_data,
datatype: "jsonstring",