0

I was using jqGrid 4.5.3. version which I have upgraded to free jqGrid version 4.13.6. After upgrading I am facing following issues

  1. Sorting is not working for all columns
  2. I want to display the column size as per the content for which I have set autoResizable: true and autoresizeOnLoad: true, still column width is not set as per content

Below is my jqGrid code written on document.Ready function

jQuery("#jqgrid").jqGrid({
            url: '@Url.Action("GetClassList", "Class")',
            datatype: 'json',
            height: 'auto',
            colNames: ['ClassID','CourseID', '@objLocalizer["Class_Title_GridCol"]','@objLocalizer["ViewCourseDetails"]','@objLocalizer["ViewClassSchedule"]', '@objLocalizer["Class_AssignUser"]','@objLocalizer["ClassCreatedBy"]','@objLocalizer["ClassCreatedDate"]'],
            colModel: [

                 {
                     name: 'Class.ClassID',
                     index: 'Class.ClassID',
                     sortable: true,
                     hidden: true
                 },
                 {
                     name: 'Class.CourseID',
                     index: 'Class.CourseID',
                     sortable: true,
                     hidden: true
                 },
                 {
                     name: 'ClassLang.Title',
                     index: 'ClassLang.Title',
                     sortable: true,
                     formatter: addLink,

                 },
                 {
                     name: 'CourseDetails',
                     index: 'CourseDetails',
                     sortable: false,
                     align: 'center',
                     title: false,
                     formatter:AddCourseDetailsLink
                 },
                 {
                     name: 'ClassSchedule',
                     index: 'ClassSchedule',
                     sortable: false,
                     align: 'center',
                     title: false,
                     formatter:AddViewClassScheduleLink
                 },
                 {
                     name: 'AssignUser',
                     index: 'AssignUser',
                     sortable: false,
                     align: 'center',
                     title: false,
                     formatter: AddAssignUserLink
                 },
                 {
                     name: 'UserName',
                     index: 'UserName',
                     align: 'center',
                     sortable: true
                 },
                 {
                     name: 'Class.WhenCreated',
                     index: 'Class.WhenCreated',
                     align: 'center',
                     formatter:'date',
                     sortable: true
                 }],

            rowNum: 10,
            rowList: [10, 20, 30],
            pager: '#pjqgrid',
            sortname: "Title",
            sortorder: "desc",
            toolbarfilter: true,
            viewrecords: true,
            multiSort: true,
            sortable: true,
            loadonce: true,
            ignoreCase: true,
            gridComplete: function () {

                $("#progbar").css('width', '100%')
                $("#progess").hide();
                $("#grid").css("visibility", 'visible');

            },
            editurl: " ",
            caption: "",
            multiselect: false,
            autowidth: true,

        });

I have also added the below function to expand the grid as per the available tabs

    $(window).on('resize.jqGrid', function () {
        $("#jqgrid").jqGrid('setGridWidth', $("#content").width());
    })

HTML Markup

<div class="well well-sm well-light" id="content">
    <div id='divResult'>
        @{
            await Html.RenderPartialAsync("~/ViewsBackend/Class/_ClassTab.cshtml");
        }
    </div>

    <section id="widget-grid" class="">
        <div class="row">
            <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <div id="grid">
                    <table id="jqgrid"></table>
                    <div id="pjqgrid"></div>
                    <br>
                </div>
                <br>
            </article>
        </div>
    </section>
</div>

Edit 1 :- Custom formatters

function addLink(cellvalue, options, rowObject) {

            return "<a href='#' style='height:25px;width:120px;' type='button'  onclick=CallActionMethod(" + "'" + rowObject.Class.ClassID + "'" + ")>" + rowObject.ClassLang.Title + "</a>";

    }

    function AddAssignUserLink(cellvalue, options, rowObject){

            return "<button class='btn btn-xs btn-default' data-placement='center' title='@objLocalizer["Class_AssignUser"]' onclick='AssignUser(" + rowObject.Class.ClassID + ")'\"><i class='fa fa-users fa-lg' aria-hidden='true'></i></a>";
    }

    function AddCourseDetailsLink(cellvalue, options, rowObject){

            return "<button class='btn btn-xs btn-default' data-placement='center' title='@objLocalizer["ViewCourseDetails"]' onclick='CourseDetails(" + rowObject.Class.CourseID + ")'\"><i class='fa fa-file fa-lg' aria-hidden='true'></i></a>";

    }

    function AddViewClassScheduleLink(cellvalue, options, rowObject){

            return "<button class='btn btn-xs btn-default' data-placement='center' title='@objLocalizer["ViewClassSchedule"]' onclick='ViewClassSchedule(" + rowObject.Class.ClassID + ")'\"><i class='fa fa-calendar fa-lg' aria-hidden='true'></i></a>";

    }

