【问题标题】:Javascript put data in columns tableJavascript将数据放在列表中
【发布时间】:2019-08-09 18:06:15
【问题描述】:

我有一个表格,我想将我从 ajax 调用中获得的数据放入列而不是行中,这里是表格正文代码

<tbody id="tableData-marketMonth">
    <tr>
        <th>Leads</th>
    </tr>
    <tr>
        <th>Full Year Cost</th>
    </tr>
    <tr>
        <th>{{date('F')}} Share of Cost</th>
    </tr>
    <tr>
        <th>Cost per Lead</th>
    </tr>
</tbody>

这是将数据放入表中的 JavaScript 代码

//Monthly Marketing Cost Report
$.get('/dashboard/costs', function(data){
  $.each(data,function(i,value){
    var tr =$("<tr/>");

    tr.append($("<th/>",{
      text : value.olxTotal
    })).append($("<th/>",{
      text : value.budget_total_year
    })).append($("<th/>",{
      text : value.budget_total_month
    })).append($("<th/>",{
      text : value.budget_per_lead
    }))
    $('#tableData-marketMonth').append(tr);
  })  
})

这是当前输出 期望的输出

非常感谢

【问题讨论】:

标签: javascript jquery mysql laravel


【解决方案1】:

我想我明白你的意思,可能最好只是将 id 添加到每个 &lt;tr&gt;,然后将值附加到它们,如下所示。

HTML

<table>
    <tbody id="tableData-marketMonth">
        <tr id="leads">
            <th>Leads</th>
         </tr>
         <tr id="fyc">
             <th>Full Year Cost</th>
         </tr>
         <tr id="soc">
             <th>{{date('F')}} Share of Cost</th>
         </tr>
         <tr id="cpl">
             <th>Cost per Lead</th>
         </tr>
    </tbody>
</table>

jQuery

//Monthly Marketing Cost Report
$.get('/dashboard/costs', function(data){
  $.each(data,function(i,value){
      var leads = $('#leads');
      var budget_total_year = $('#fyc');
      var budget_total_month = $('#soc');
      var budget_per_lead = $('#cpl');

      leads.append('<td>' + value.olxTotal + '</td>');
      budget_total_year.append('<td>' + value.budget_total_year + '</td>');
      budget_total_month.append('<td>' + value.budget_total_month + '</td>');
      budget_per_lead.append('<td>' + value.budget_per_lead + '</td>');
  })  
})

【讨论】:

  • 太棒了,非常感谢你的工作就像一个魅力正是我想做的事
  • 太棒了!这不是有史以来最干净的代码,请随意清理它,我喜欢有一些示例,人们可以看到正在发生的事情并从中学习:)
猜你喜欢
  • 2014-01-23
  • 2014-12-31
  • 2012-11-08
  • 2021-09-23
  • 2019-02-17
  • 2012-05-08
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多