Raaz
Raaz

Reputation: 125

How to pass the id of the selected CommandLink

I have a datatable, and one of the columns contains the status of the particular item. The status appears as a commandLink in the column. If the status of the item as retrieved from the database is a comma separated value like Status1,Status 2, I have to display 2 commandlinks in the column for the same item namely, one with Status1 and another with Status 2.

<h:commandLink id="status1Link" value="#{pc_test.status1}"
                            onclick="showAssignKeyRvPopup(#{plist.t3Id},'#{plist.t3FileName}','#{plist.status}');return false;"
                            rendered="#{plist.status == 'Status1,Status2'}"
                            update="assignKeyRvDialog">
                        </h:commandLink> 

The above is the code I'm using to display the commandLink. on click, it shows a popup, from which if I clcik OK, I have to perform an action that updates the status again. I have 2 commandLinks in the same column, my question is when I click on one commandLink and perform an action, I want the text of that commandLink only to change, not the other one. So, I need to pass the id of the commandLink that I click. Please let me know how I can go about doing this.

I tried just as you suggested -

<p:remoteCommand name="doSubmit" actionListener="#{pc_testmaps.doAssignUser}" />

<p:commandButton id="assignUser" value="Submit" onclick="doSubmit();"> </p:commandButton>

function doSubmit() {
            document.getElementById('nonMCLink') = commandLinkId;
            doAssignUser([{name:'commandLinkId', value:commandLinkId}]);
        }

And in the bean, 

public void doAssignUser() throws DelegateException {

        FacesContext context = FacesContext.getCurrentInstance();
        Map<String, String> map = context.getExternalContext().getRequestParameterMap();
        String linkStatus = (String) map.get("commandLinkId");
        System.out.println(linkStatus);
}

It still doesn't seem to work, when I print the linkStatus value, I'm getting it as null. Please help.

Upvotes: 0

Views: 1952

Answers (1)

mstzn
mstzn

Reputation: 2931

if you use primefaces,it can be so easily with p:remoteCommand

Firstly,get id of commandLink using javascript,then you can get it into your bean using action.

Here is simple example.Check this out.

using remoteCommand

UPDATE

    <p:remoteCommand name="doSubmit"
        actionListener="#{yourBean.doSubmit}" />

<script type="text/javascript">
function onOKclick() {
$(document).ready(function() {
    var commandLinkID = "yourcommandlinkId" //or get id using javascript with name          
doSubmit([{name:'commandLinkID', value:commandLinkID}]);
    });         
}
</script>

Your OK buton on the popup

  <h:commandButton onclick="onOKclick();" /> 
//calling onOKclick js function is important.you can call onOkclikc js function where you want 

Your doSubmitMethod in your bean;

public void doSubmit(){
  FacesContext context = FacesContext.getCurrentInstance();
            Map<String, String> map = context.getExternalContext().getRequestParameterMap();
           String value = (String) map.get("commandLinkID");
System.out.print(value);


}

Maybe it helps is you.

Upvotes: 1

Related Questions