-1

in w3.js

    {
"customers":[
{"CustomerName" : "Alfreds Futterkiste","City" : "Berlin","Country" : "Germany"},
{"CustomerName" : "Berglunds snabbköp","City" : "Luleå","Country" : "Sweden"},
]}


<select id="id01">
  <option w3-repeat="customers">{{CustomerName}}</option>
</select>



<script>
w3.getHttpObject("customers.js", myFunction);

function myFunction(myObject) {
  w3.displayObject("id01", myObject);
}
</script>

but my object not any name like(customers) and is:

[{"id":12,"name":"hamwrew"},{"id":13,"name":"mamad2"}]

how to show my object? in html or jsp page

  • In `w3.displayObject` you can add your objects name `{customers: myObject}` and then in the template instead of referencing `CustomerName`, you will just reference `name` (since that is the property in each object in your array). – AdamExchange Sep 11 '20 at 23:51

1 Answers1

0

You don't have an object but an array so first create an object from your array:

var dataArray = [{"id":12,"name":"hamwrew"},{"id":13,"name":"mamad2"}];
var nameList = { names: dataArray };

so:

    <select id="id01">   
      <option w3-repeat="names" value={{id}}>{{name}}</option>
    </select>

    <script>
      w3.getHttpObject("customers.js", myFunction);

      function myFunction(myArray) {   
        var nameList = { names: myArray};   
        w3.displayObject("id01", nameList);
      }
   </script>
JB_DELR
  • 737
  • 1
  • 4
  • 7
  • Thank you very much .it's true if dataArray is ```[{"id":12,"name":"hamwrew","countries":[{"id":5,"name":"iran","capital":"ir"},{"id":6,"name":"usa","capital":"us"}]},{"id":13,"name":"mamad2","countries":[]}]``` how to show country.name – hamed nikbakht Sep 12 '20 at 16:22
  • So you want 2 – JB_DELR Sep 12 '20 at 19:30
  • yes for example ```
    Name ID Country Name
    {{name}} {{id}} {{countries..name}}
    ```
    – hamed nikbakht Sep 13 '20 at 14:21
  • Like it is says here, w3.js can't do repeat nested object. https://stackoverflow.com/questions/45777819/display-nested-objects-using-w3-repeat-of-w3-js-library – JB_DELR Sep 13 '20 at 14:39
  • There is a 'native' way to do this with for loop or array map base on this: https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement – JB_DELR Sep 13 '20 at 14:41
  • I did not notice this document . I just want to show countries.name next to the Id and name – hamed nikbakht Sep 13 '20 at 16:04
  • Have a look a this. https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Traversing_an_HTML_table_with_JavaScript_and_DOM_Interfaces. In your case, you need a loop inside thé first one. First loop for names, inside this loop, loop for countries. – JB_DELR Sep 13 '20 at 16:49