【问题标题】:Issue in footable while adding dynamic data添加动态数据时出现问题
【发布时间】:2013-04-24 03:06:39
【问题描述】:

我需要一些关于 jQuery Mobile Foo Table 的帮助。 我正在表格中动态添加数据。

HTML:

    <table id="tblSRNDetails" class="footable">
        <thead>
            <tr>
                <th data-class="expand">SRN</th>
                <th >Failure Date</th>  
                <th >Complaint Report Date</th>                 
                <th>Promised Date</th>  
                <th >Customer Name</th>
                <th >Log Time</th>
                <th >Create FSR</th>    
                <th  data-hide="phone,tablet">Days Open</th>        
                <th  data-hide="phone,tablet">SRN Allocated Time</th>   
                <th  data-hide="phone,tablet"> SRN Status</th>  
                <th  data-hide="phone,tablet"> ESN Number</th>  
                <th  data-hide="phone,tablet"> Request Type</th>    
                <th  data-hide="phone,tablet">Service Request Details</th>                          
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

js代码:

$("#page-id").live('pagebeforeshow', function() {
    console.log("Page before show");
    $("#tblSRNDetails > tbody tr").remove();
    for (var indx = 0; indx < 2; indx++ )
    {
        $("#tblSRNDetails > tbody").append("<tr>"+
        "<td>Name</td>"+
        "<td>failureDate</td>"+
        "<td>complaintReportDate</td>"+
        "<td>promisedDate</td>"+
        "<td>custName</td>"+
        "<td><a href='#'><b>Log Time</b></a></td>"+
        "<td><b>Create FSR</b></td>"+
        "<td>daysOpen</td>"+
        "<td>allocatedTime</td>"+
        "<td>srn_status</td>"+
        "<td>ESNNumber</td>"+
        "<td>requestType</td>"+
        "<td>customerComplaint</td>"+
        "</tr>");   
    }
    $('#tblSRNDetails').footable();
});

第一次打开时,此 FooTable 会正确应用。如果我单击主页按钮并返回,然后再次进入该页面,则 FooTable 未正确应用。

截图:

所以我当时面临的问题包括:

  1. 显示隐藏字段。 (意味着不应用Footable): 在设备中更改方向两次后,此问题得到解决。

  2. 第一个字段不再包含“数据展开”按钮(表示未应用Footable):

我认为问题在于我要删除旧行并添加新行。我试过没有给删除电话。那时,旧行正在正确显示。如屏幕截图所示,新附加的字段存在问题。

有人可以帮我解决这个问题吗?

P.S: 我在 android webview 中渲染这个。同样的问题也在浏览器中重现。

【问题讨论】:

  • 我能知道投反对票的原因吗?在作为问题发布之前,我已经尽力在谷歌上搜索
  • 您使用的是哪个版本的 jquery 和 JQM? .live 已折旧并替换为 .on。另外,请尝试在附加项目 $('[data-role=page]').trigger('create') 后添加它。
  • @Omar jQM 版本 1.2.0。 .on 没用。我尝试在附加数据后添加$('[data-role=page]').trigger('create')。但这并没有改变什么。
  • 如果您使用的是 jquery 1.8.3 及更高版本,.on 应该可以工作。在$('#tblSRNDetails').footable(); 之后添加trigger('create') 了吗?试试refresh 或pagecreate 而不是create。
  • 是的,@Omar。我在$('#tblSRNDetails').footable(); 之后添加了触发器并尝试刷新和页面创建两者。还是一样的输出。有什么办法可以去除脚感/刷新它?

标签: android jquery jquery-mobile jquery-plugins footable


【解决方案1】:

Foo table 是作为 jQuery 插件创建的,因此从未打算与 jQuery Mobile 一起使用。它只是另一个无法与 jQuery mobile 一起正常工作的插件。 Foo table 不知道如何处理 jQuery Mobile 页面切换。

只有在每次访问该页面时动态创建整个表格才能使其工作。在任何其他情况下,Foo table 将无法正常工作,因为页面已经具有增强的表格标记。这就是为什么每个 jQuery Mobile 小部件都有一个带有刷新属性的函数的原因。