These methods are called from custom formatters

 function CallActionMethod(cellvalue) {
        $.ajax({

            type: "POST",
            url: '@Url.Action("SetClassDetailsClassID", "Class")',
            data: { cellvalue: cellvalue },
            success: function (data) {
                document.location.href = "@Url.Action("ClassDetails", "Class")";
            }
        });
    }

    function ViewClassSchedule(ClassID){

        $.ajax({

            type: "POST",
            url: '@Url.Action("SetClassDetailsClassID", "Class")',
            data: { cellvalue: ClassID },
            success: function (data) {
                document.location.href = "@Url.Action("ClassSchedule", "Class")";
            }
        });
    }

    function AssignUser(ClassID) {

        $.ajax({
            type: "POST",
            url: '@Url.Action("AssignUser", "Class")',
            data: { classid: ClassID },
            success: function (data) {

                jQuery(".modal-content").html('');
                jQuery(".modal-content").html(data);
                jQuery("#AssignUserModal").modal('show');

            }
        });
    }

    function CourseDetails(CourseID)
    {

        $.ajax({

            type: "POST",
            url: '@Url.Action("setcourseid", "Course")',
            data: { cellvalue: CourseID },
            success: function (data) {

                document.location.href = "@Url.Action("CourseDetails", "Course")";
            }
        });

    }

Screenshot of jqGrid with updated version

enter image description here Any help on this appreciated !

XamDev
  • 2,435
  • 8
  • 44
  • 75
  • Refer this http://stackoverflow.com/questions/3906300/jqgrid-and-the-autowidth-option-how-does-it-work – Ghanshyam Baravaliya Feb 14 '17 at 05:11
  • Could you include the code of custom formatters (`addLink`, `AddCourseDetailsLink`, `AddViewClassScheduleLink`, `AddAssignUserLink`), which you use in your code? – Oleg Feb 14 '17 at 08:31

1 Answers1

1

You can fix your code to the following

$(window).on("resize", maximizeGrid);
$grid.on("jqGridAfterLoadComplete", function () {
    var colModel = $(this).jqGrid("getGridParam", "colModel"), i, cm;

    // reset widthOrg to the new values after autoResizeAllColumns
    for (i = 0; i < colModel.length; i++) {
        cm = colModel[i];
        cm.widthOrg = cm.width;
    }
    maximizeGrid();
});

$grid.jqGrid({
    datatype: "json",
    url: "/echo/json/",
    mtype: "POST",
    postData: {
        json: JSON.stringify(serverResponse)
    },
    colModel: [
        {
            name: 'ClassID',
            key: true,
            jsonmap: 'Class.ClassID',
            hidden: true
        },
        {
            name: 'Title',
            formatter: addLink,
            jsonmap: 'ClassLang.Title'
        },
        {
            name: 'CourseDetails',
            sortable: false,
            align: 'center',
            formatter:AddCourseDetailsLink,
            title: false
        },
        {
            name: 'ClassSchedule',
            sortable: false,
            align: 'center',
            formatter:AddViewClassScheduleLink,
            title: false
        },
        {
            name: 'AssignUser',
            sortable: false,
            align: 'center',
            formatter: AddAssignUserLink,
            title: false
        },
        {
            name: 'UserName',
            align: 'center'
        },
        {
            name: 'WhenCreated',
            jsonmap: 'Class.WhenCreated',
            align: 'center',
            formatter:'date'
        }
    ],
    iconSet: "fontAwesome",
    rowNum: 10,
    rowList: [10, 20, 30],
    pager: true,
    sortname: "Title",
    sortorder: "desc",
    viewrecords: true,
    multiSort: true,
    sortable: true,
    loadonce: true,
    additionalProperties: ['Class', 'ClassLang'],
    autoencode: true,
    cmTemplate: {
        autoResizable: true
    },
    autoresizeOnLoad: true,
    autowidth: true,
    autoResizing: {
        //resetWidthOrg: true,
        compact: true
    }
});

See the demo https://jsfiddle.net/OlegKi/b15pmdcg/4/. You can read more details More details about widthOrg in the issue. The same issue explains new resetWidthOrg: true property of autoResizing.

I'd recommend you to consider to use custom buttons of the formatter: "actions" (see the wiki article for details)

{
    name: "act", label: "Details", template: "actions", width: 70,
    formatoptions: { editbutton: false, delbutton: false }
}

and the option

actionsNavOptions: {
    courseDetailsicon: "fa-file",
    courseDetailstitle: "show course details",
    classScheduleicon: "fa-calendar",
    classScheduletitle: "class schedule",
    assignUsericon: "fa-users",
    assignUsertitle: "Assign user to class",
    custom: [
        { action: "courseDetails", position: "first",
            onClick: function (options) {
                alert("Course Details, rowid=" + options.rowid);
            } },
        { action: "classSchedule", position: "first",
            onClick: function (options) {
                alert("Class Schedule, rowid=" + options.rowid);
            } },
        { action: "assignUser",
            onClick: function (options) {
                alert("Assign User, rowid=" + options.rowid);
            } }
    ]
}

