0

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}

                            }

                });
Community
  • 1
  • 1
troy_frommer
  • 102
  • 1
  • 21
  • Can we see the definition of the jqgrid? I'm not 100% sure what you're asking. – Aaron Brake Aug 01 '13 at 18:00
  • @AaronBrake Sure let me add that in. – troy_frommer Aug 01 '13 at 18:07
  • @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 Aug 01 '13 at 18:20
  • 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 Aug 01 '13 at 18:25
  • Alternatively, I found this on the wiki: rowObject - is a row data represented in the format determined from datatype option. If we have datatype: xml/xmlstring - the rowObject is xml node,provided according to the rules from xmlReader If we have datatype: json/jsonstring - the rowObject is array, provided according to the rules from jsonReader. If you're pulling in JSON, is it possible that you'd need to access it through array? – Aaron Brake Aug 01 '13 at 18:28
  • @AaronBrake That is exactly it. How exactly could I build the link in the view per say? I am very new to both Xpages and jqGrid. – troy_frommer Aug 01 '13 at 18:30
  • In your view definition, simply build the url string as the column value just as you are in your formatter, and when trying to get the Doc ID use @Text(@DocumentUniqueID), something like this: "' class='doclink'>" + + "" – Aaron Brake Aug 01 '13 at 18:36
  • @AaronBrake I see what you are saying with the Array comment. Let me look into that, and then if I cannot get it that way I will try with setting the view definition. – troy_frommer Aug 01 '13 at 18:40
  • 2
    Maybe this answer can help? http://stackoverflow.com/a/4486142/785061 – Per Henrik Lausten Aug 01 '13 at 18:41
  • @PerHenrikLausten that does help. Once I save the row data to a local object will I be able to access rowObject["docid"] when I build the url after sorting then? – troy_frommer Aug 01 '13 at 18:48
  • I really don't know :-) But I see that it mentions the need to access the values using integers (such as rowObject[1]) determined by using isArray(). – Per Henrik Lausten Aug 01 '13 at 19:06
  • @PerHenrikLausten I tried accessing the values of docid with integers but it did not fix the problem as it made the docid param "undefined" on the first load. I have a check in my root function with .isArray() is this what you are referring to? – troy_frommer Aug 02 '13 at 15:10
  • yes, apparently you have to check with isArray and then either do rowObject["docid"] or rowObject[integer] – Per Henrik Lausten Aug 02 '13 at 15:14
  • @PerHenrikLausten Oh ok, I understand now. In my case I need the rowObject["docid"] syntax or rowObject.docid (both work) I think I understand what I need to do now. Is it better to add the docid in a hidden column or save it into an external object? – troy_frommer Aug 02 '13 at 15:20
  • Troy, I haven't got a clue :-) – Per Henrik Lausten Aug 02 '13 at 15:23
  • @PerHenrikLausten I fixed my issue! I added in a column for the docid and it allows the sort function to run correctly. Thanks so much for your help and pointing me in the right direction. – troy_frommer Aug 02 '13 at 17:16
  • @PerHenrikLausten if you drop that link into an answer I will award you the bounty since that helped solve my problem. – troy_frommer Aug 02 '13 at 19:49

2 Answers2

1

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.

troy_frommer
  • 102
  • 1
  • 21
1

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?

Community
  • 1
  • 1
Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76