这是一个工作示例:http://jsfiddle.net/Gajotres/PjmEL/

最后一件事,如果这对您来说不是一个好的解决方案,您应该切换到 dynamic table 的 jQuery Mobile 实现。

$(document).on('pageshow', '#index', function(){  
    $("#tblSRNDetails").remove();
    $('<table>').attr({'id':'tblSRNDetails','class':'footable'}).appendTo('[data-role="content"]');
        $("#tblSRNDetails").append(
            "<thead><tr>"+
            "<th data-class='expand'>SRN</th>"+
            "<th >Failure Date</th>"+
            "<th >Complaint Report Date</th>"+
            "<th>Promised Date</th>"+
            "<th >Customer Name</th>"+
            "<th >Log Time</th>"+
            "<th >Create FSR</th>"+
            "<th  data-hide='phone,tablet'>Days Open</th>"+
            "<th  data-hide='phone,tablet'>SRN Allocated Time</th>"+
            "<th  data-hide='phone,tablet'> SRN Status</th>"+
            "<th  data-hide='phone,tablet'> ESN Number</th>"+
            "<th  data-hide='phone,tablet'> Request Type</th>"+
            "<th  data-hide='phone,tablet'>Service Request Details</th>"+
            "</tr></thead>");  
    for (var indx = 0; indx < 2; indx++ )
    {
        $("#tblSRNDetails").append(
            "<tbody><tr>"+
            "<td>Name</td>"+
            "<td>failureDate</td>"+
            "<td>complaintReportDate</td>"+
            "<td>promisedDate</td>"+
            "<td>custName</td>"+
            "<td><a href='#'><b>Log Time</b></a></td>"+
            "<td><b>Create FSR</b></td>"+
            "<td>daysOpen</td>"+
            "<td>allocatedTime</td>"+
            "<td>srn_status</td>"+
            "<td>ESNNumber</td>"+
            "<td>requestType</td>"+
            "<td>customerComplaint</td>"+
            "</tr></tbody>");   
    }
    $('#tblSRNDetails').footable();
});

$(document).on('pagebeforeshow', '#second', function(){       

});

【讨论】:

  • 谢谢。我对 Foo Table 实现及其现在的工作进行了更改。 +1。
【解决方案2】:

我在我的 web 应用程序中遇到了同样的问题,我在 datatables.net 中使用回调时添加了重绘功能,它工作得非常好。

$('table').trigger('footable_redraw');

【讨论】:

    【解决方案3】:

    'Footable' 与'jQuery Mobile' 配合得很好。我将用我的代码 sn-ps 来解释它的用法。

    Footable 初始化,

    function initFootable() {
        $(function () {
            $('.footable').footable({ //.footable is my class for table
                breakpoints: {
                    phone: 480, //Breakpoint width for phones
                    tablet: 1024 //Breakpoint width for tablets
                }
            });
        });
    }
    

    我正在使用 jQuery AJAX 调用来获取新的表数据并使用模板方法将它们添加到表中。我正在使用 handlebars.js 进行模板化。 (请记住,这种模板方法不是强制性的。您可以使用自己的方法将新行追加到表中。)

    因此,在您将新数据更新到表后,您需要触发 footable 以重新初始化。这里是代码sn-p,

    function updateFootable() {
        var paramTable = $('.footable');
        paramTable.footable();
        paramTable.trigger('footable_initialize'); //Reinitialize
        paramTable.trigger('footable_redraw'); //Redraw the table
        paramTable.trigger('footable_resize'); //Resize the table
    }
    

    这里的所有触发器都不是必需的。根据您在重新初始化/重绘/调整大小时遇到​​的问题,检查并确认调用哪个触发器对您来说足够了。

    就是这样,您现在在 jQuery Mobile 中使用 footable 不会有任何问题。

    重要提示: 但是,在调用 updateFootable() 时必须小心,如果包含表格的 div 或包含表格的页面被隐藏,则表格将不会响应地调整大小。您必须确保在调用更新表时该表是可见的。

    链接:

    这里是可触发触发器的文档 - http://fooplugins.com/footable/demos/triggers.htm#docs

    请务必阅读适合的文档 - http://fooplugins.com/footable-demos/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 2011-06-17
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多