简体   繁体   中英

Loop json with Vue.js to populate a html table

On page load I fetch some data in json format from the database. Now I would like to use Vue.js to loop that json and build a html table.

Javascript/Vue.js part:

callStocks = function () {
          var app = new Vue({
            delimiters: ["[[", "]]"],
            el: "#stocksTerminal",
            data: {
              stocks: []
            },
            created() {
              axios
                .get("getStocksAvailable/")
                .then(response => {
                  this.stocks = response.data.data;
                  console.log(response.data.data[0].fields.stockName);
                });
            }
          });
};

callStocks();

HTML structure:

<table>
 <thead>
  <tr>
   <th>Company</th>
  </tr>
 </thead>
 <tbody id="stocksTerminal">
 <tr>
  <td v-for="item in stocks">[[ item.fields.stockName ]]</td>
 </tr>
 </tbody>
</table>

Json object:
在此处输入图像描述

Right now the frontend doesn't render anything. However I don't have any errors from Vue and the implemented console.log shows the correct item Infineon in the above example . I assume that the transmission from Vue to the variable in the fronend doesn't work.

Note: I use [[...]] delimiter since my Django framework preserves curly brackets. I have this in another project running successfully and this shouldn't be the issue here.

// Update: The json on vue devtool show that info is not an array you want, you may do this: this.info = response.data.data in the .then to get the correct data.

Then try this

<td v-for="stockName in info">[[ stockName.fields.stockName ]]</td>

Use stockName in template

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM