B4J Question [BANano] [Solved] FloatingActionButton iif method not defined - please advise

Discussion in 'B4J Questions' started by Mashiane, Jan 8, 2019.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi there

    I'm trying to build a floating action button, i have an iif method defined.

    Depending on what the developer, specifies, there is some javascript that needs to be built and then somehow injected to the page. I think perhaps I will be able to use an Eval Method, still learning the ropes anyway.

    Code:
    'injectjs
    private Sub InjectJS
        
    Dim script1 As String = $"$('#${ID}div').floatingActionButton({
        hoverEnabled: ${
    iif(hoverEnabled,"true","false")},
        direction:'${
    Direction}',
        toolbarEnabled: ${
    iif(toolbarEnabled,"true","false")}});"$
        
    Dim script2 As String = $"var inst${ID} = document.getElementById('${ID}');
        var ${
    Instance} = M.FloatingActionButton.getInstance(inst${ID});"$
    End Sub

    'iif method
    private Sub iif(Expression2Evaluate As String, ReturnIfTrue As Object, ReturnIfFalse As Object) As Object
        
    If Expression2Evaluate = True Then
            
    Return ReturnIfTrue
        
    Else
            
    Return ReturnIfFalse
        
    End If
    End Sub
     
    joulongleu likes this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    (I've edited the thread title)
     
    Mashiane likes this.
  3. Kiffi

    Kiffi Well-Known Member Licensed User

    @Mashiane:

    Smart strings are not easy to parse. If you get an error, try simplifying your code.

    This works for me:
    Code:
    Dim hEnabled As String = iif(hoverEnabled,"true","false")
    Dim tEnabled As String = iif(toolbarEnabled,"true","false")

    Dim script1 As String = $"jQuery('#${ID}div').floatingActionButton({
            hoverEnabled: ${
    hEnabled},
            direction:'${
    Direction}',
            toolbarEnabled: ${
    tEnabled}});"$
    Greetings ... Peter
     
    Mashiane and alwaysbusy like this.
  4. Mashiane

    Mashiane Expert Licensed User

    Had to resort to old-school. As its using jQuery and I had to initialize this with the bananoobject using $, it couldnt work, so the BANano stringbuilder has come to the rescue. Ta Peter..

    Code:
    change settings for the fab
    Sub setSettings()
        
    Dim hEnabled As String = iif(hoverEnabled,"true","false")
        
    Dim tEnabled As String = iif(toolbarEnabled,"true","false")
      
        
    Dim sb As StringBuilder
        sb.Initialize
        sb.Append(
    "$('#")
        sb.Append(
    ID)
        sb.Append(
    "div').floatingActionButton({")
        sb.Append(
    CRLF)
        sb.Append(
    "hoverEnabled: ")
        sb.Append(hEnabled)
        sb.Append(
    ",")
        sb.Append(
    CRLF)
        sb.Append(
    "direction: '")
        sb.Append(Direction)
        sb.Append(
    "',")
        sb.Append(
    CRLF)
        sb.Append(
    "toolbarEnabled: ")
        sb.Append(tEnabled)
        sb.Append(
    CRLF)
        sb.Append(
    "});").Append(CRLF)
        
    Dim jQ As BANanoObject
        jQ.Initialize(
    "$")
        Banano.Eval(sb.tostring)
    End Sub
    To pick up errors easily on my side, I don't do chaining between b4j and BANano. Very unreadable code though... lol

    fab.png
     
  5. Kiffi

    Kiffi Well-Known Member Licensed User

    you can replace the jQuery '$' sign by 'jQuery'. Take a look at my code above.
     
    joulongleu and Mashiane like this.
  6. alwaysbusy

    alwaysbusy Expert Licensed User

    Have you tried this? I can't test it now, but this could work:

    Code:
    Dim jQ As BANanoObject
    jQ.Initialize(
    "$")
    jQ.Selector(
    $"#${ID}div"$).RunMethod("floatingActionButton", CreateMap("hoverEnabled": hoverEnabled, "direction": Direction, "toolbarEnabled": toolbarEnabled ))
     
    joulongleu, Kiffi and Mashiane like this.
  7. Mashiane

    Mashiane Expert Licensed User

    Damn!!! what did you just do??? This works like magic! Perfect! I have so much to learn! Thanks a mill!

    There are a lot of these M.<ObjectType>.getInstance that I have to do here hey... perhaps let me push my luck whilst this is still hot.

    Any thoughts on this please (when you have time off course). The code below works, can it be done with the BANanoObject too?

    Code:
    'close
    Sub Close
        
    Dim script As String = $"var inst${ID} = document.getElementById('${ID}div');
        var ${
    Instance} = M.FloatingActionButton.getInstance(inst${ID});
        ${
    Instance}.close();"$
        Banano.Eval(script)
    End Sub
    Wow!! Thanks...
     
    joulongleu likes this.
  8. alwaysbusy

    alwaysbusy Expert Licensed User

    Start the same (again, can't test) and go from there:

    Code:
    Dim M as BANanoObject
    M.Initialize(
    "M")
    ...
     
    joulongleu and Mashiane like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice