【问题标题】:Why the table data is no being appending properly为什么表数据没有正确附加
【发布时间】:2016-01-26 16:41:38
【问题描述】:

我正在使用我得到的 json 响应动态显示表格。

我面临的问题是,表格中显示的数据以及下面的数据

Symbol  Close   Change(Rs)  Change(%)   AS ON
Symbol  Close   Change(Rs)  Change(%)   AS ON
Symbol  Close   Change(Rs)  Change(%)   AS ON
Symbol  Close   Change(Rs)  Change(%)   AS ON

这是我的代码

<section class="widget">
   <div>
      <table id="1d" class="losstable tablesorter">
         <thead>
            <tr>
               <th>Symbol</th>
               <th>Close</th>
               <th>Change(Rs)</th>
               <th>Change(%)</th>
               <th>AS ON</th>
            </tr>
         </thead>
         <tbody></tbody>
      </table>
      <table id="1w" class="losstable tablesorter">
         <thead>
            <tr>
               <th>Symbol</th>
               <th>Close</th>
               <th>Change(Rs)</th>
               <th>Change(%)</th>
               <th>AS ON</th>
            </tr>
         </thead>
         <tbody></tbody>
      </table>
      <table id="2w" class="losstable tablesorter">
         <thead>
            <tr>
               <th>Symbol</th>
               <th>Close</th>
               <th>Change(Rs)</th>
               <th>Change(%)</th>
               <th>AS ON</th>
            </tr>
         </thead>
         <tbody></tbody>
      </table>
      <table id="3w" class="losstable tablesorter">
         <thead>
            <tr>
               <th>Symbol</th>
               <th>Close</th>
               <th>Change(Rs)</th>
               <th>Change(%)</th>
               <th>AS ON</th>
            </tr>
         </thead>
         <tbody></tbody>
      </table>
      <table id="1m" class="losstable tablesorter">
         <thead>
            <tr>
               <th>Symbol</th>
               <th>Close</th>
               <th>Change(Rs)</th>
               <th>Change(%)</th>
               <th>AS ON</th>
            </tr>
         </thead>
         <tbody></tbody>
      </table>
   </div>
</section>

这是小提琴

http://jsfiddle.net/abkNM/7607/

你能告诉我如何解决这个问题吗??

【问题讨论】:

    标签: jquery


    【解决方案1】:
    <script type="text/javascript">
    var tableclass = ".tablesorter";
    var html = "";
    
    for (var s = 0; s < jsonresp.length; s++) 
    {
        var symbol = jsonresp[s].symbol,
        close = jsonresp[s].todays_close,
        prev_close = jsonresp[s].prev_close,
        as_on = "YDA";
        d = parseFloat(close) - parseFloat(prev_close), d = d.toFixed(2);
        var l = (close - prev_close) / close * 100;
        l = l.toFixed(2), html += "<tr><td>" + symbol + "</td><td>" + close + '</td><td class="redclass">' + d + '</td><td  class="redclass">' + l + '</td><td class="">' + prev_close + "(" + as_on + ")</td></tr>";
    }
    $(".widget > div > table > tbody").html(html);
    </script>
    

    【讨论】:

    • 我有很多表使用 tablesorter 作为它的类,所以不能使用这个代码。
    • 有四个表是固定的,如 1d、1w 等。
    • 使用此代码:$(".widget > div > table > tbody").html(html);
    【解决方案2】:

    查看您的 jsfiddle,您有不同的带有 id 的表,但您只选择第一个带有 id #1d 的表并将数据附加到第一个表的 tbody 中。其他表格标题仍在打印中。

    在您的 jsfiddle 中,您有 var tableid = "#1d";,但在那之后,除了您调用插入 html 之外,该 tableid 没有任何反应。看起来您的代码还没有完成/完成。

    【讨论】:

    • 谢谢,但我的代码已经完成,只是我想将数据附加到表中。
    • 我试过这种方式 $('.losstable table').not(tableid ).hide();
    • 我以某种方式解决了这个问题,非常感谢 Christophe 和 Ashsish。
    • 抱歉忘记回来查看。默认情况下,您可以使用 css 隐藏它们,并且仅在通过添加 .show() 将数据附加到表格后才显示表格。例如.html(html).show();
    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多