【问题标题】:Reflow table generated in loop not responsive循环中生成的回流表无响应
【发布时间】:2017-08-13 05:03:20
【问题描述】:

ajax 成功 之后,我正在 jQuery mobile 中循环生成一个reflow table。但是问题是表没有响应。在小屏幕上,生成的表格应该通过将表格列折叠成堆叠的演示文稿来工作,该演示文稿看起来像每行的标签/数据对块。当我输出从循环生成的表格标记并将其复制/粘贴到单独的页面时,表格看起来很好并且响应迅速。

我生成的html是:

<table data-role="table" id="time-table" data-mode="reflow" class="ui-responsive table-stroke">
    <thead>
       <tr>
         <th>Linje</th>
         <th>Destination</th>
         <th>Nästa tur (min)</th>
         <th>Därefter</th>
       </tr>
    </thead>
    <tbody>
       <tr>
         <th>7</th>
         <td>Resecentrum</td>
         <td>Nu</td>
         <td>11</td>
       </tr>
       <tr>
         <th>7</th>
         <td>Ö Lugnet via Resecentrum</td>
         <td>23</td>
         <td>51</td>
       </tr>
    </tbody>
</table>

在小屏幕上看起来像这样当我复制生成的标记并粘贴到单独的文件中时这是what I need

但是,当我使用代码动态执行此操作时:

success: function(data){
    //Generate table header and populate.
var thdata1 = "<th>"+data[2]+"</th>";
var thdata2 = "<th>"+data[3]+"</th>";
var thdata3 = "<th>"+data[4]+"</th>";
var thdata4 = "<th>"+data[6]+"</th>";

    var tblrow = $("<tr></tr>");
var thead = $("<thead></thead>");
var table = $("<table data-role=\"table\" id=\"time-table\" data-mode=\"reflow\" class=\"ui-responsive table-stroke\">");

tblrow.append(thdata1);
tblrow.append(thdata2);
tblrow.append(thdata3);
tblrow.append(thdata4);

    thead.append(tblrow);
table.append(thead)

    //Generate table body and populate.
var row = $("<tr>");
var flag = 0;
var tbody = $('<tbody>');
$.each(data, function(key, val){
    if(key >= 8){
    if(key%4 != 3){
      if(flag == 0)
        row.append("<th>"+val+"</th>");
      else
        row.append("<td>"+val+"</td>");
      flag++;
    }
    else if(key%4 == 3){
      row.append("<td>"+val+"</td>");
      tbody.append(row);
      row = $("<tr>");
      flag = 0;
    }
     }
     });
   table.append(tbody);
   table.appendTo("#contbl");
   console.log($("#contbl").html());
}

它会生成一个正常布局的无响应表格,如下图所示,即使在较小的屏幕中也保持相同的结构。在这个布局中,css 看起来也不合适。我正在使用 jQM 提供的默认表 class="ui-responsive table-stroke"

我生成的标记工作正常。我无法弄清楚这里有什么问题。

【问题讨论】:

  • 在小屏幕上查看时,my-custom-breakpoint css 类需要在哪里打破表格?您是否也尝试过刷新页面,因为我知道添加动态生成的 html 时,您需要使用 $('page_id').trigger('pagecreate'); 刷新页面内容
  • 我正在使用默认类 class="ui-responsive table-stroke" 我猜也应该这样做。因为当我复制粘贴生成的 html 时,它工作正常。
  • @AntonBelev:是的,我确实错过了这一点$('page_id').trigger('pagecreate');。现在可以了,谢谢。发布为答案,我会接受。

标签: jquery html css jquery-mobile html-table


【解决方案1】:

当您像在您的情况下那样动态创建 html 时,您必须使用更新页面内容

$('page_id').trigger('pagecreate');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多