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

Mashiane

Expert
Licensed User
Longtime 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.

B4X:
'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
 

Kiffi

Well-Known Member
Licensed User
Longtime User
@Mashiane:

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

This works for me:
B4X:
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
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime 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..

B4X:
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
 
Upvote 0

alwaysbusy

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

B4X:
Dim jQ As BANanoObject
jQ.Initialize("$")
jQ.Selector($"#${ID}div"$).RunMethod("floatingActionButton", CreateMap("hoverEnabled": hoverEnabled, "direction": Direction, "toolbarEnabled": toolbarEnabled ))
 
Upvote 0

Mashiane

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

B4X:
Dim jQ As BANanoObject
jQ.Initialize("$")
jQ.Selector($"#${ID}div"$).RunMethod("floatingActionButton", CreateMap("hoverEnabled": hoverEnabled, "direction": Direction, "toolbarEnabled": toolbarEnabled ))
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?

B4X:
'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...
 
Upvote 0
Top