Search code examples
jqueryajaxscalalift

using jquery functions for showing div in lift framework snippet


I am trying to create one simple lift web app. in that i want to show and hide two div while clicking a button in the same page using ajax call. i tried the following code so far.

In my view

 <div id="PGMainDiv" data-lift="ShowBookedTicket" style="width=400px;height=600px;border:2px solid #FF0000;border-radius:5px">
      <div id="sampleDiv" style="font-size:15px;color:#19552D;padding:10px 5px 10px 10px">
       This is a sample div which show and hide while ajax calling
      </div>
      <div style="padding-left:267px">
         <button id="PGOK" value="OK" style="width:70px">OK</button>
      </div> 

 </div>
 <div id="ticketDiv" style="display:none;border:2px solid #FF00FF">
    <p>This is a another sample div which show and hide while ajax calling</p>
 </div>

and in my ShowBookedTicket snippet

class ShowBookedTicket {

def testFunction(s: String): JsCmd = {                  
        JsCmds.Run("jQuery('#ticketDiv').show()")
        JsCmds.Run("jquery('#sampleDiv').hide()")
        //JsCmds.Function("testJsFunction",List("param"),SHtml.ajaxCall( () => JsRaw("""$('#ticketDiv').show()""")).cmd)._2 
}

  def render = "*" #> {

     "#PGOK [onClick]" #> SHtml.onEvent(testFunction)

   }
 }

here if i give only the first div showing code (JsCmds.Run("jQuery('#ticketDiv').show()")) it is working properly. but if i give the second line, nothing is happening there. and in the commented line also tried but that is showing some errors (overloaded method value ajaxCall with alternatives: (jsCalcValue: net.liftweb.http.js.JsExp,jsContext: net.liftweb.http.JsContext,func: String => net.liftweb.http.js.JsCmd)net.liftweb.http.GUIDJsExp <and> (jsCalcValue: net.liftweb.http.js.JsExp,func: String => net.liftweb.http.js.JsCmd)net.liftweb.http.GUIDJsExp cannot be applied to (() => net.liftweb.http.js.JE.JsRaw) in eclipse). can anybody give an answer..

Thanxx..!!


Solution

  • You forgot to link both calls. Your function only returns the second line as a jsCmd. Try the following:

    def testFunction(s: String): JsCmd = {                  
        JsCmds.Run("jQuery('#ticketDiv').show()") &
        JsCmds.Run("jquery('#sampleDiv').hide()")
    }
    

    Note: as described here, you can also use the built in commands for hide/show:

    def testFunction(s: String): JsCmd = {
        JsCmds.JsShowId("ticketDiv") &
        JsCmds.JsHideId("sampleDiv")
    }