1

I have a JqGrid and want to toggle automatically the display of the last edited row/group when returning to the view/page containing the grid.

The process I have is

  1. Write the last edited OrderId to a session variable on completion of add/edit
  2. Pass this to the view where is is held in a hidden form element

What I would like to do now is use this "id" to locate the nearest (in reverse) grouping table row which I can then use to toggle the display.

The for the OrderId is easily identified since the id"" attribute matches the OrderId.

The grouping element though is 0 indexed and numbered only according to it's position in the grid i.e. it does not relate to the OrderId or OrderLinkId.

24/10/2012 Amended the below example to demonstrate that the group header elements are closed before the individual Order elements ie. siblings not parent/child

<tr id="clientOrderGridghead_0_6" class="ui-widget-content jqgroup ui-row-ltr clientOrderGridghead_0" role="row">
.....Group Header 6 Content.....
</tr>
<tr id="403" class="ui-widget-content jqgrow ui-row-ltr" style="" tabindex="-1" role="row">
</tr>
<tr id="414" class="ui-widget-content jqgrow ui-row-ltr" style="" tabindex="-1" role="row">
</tr>
<tr id="418" class="ui-widget-content jqgrow ui-row-ltr" style="" tabindex="-1" role="row">
</tr>
<tr id="clientOrderGridghead_0_7" class="ui-widget-content jqgroup ui-row-ltr clientOrderGridghead_0" role="row">
......Group Header 7 Content......
</tr>

The example above shows the top element which acts as the group header, with the subsequent 3 elements being the record rows for the related Orders.

The header element always has the class

class="ui-widget-content jqgroup ui-row-ltr clientOrderGridghead_0"

I have been able to successfully do the toggling by hard coding the id of the group element as follows

jQuery('#clientOrderGrid').jqGrid('groupingToggle','clientOrderGridghead_0_6');

But am now struggling to get the id necessary to plug in to the groupingToggle call and make it dynamic.

I've tried the following

var groupId = $('#' + lastOrderId).closest('tr').find('.ui-widget-content jqgroup ui-row-ltr clientOrderGridghead_0').attr('id');

After seeing the example here Using a class name in jQuery's .closest()

But but this returns undefined.

Any pointers greatly appreciated.

24/10/2012 Another attempt

var groupId = $('#' + lastOrderId).closest('tbody').find('.ui-widget-content jqgroup ui-row-ltr clientOrderGridghead_0').attr('id');

But still no joy

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

I think you should go for

var groupId = $('#' + lastOrderId).parent().find('.clientOrderGridghead_0').attr('id');

EDIT: Removed unnecessary classes in find.

EDIT 2: I couldn't understand your question at the first place.

Please try this:

var groupId = $('#' + lastOrderId).prev('.clientOrderGridghead_0').attr('id');

I seriously hope this works for you this time.

Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28
  • Thanks for your response @Pulkit Mittal however this still returns 'undefined'. Though it's not clear from my original post the group header is closed before - and does not enclose - the other elements, so I believe they are regarded as siblings rather then parent/child. – Cheesenbranston Oct 24 '12 at 11:39
  • My mistake I think what @Pulkit Mittal suggested is equivalent to my last attempt as the is the first parent node when traversing back up the DOM tree. – Cheesenbranston Oct 24 '12 at 14:06
  • Thanks @Pulkit I tested your last edited suggestion and am now getting the id attribute back. Only problem is that since the all elements are enclosed on the one parent I always get the id of the same group header element returned i.e. clientOrderGridghead_0_0, the first in the sequence. I have edited my original attempt to use the 'closest' method with find, but am now back to getting 'undefined' – Cheesenbranston Oct 24 '12 at 16:59
  • Hey bro, check out my update this time. I couldn't get your question until before your last comment. I hope it helps. – Pulkit Mittal Oct 24 '12 at 17:48
  • Hi Puklit, no problem, my question wasn't the best in the first place. I'm away from work just now, but will test first thing tomorrow and get back to you. Thanks again, – Cheesenbranston Oct 24 '12 at 19:01
  • Just a note, this relates to JqGrid. I ran into an anomaly when the header to be toggled is the first on the grid. I watched and found that when this is the case 'undefined' is returned. I put an if statement i to default to first row if this happened i.e. (groupId == null) : 'clientOrderGridghead_0_0' : groupId – Cheesenbranston Oct 25 '12 at 15:27