0
Under vurdering

Construct a JSON array

doug 10 år siden opdateret 10 år siden 3
I am looking to be able to either build an array within a local JSON datasource, or be able to render a full list of comma separated objects that I have created via the "add" property for a JSON datasource.

I can add the data to the datasource just fine, but I then want to assign ALL of the data of the datasource to a variable to be able to push it to a REST API. With that said, how can I add an object to a datasource, and then add additional objects to the first object as an array. OR as I said, be able to render all of the content of the datasource that I have been assigning data to.
Under vurdering
Doug,

Let me work up an example for you to look at. 
Let me know if you have any other questions: 


<wire>
  <datasources>
    <datasource name="field-list" source="" query="/" providertype="json"/>
  </datasources>
  <classes>
    <class name="article-list-item">
         <panel name="article-[param:dataSourceIndex]" width="95%" height="20%" lheight="25%" _name="[param:text]" _dataSourceIndex="[param:dataSourceIndex]" align="center" background="#ff0000" bordercolor="00ee00" borderwidth="3">
              <text name="headline" width="100%" height="50%" color="#ffffff" font="helvetica" size="18" align="center" valign="top" alignment="center" text="[param:text]"></text>
         </panel>
    </class>
  </classes>
  <main>
  
    <panel name="bg" width="100%" height="100%">
      <panel name="list-container" width="100%" lwidth="95%" height="100%" align="center" clip="yes">
    <list name="list" datasource="field-list" width="102%" lwidth="102%" height="100%" align="center" orientation="vertical" paginate="no">
      <object class="article-list-item" dataSourceIndex="[param:dataSourceIndex]" text="[param:value]" />
    </list>
      </panel>
    </panel>
  </main>
<actions>
  <action name="oninit" oninit="yes">         
       <play action="add"></play>
  </action>
   <action name="complete" datasource="field-list">
      <alert message="[datasource:field-list.1.value] \\ [datasource:field-list.2.value] \\ [datasource:field-list.3.value]"/>
      <play action="concatonate" />
   </action> 
   
   <!-- This action is acting as the tool to add elements to a locally stored json file-->
   <action name="add">
      <sync>
      <assign property="var:counter" value="0" />
         <!-- The loop allows us to add more elements to the json faster-->
      <loop lhs="[var:counter]" operator="lt" rhs="10">
          <assign property="datasource:field-list.add" value='{"name":"param","value":"value[var:counter]"}'/>   
          <assign property="var:counter" value="[js:[var:counter]+1]" />
      </loop>
      <!--Trigger a querycomplete to them compile the contents to send somewhere else-->   
      <assign property="datasource:field-list.event" value="querycomplete"/>
      </sync>   
   </action>
   
   <!--This action will capture the counter value from the json and concatonate a comma separated list. You probably will need to do some regex or something to remove the first comma.-->
   <action name="concatonate" >
      <sync>
         <assign property="var:allthestuff" value="" />    
         <assign property="var:counter" value="0" />
         <loop lhs="[var:counter]" operator="lte" rhs="[datasource:field-list.dataSourceResultCount]">
             <assign property="var:allthestuff" value="[var:allthestuff],[datasource:field-list.[var:counter].value]"/>   
             <assign property="var:counter" value="[js:[var:counter]+1]" />
         </loop>
         <!--Use this final var to send as a postcontent through your REST API-->
         <alert message="[var:allthestuff]"/>
      </sync>   
   </action>
</actions>
</wire>
              
  
Yep this worked. Instead of using a regex to remove the comma, I just wrapped it within an if statement like so:

<if lhs="[var:contacts-to-upload]" operator="ne" rhs="">
	<assign property="var:contacts-to-upload" value="[var:contacts-to-upload],[datasource:updatedContactStorage.[var:contacts-index].dataSourceResultContent]" />
	<else>
		<assign property="var:contacts-to-upload" value="[datasource:updatedContactStorage.[var:contacts-index].dataSourceResultContent]" />
	</else>
</if>

Kundesupport af UserEcho