2013-08-18 23 views
-1

我有两个JQuery数据表(datatables.net),一个带有消息,第二个带有联系人。我想在不刷新页面的情况下加载它们,即如果我单击link1我会显示消息,如果我单击link2我会收到消息表disapear和联系人显示。我可以通过将表放入不同的div中,并在这些上使用.show和.hide。但是如果我有20个不同的表格,我需要一次显示一个表格。在客户端加载全部20个表并隐藏这些表可能会占用很多内存。我有点卡住和溢出)。在两个JQuery数据表之间切换

我曾尝试以下:

<a href='#' onclick='test_widget();return false;' 

<script type="text/javascript"> 
function test_widget() 
{ $.ajax({ 
     type:'POST', 
     url:'contacts.php', 
     data:$('#test_widget').serialize(), 
     success:function(data) 
     { $('#test_widget').html(data); 
     }, 
    error:function(XMLHttpRequest, textStatus, errorThrown) 
     { $("#test_widget").html(errorThrown + ': ' + this.url); 
     }, 
    dataType:'html' 
    }); 
} 
</script> 
<div id='test_widget'></div> 

然后是contacts.php:

<table class="table table-bordered table-striped checked-in has-checkbox" id="dtable"> 
<thead> 
    <tr> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Middle Name</th> 
    <th>Email</th> 
    <th>Phone</th> 
    <th> </th> 
    </tr> 
</thead> 
<tbody> 
    <? 
$res=mysql_query('SELECT * FROM people ORDER BY lname LIMIT 1000'); 
while($row=mysql_fetch_array($res)){ 

    ?> 

    <tr class="gradeA"> 
    <td><?=$row['lname']?> </td> 
    <td><?=$row['fname']?> </td> 
    <td><?=$row['patronymic']?> </td> 
    <td class="center"><?=$row['email']?> </td> 
    <td class="center"><?=$row['phones']?> </td> 
    <td><input type="checkbox"></td> 
</tr> 
<? } ?> 

</tbody> 
</table> 

回答

0

什么后端您使用的这种语言? 如果您需要一次只显示一个表格,并且担心隐藏(display:none)多个div,那么只需发送一个AJAX请求后端并呈现相应的表格。

+0

我正在使用PHP。服务器端PHP脚本连接到mysql数据库,获取数据并呈现该dable。我试图用Ajax POST它的输出,表显示了它,但它不再是Ajax表,它只是HTML。 – user164863

+0

我已添加代码。 – user164863

+0

对不起,我只是一遍又一遍,它的工作!谢谢。我仍然遇到的问题 - 可以用滑块或XChart完成吗? – user164863