One can see the results on another demo https://jsfiddle.net/OlegKi/rmsz529L/3/

By the way, you can use the same demos with Boostrap CSS instead of jQuery UI CSS. You will need just add guiStyle: "bootstrap" option of jqGrid:

https://jsfiddle.net/OlegKi/b15pmdcg/8/

https://jsfiddle.net/OlegKi/rmsz529L/2/

Oleg
  • 217,934
  • 30
  • 386
  • 757
  • Thanks for the response. Please allow me some time to test it and will let you know for any queries ! – XamDev Feb 14 '17 at 11:39
  • Thanks for the help ! that is working as per my expectations, only I am some of the issues like Enable sorting for multiple columns, I think this applied automatically but it is not sorting the data. After few clicks(3 clicks precisely, the up-down arrows vanishes). Also, I have added `guiStyle: "bootstrap"` after which ONLY pagination controls are getting disturbed(not sure it is conflicting with other CSS, as I am using ready made template). On previous version of jqGrid I was having Search,Refresh buttons and Add Button(Custom button, which we have added). So how can we add those ? – XamDev Feb 15 '17 at 07:17
  • I have update the question with screenshot of jqGrid with latest version. – XamDev Feb 15 '17 at 07:25
  • @Rohit: You are welcome! I see currently some errors on the CDN cdnjs. Some files will be not loaded. I hope the problem will be fixed very soon. You can open console of developer tools and verify that no errors exist during loading from cdnjs.cloudflare.com. If it's not the problem then you should describe exactly the problem, which you have: which demo should I open and what steps I should do to reproduce the problem, which you describe. – Oleg Feb 15 '17 at 07:25
  • I am not using the CDN, I have copied ONLY 2 files from your fork( `jquery.jqgrid.min.js & ui.jqgrid.min.css`) in my project. Still for sorting issue you can open the demo https://jsfiddle.net/OlegKi/b15pmdcg/4/, where you can apply the sorting on 'Date' column and check – XamDev Feb 15 '17 at 07:33
  • 1
    @Rohit: The demo https://jsfiddle.net/OlegKi/b15pmdcg/4/ loads jqGrid from https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.6/js/jquery.jqgrid.src.js (see "External Resources" on the left part). I see currently the error with the status 502 (bad gateway) during loading the file. It's the reason, why I can't open currently the demo https://jsfiddle.net/OlegKi/b15pmdcg/4/. Then I can load jagrid file, but another file https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/fonts/fontawesome-webfont.woff2?v=4.7.0 can't be loaded... – Oleg Feb 15 '17 at 07:38
  • Yes, but that is for CDN right ? what if I have copied the files locally and trying ? – XamDev Feb 15 '17 at 07:41
  • @Rohit: Sorry, but I can't follow you. You should describe exactly what problem you have and **how I can reproduce the problem**. – Oleg Feb 15 '17 at 07:43
  • Simply, We have added sorting for Title colum, but if I want to add for other columns how can I do that ? – XamDev Feb 15 '17 at 07:46
  • @Rohit: What demo should I open and what should I do to reproduce the problem? You can open https://jsfiddle.net/OlegKi/b15pmdcg/10/, where I replaces some URLs from cdnjs to jsdelivr and it works at me. You can click on the column header of Title file, then on the column header of UserName Column or WhenCreated column. By clicking 3 times of some column header you can remove sorting by the column. – Oleg Feb 15 '17 at 07:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135742/discussion-between-rohit-and-oleg). – XamDev Feb 15 '17 at 08:41
  • @Rohit: You use loading from the server using `loadonce: true`. Thus **the server have to sort the data corresponds to `sortname: "Title", sortorder: "desc"`**. It you want that jqGrid do initial sorting **on the client side**, then you should add new free jqGrid option `forceClientSorting: true`. See https://jsfiddle.net/OlegKi/b15pmdcg/18/. – Oleg Feb 15 '17 at 09:52
  • @Rohit: You use `multiSort: true` option. Thus the first click on the column header of Title column will invert the sort order. The next click will switch off the sorting and display all in the order in which the data are loaded. One more click will sort in ASC order. If you don't need the 3-state behavior of sorting then you should remove `multiSort: true` option. See https://jsfiddle.net/OlegKi/b15pmdcg/19/. All works exactly as expected. – Oleg Feb 15 '17 at 09:53
  • @Rohit: Try the demo https://jsfiddle.net/OlegKi/b15pmdcg/22/, which loads all data from cdn.jsdelivr.net CDN only. Is some problem exist on the demo? It does the same as https://jsfiddle.net/OlegKi/b15pmdcg/19/, but using Bootstrap CSS instead of jQuery UI CSS. – Oleg Feb 15 '17 at 10:18