Showing posts with label jqgrid. Show all posts
Showing posts with label jqgrid. Show all posts

Monday, November 5, 2012

how to create dynamic jqgrid in asp.net mvc razor

How to make Dynamic jqgrid in asp.net mvc Razor

to create dynamic jqgrid  Dynamically

Step1: create Dyanmic column model from service and return as Json

example    

        DataTable dcm = new DataTable("colModel");

            dcm.Columns.Add("name");
            dcm.Columns.Add("index");
               for (int i = 0; i < mdt.Rows.Count; i++)
                    {
                        DataRow DR = dcm.NewRow();

                        DR[0] = mdt.Rows[i][0].ToString();
                        DR[1] = mdt.Rows[i][0].ToString();

                        dcm.Rows.Add(DR);
                    }

then conver dcm to json format

Step2:Create Jqgrid dynamic strucure using Ajax call

Example:


  <div id="did" style="width:100%"  align="center">
    <table id="empsummary" width="100%" border="0" cellspacing="0" cellpadding="0">
    </table>
    <div id="pager"></div>
    </div>
<script type="text/javascript" id="getdata">
//creating the dyanamic colmodel
   $("#empsummary").jqGrid('GridUnload');

        $.ajax(
    {
        type: "POST",
        url: '@Url.Action("gdata")',
        data: { tid: data },
        dataType: "json",
        success: function (result) {

            var obj = jQuery.parseJSON(result);
            colM = obj.DocumentElement.colModel;


            jQuery("#empsummary").jqGrid({
                colModel: colM,
//                width: 100%,
                caption: "EMPLOYEE SUMMARY",
                pager: jQuery('#pager'),
                rowNum: 3,
                rowList: [5, 10, 20, 50],
                viewrecords: true,
                gridComplete: function () {




                },
                loadComplete: function (data) { }

            })
        },
        error: function (x, e) {
            alert(x.readyState + " " + x.status + " " + e.msg);
        }
    });

setTimeout(function () { $("#empsummary").jqGrid('setGridParam', { datatype: 'json' }); }, 500);

//binding the data to  dyanamic colmodel


        $.ajax({

            url: '@Url.Action("empsummarygrid")',
            type: "POST",
            datatype: "json",
            data: { tid: data },

            async: false,
            success: function (response) {

                $('#empsummary').jqGrid('clearGridData');
                var obj = jQuery.parseJSON(response);

                var obs1 = obj.DocumentElement.et;


                var tcount = obj.DocumentElement.et.length;

                if (tcount > 0) {


                    for (var i = 0; i < obj.DocumentElement.et.length; i++) {

                        jQuery("#empsummary").jqGrid('addRowData', i + 1, obs1[i]);

                    }

                }
                else {
                    var obs11 = obj.DocumentElement.et[0];
                    jQuery("#empsummary").jqGrid('addRowData', i + 1, obs1);

                }

            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("Your Request is Failed at this time Please Try after some time");
            }
        });


</script>
       

Code:

Any problem With Dynamic Jqgrid Post Here

Saturday, November 3, 2012

how to change jqgrid row color

  how to change jqgrid row color

step1:create css stylesheet 

<style type="text/cs

 .myAltRowClass { background-color: #F08484; background-image: none; }
    </style>

Before jqgrid creation write the following function

   getColumnIndexByName = function (mygrid, columnName) {
        var cm = mygrid.jqGrid('getGridParam', 'colModel');
        for (var i = 0, l = cm.length; i < l; i++) {
            if (cm[i].name === columnName) {
                return i; // return the index
            }
        }
        return -1;
    };

Step2: on loadComplete: function () or gridcomplete function() wirite the following code


loadComplete: function () {
            var iCol = getColumnIndexByName($(this), 'del_columnname'),
                        cRows = this.rows.length, iRow, row, className;

            for (iRow = 0; iRow < cRows; iRow++) {
                row = this.rows[iRow];
                className = row.className;
   if ($.inArray('jqgrow', className.split(' ')) > 0) {                                                     
    var x = $(row.cells[iCol]).text();
                  
     if (x > 0) {
              if ($.inArray('myAltRowClass', className.split(' ')) === -1) {
                row.className = className + ' myAltRowClass';
                        }
                    }
                }
            }
        }



 Thats it!!!!

Another method :

on afterInsertRow: function () wirite the following code

 

afterInsertRow: function (rowid, rowdata) {

            var d = rowdata.del;

           

            if (parseInt(d) > 0) {
                $(this).jqGrid('setRowData', rowid, false, 'ui-state-error');


                $("#jqgrid").jqGrid('setRowData', rowid, false, 'StyleCss');

      }

        },

Friday, October 26, 2012

How To Show DropDownList in JQGRID ASP.Net MVC Razor

How To Show DropDownList in JQGRID ASP.Net MVC Razor

here is the solution: 


jQuery("#jqgriddemo").jqGrid({
        datatype: 'local',
        colNames: ['name','age'],
        colModel: [{ name: name, index: name, width: "16%", align: "center" }, 
                  { name: 'age', index: 'age', width: "12%", align: "center",
                 editable:true, viewable: true, edittype: 'select', editoptions:
                    { value: "1:1;2:2;3:3;4:4;5:5;6:6;7:7;8:8;9:9;10:10"} },
                         ],
       rowNum: 5,
        rowList: [5, 10, 15],
        viewrecords: true,
        pager: '#pager2',
        sortname: 'Score_Type',
        editurl: '/urladress',
        forceFit: true,
        cellEdit: true,
        cellsubmit: 'clientArray',
   afterSaveCell: function (rowid, name, val, iRow, iCol) {
  var  age = jQuery("#jqgriddemo").jqGrid('getCell', rowid, iCol);

}
  }).navGrid('#pager2', { edit: false, add: false, del: false });