Filling a ListIconGadget error?

Just starting out? Need help? Post your questions and find answers here.
51_q99e9h.C
Posts: 3
Joined: Thu May 21, 2015 10:51 pm

Filling a ListIconGadget error?

Post by 51_q99e9h.C »

Hi All!

After my first steps with SB I ran into an issue: I try to fill an ListIconGadet by reading JSON-Data from a PHP-Script. What happend is, that the data appears only after the second click on the Test-Button!

Could anyone give a hint to solve the problem?

Lars

Attantion: SB and demo.php only work if they are running on a web-server.

demo.php

Code: Select all

<?php
   $array = array(	"Peter"=>"35",
			"Ben"=>"37",
			"Joe"=>"43");
   echo json_encode($array);
?>
Spiderbasic-Code

Code: Select all

Global Window_0
Global lb_job,demobutton
Global NewMap jsonValues.s()

Procedure get_json(Success, Result$)
  Protected i,jj
  ClearMap(jsonValues())
  If Success
    jj=ParseJSON(#PB_Any, Result$)
    If jj
      ExtractJSONMap(JSONValue(jj), jsonValues())
      FreeJSON(jj)
    EndIf
  Else
    ; Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

Procedure GadgetEvents()
  eg=EventGadget()     
  et=EventType() 
  Select eg
    Case demobutton
      HTTPRequest(#PB_HTTP_Get, "demo.php", "", @get_json())
  ClearGadgetItems(lb_job)
  ForEach jsonValues()
    AddGadgetItem(lb_job,-1,Str(r)+#LF$+MapKey(jsonValues()) + #LF$ + jsonValues())
    r+1
  Next
  EndSelect
EndProcedure

Window_0 = OpenWindow(#PB_Any, x, y, 600, 400, "demo", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_WindowCentered)
lb_job = ListIconGadget(#PB_Any, 5, 50, 520, 330, "Column 1", 100)
demobutton = ButtonGadget(#PB_Any, 5, 8, 80, 30, "TEST")
  
RemoveGadgetColumn(lb_job,0)
AddGadgetColumn(lb_job, 0, "id",0)  ; is not 0!
AddGadgetColumn(lb_job, 1, "Name",80)
AddGadgetColumn(lb_job, 2, "Age",20)

BindEvent(#PB_Event_Gadget,@GadgetEvents())

User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Filling a ListIconGadget error?

Post by Peter »

move the code to fill your Gadget into the Callback:

Code: Select all

Global Window_0
Global lb_job,demobutton
Global NewMap jsonValues.s()

Procedure get_json(Success, Result$)
  Protected i,jj
  ClearMap(jsonValues())
  If Success
    jj=ParseJSON(#PB_Any, Result$)
    If jj
      ExtractJSONMap(JSONValue(jj), jsonValues())
      FreeJSON(jj)
      
      ClearGadgetItems(lb_job)
      ForEach jsonValues()
        AddGadgetItem(lb_job,-1,Str(r)+#LF$+MapKey(jsonValues()) + #LF$ + jsonValues())
        r+1
      Next      
      
    EndIf
  Else
    ; Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

Procedure GadgetEvents()
  eg=EventGadget()     
  et=EventType() 
  Select eg
    Case demobutton
      HTTPRequest(#PB_HTTP_Get, "demo.php", "", @get_json())
      
  EndSelect
EndProcedure

Window_0 = OpenWindow(#PB_Any, x, y, 600, 400, "demo", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered | #PB_Window_WindowCentered)
lb_job = ListIconGadget(#PB_Any, 5, 50, 520, 330, "Column 1", 100)
demobutton = ButtonGadget(#PB_Any, 5, 8, 80, 30, "TEST")

RemoveGadgetColumn(lb_job,0)
AddGadgetColumn(lb_job, 0, "id",0)  ; is not 0!
AddGadgetColumn(lb_job, 1, "Name",80)
AddGadgetColumn(lb_job, 2, "Age",20)

BindEvent(#PB_Event_Gadget,@GadgetEvents())
Greetings ... Peter
51_q99e9h.C
Posts: 3
Joined: Thu May 21, 2015 10:51 pm

Re: Filling a ListIconGadget error?

Post by 51_q99e9h.C »

I think it has to do with asyncron AJAX (httprequest is allway asyncron I think). So if I try to access the map the map is not succssfully filled. For some kind of things asyncron AJAX request are not really usefull and compared to PB it is the complete oposite behavior. So for my needs I think to use a custom syncron AJAX-function to make sure the fetched data is really available after sucessfull script loading.
Post Reply