Search code examples
jqueryjqgridstruts2

How to send id with <a href > of an element in struts jQuery grid


I want to retrieve and send an id of an element in struts jQuery grid.

<sjg:grid id="gridtable"
        caption="Editer Eleves"
        dataType="json"
        width="755"
        href="%{remoteurl}"
        pager="true"
        gridModel="gridModel"
        rowList="5,10,15,20"
        rowNum="5"
        rownumbers="true"
        multiselect="true"      

    >
    <sjg:gridColumn name="eleveId" title="Eleve Id" formatter="integer" sortable="false"/>
    <sjg:gridColumn name="eleveNom" index="eleveNom" title="Eleve Nom" editable="true" edittype="text" sortable="true"/>
    <sjg:gridColumn name="elevePrenom" index="elevePrenom" title="Eleve Prenom" editable="true" edittype="text" sortable="false"/>

</sjg:grid>

so I did these but it's not working:

<p><a class="btn" onclick="jQuery('#gridtable').getRowData(id)['eleveId']" href="#"> Détails</a>

Solution

  • First of all it seems that eleveId play the role of native id your grid. If the values of the column have unique values that you could add key="true" property (see the documentation) to the definition of the eleveId column. After that the rowid (the value of id attribute of <tr> elements in the grid) will be the same as the values in eleveId column.

    If the user select some row of grid the callback onSelectRow will be called. In the documentation of struts2 is described "Topics". The example shows how to define and subscribe the corresponding events. The value event.originalEvent.id in event handler will be the rowid of the last selected row. If you added key="true" property to the definition of the column eleveId then the value event.originalEvent.id will be the same as eleveId. If you can't use key="true" property then you can use $("#gridtable").jqGrid("getCell", event.originalEvent.id, "eleveId") to get the data.

    Starting with version 4.3.2 jqGrid supports events additionally to callbacks. For example you can use event jqGridSelectRow to get full information about the last selected row:

    $("#gridtable").bind("jqGridSelectRow", function (e, rowid, status, orgEvent) {
        alert("rowid=" + rowid);
    });
    

    because you need only rowid then you can remove all optional parameters from the event handler

    $("#gridtable").bind("jqGridSelectRow", function (e, rowid) {
        alert("rowid=" + rowid);
    });
    

    Inside of the handler you can use jQuery.ajax to call struts2 action.