0
votes

I am using a custom control on an Xpage to build a table with jqGrid. The jqGrid receives the data via JSON and fills the table correctly.

My question is with the sort function of jqGrid. I apply a custom formatter to the cell value of my "docname" column when the table first builds. The issue that I am having is after sorting any of the columns this formatter becomes unusable. The format is still applied but this particular formatter builds a url that launches another Xpage and relies on the parameter "docid".

Here is my custom formatter:

function openDocument(cellvalue, options, rowObject) {                          
return "<a target='#{javascript:compositeData.target}' title='Open Document' 
href='https://test.url.com?DocID=" +rowObject["docid"]+ 
"&action=#{javascript:compositeData.action}' class='doclink'>"
+ cellvalue + "</a>"; }

Is there any way to get the rowObject "docid" back into the openDocument formatter after sorting the grid?

I researched some and found the onSortCol event and have it initialized like this:

 onSortCol: function(index,iCol,sortorder) {alert("Test")}

My alert that I have in the onSortCol initilization is working, I just need to find out how to set my "docid" parameter after the sort is run since it is currently set to undefined after the sort.

I have tried putting in:

{rowObject["docid"]}

But this just breaks the sorting all together.

I have also read these posts:

onSortCol Event

JQGrid Sorting - how to trigger onSortCol event

jqGrid - Is it possible to set onSortCol after init?

Thanks for your help.

Edit:

jqGrid Definition:

     $().ready(function(){
     jQuery("#listxGrid").jqGrid({
        url:'#{javascript:compositeData.url}',
        datatype: "json",
        colNames:#{javascript:compositeData.colNames},
        colModel:#{javascript:compositeData.colModel},
        shrinkToFit:  false,
        jsonReader: {
        repeatitems: false,
        id: '#{javascript:compositeData.colID}',
        root: function (obj) {
            if ($.isArray(obj)) return obj;
            if ($.isArray(obj.items)) return obj.items;
                                return [];
                            },
        page: function () { return 1; },
        total: function () { return 1; },
        records: function (obj) {
            if ($.isArray(obj)) return obj.length;
            if ($.isArray(obj.items))   return obj.items.length;
                                return 0;
                            }
                        },
        gridview: true,
        loadonce: true,
        ignoreCase: #{javascript:compositeData.ignoreCase},
        rowNum: #{javascript:compositeData.rowNum},
        rowList: #{javascript:compositeData.rowList},
        rownumbers: #{javascript:compositeData.showRowNumbers},
        height: #{javascript:compositeData.height},
        caption: '#{javascript:compositeData.caption}',
        pager: '#pagerxGrid',
        viewrecords: true,
        emptyrecords: '#{javascript:compositeData.emptyRecords}',
        sortable: true,
        onSortCol: function(index,iCol,sortorder) {alert(index)},
        grouping: #{javascript:compositeData.grouping},
        groupingView : { 
            groupField : #{javascript:compositeData.groupField},
            groupDataSorted : true,
            groupColumnShow : #{javascript:compositeData.showGroupCol}

                            }

                });
2
Can we see the definition of the jqgrid? I'm not 100% sure what you're asking. - Aaron Brake
@AaronBrake Sure let me add that in. - troy_frommer
@AaronBrake my goal is to get the correct docid in the custom formatter after the table uses the sort function. Currently the formatter is still applied after sorting, but the docid parameter is underfined. I need to update this parameter based on the new order of the rowObjects. - troy_frommer
I'm not that familiar with jqgrid but what it looks like is you're passing in a number of things to a custom control that's using a URL (theoretically to a view) to populate the list. What I'm assuming is happening is that when the page is updating after your function call, the formatter is associated with the row (?) and thus the DocID doesn't update? Would it be possible to calculate out the entire link in the view rather than building it via formatter? - Aaron Brake

2 Answers

1
votes

Have a look at this answer which suggests the use of isArray to determine whether to use rowObject["docid"] or rowObject[integer]: jqGrid - rowObject inconsistencies?

1
votes

I ended up solving the problem this way. On the jqGrid it was receiving the "docid" param from the remote data on the first load just fine but as soon as the sort function called it would be set to "undefined."

As @PerHenrikLausten pointed out with his link, after the onload function completes this remote data type is then switched to local data. By loading in the docid into a hidden column, this is then available as a parameter for my formatter after the data type is switched.

By adding:

'Docid' as a colName & {name:'docid', index:'docid', hidden: true} to the colModel

It allows me to sort the jqGrid from any of my columns that I have sortable set to true with the docid parameter as what it should be not "undefined."

Thanks everyone for your input.