4

I have this sample page created that uses Jquery datatable within that I'm using <apex:actionsupport> and when I click on the hyperlink for that particular column it does not fire the event and the way I'm testing is that when the user clicks on the a href it fires the action method on the controller and I'm spitting the system.debug just to make sure it does calling this method. When I looked at the log it does not show that action method fires, what I'm doing wrong here?

Markup:

 <apex:form id="myform"> 
        <head>
            <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
                <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
                <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
                <script>
                j$ = jQuery.noConflict();
                j$(document).ready( function () {
                    var tbl = j$('[id$="tbl"]').DataTable({
                        //for empty
                        "columnDefs": [{
                            "defaultContent": "empty",
                            "targets": "_all"
                        }]
                    });
                });
                </script>
            </head>

<table class="table table-striped table-hover" id="tbl" >
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <apex:repeat value="{!myData}" var="t" id="reg">
                        <tr>
                            <td>  
                                <apex:actionsupport action="{!loadDataById}" event="onclick" rerender="pb2" >  
                                <a href="#"><apex:param name="selectedId" value="{!t.Id}">{!t.Name}</apex:param></a>
                                </apex:actionsupport>  
                            </td>
                            <td>{!t.description} </td> 
                        </tr> 
                    </apex:repeat>
                </tbody>
            </table> 

          <br/>
          <apex:pageblock id="pb2 ...>
             {!SecondList}
          </apex:pageblock>

 </apex:form>

Controller:

public List<my_custom_object> secondList { get; set; } 
public void loadDataById()
{
    String selectedId = System.currentPagereference().getParameters().get('selectedId');
    List<my_custom_object>  myDataList = [SELECT ...];
    system.debug('selectedid: ' + selectedId);
    secondList = myDataList ; 
}
  • Can you try with apex:outputLink instead of plain a tag? See if it works.. – javanoob Jan 10 '17 at 17:27
4

You have to nest the <apex:actionSupport> tag within the tag whose action you are trying to support. You have it inverted.

Correct

<a ...>
    <apex:actionSupport ... />
</a>

Incorrect

<apex:actionSupport ...>
    <a ...>
    </a>
</apex:actionSupport>

However, it appears you may want to simply use the <apex:commandLink> tag here...

Joy

<td>
    <apex:commandLink action="{!loadDataById}" rerender="pb2">
        <apex:param name="selectedId" value="{!t.Id}" />
        {!t.Name}
    </apex:commandLink>
</td>

No Joy

<td>
    <apex:actionSupport ... />
    <a ...>
        <apex:param ...>
    </a>
</td>

Or if you are really committed to sticking with an <a> tag for some reason, at least follow the approach outlined in the documentation on <apex:actionSupport>, adapted below to closer mirror your situation. Basically you want to add the click listener to a wrapper div.

<apex:page controller="Throwaway">
    <apex:form>
        <apex:outputpanel id="counter">
            <a href="#">Click Me!: {!count}</a>
            <apex:actionSupport event="onclick" action="{!incrementCounter}" rerender="counter"/>
        </apex:outputpanel>
    </apex:form>
</apex:page>
  • Adrian, did not help either so when I click on the hyperlink it does not fire the event and i do not see any activity in the logs. – Nick Kahn Jan 10 '17 at 17:16
  • I did not know that we can use actionSupport for plain HTML elements, I thought only for apex namespace elements. – javanoob Jan 10 '17 at 17:25
  • Joy snippet worked! 1+ – Nick Kahn Jan 10 '17 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.