Search code examples
javagrailsgsp

How to compare hash map key dynamically inside the closure in Grails?


Here I have hash map passed from the controller to GSP page.

Controller:

Map<String, List<String>> nameFiles = new HashMap<String, List<String>>();

  nameFiles.put('patient1',["AA","BB","CC"]);
  nameFiles.put('patient2',["DD","EE","FF"]);

model.put("nameFiles", nameFiles);

GSP page:

var patient = getPatient(); // lets say we get random patient through some jQuery function, could be not available in the nameFiles key

//check If nameFiles contain key same as patient varaible 
 <% nameFiles.each { fm -> %>
    <% if (fm.containsKey(patient)) { %> // This if statement cannot compare dynamic sting varaaible. How to do this.
        alert("Yes nameFiles contain the patient");
    <% } %>
<% } %>

Solution

  • Assuming you have :

      Map<String, List<String>> nameFiles = new HashMap<String, List<String>>();
                nameFiles.put('patient1',[id:1,name:'Fred'])
                nameFiles.put('patient2',[id:2,name:'Tom'])
    

    It is as simple as this to get current patient:

        <% def aa = nameFiles?.find{it.key=='patient1'}?.value %>
          <g:if test="${aa}">
            //  we definitely have ${aa} and it has been made null safe 
          <g:if>
    

    This returns {id:1, Name:Fred} on gsp which is the list iteration

    My goodness all that else if it is as if you are in a controller, I understand why you are having to do this but it isn't good practice you could just create a tagLib that takes current value and processes the entry according to something in a given list or maybe against db fresh on the fly all correctly presented produced.

    Final edit whilst you can declare variables like jsp you can also use

    <g:set var="aa" value="${nameFiles?.find{it.key=='patient1'}?.value}" scope="page|session|..."/>
    

    By default variable is set for the page but could be made into a session variable either way it is a lot cleaner than <% %>

    Hopefully final edit

    I think people should think carefully about what their actual problem is and try to present the problem clearly otherwise the audience ends up answering something else due to the poor quality of the post.

    If I understand correctly you have something happening in a controller as in some list being produced. The missing bit must be you are then doing some form of form check maybe a select box selection that then ends up in jquery by that you mean you have some form of java script check going on against a form field check.

    There are two ways of pumping such information into the javascript world for such purposes

    Method 1:

    //I haven't tested this, hopefully gives you the idea
    var array = []
    <g:each in="${namedFiles}" var="${pa}">
        array.push({code:'${pa.key} listing:'${pa.value}'})
    </g:each>
    

    Method 2 Controller

     //where you now push named files as a json  
      return [namedFiles as JSON].toString()
    
      // or alternatively in gsp javascript segment something like this
      var results=$.parseJSON('<%=namedFiles.encodeAsJSON()%>');
      var results = results['patient1']