2016-03-15 74 views
0

如何使用AJAX对从数据库中显示的表进行排序? 我见过几个例子,但是他们的MVC和我正在做的完全不一样,我完全迷失了。有人可以为此提供一些有用的资源吗?谢谢。这是我的代码。使用Ajax对MVC体系结构中的表进行排序

查看表

<table> 
<thead> 
<tr> 
    <th>CustomerID</th> 
    <th>FirstName</th> 
    <th>LastName</th> 
    <th>Address</th> 
    <th>Street</th> 
    <th>County</th> 
    <th>Mobile</th> 
    <th>Email</th> 
</tr> 
</thead> 
<?php foreach ($customers as $customer) : ?> 
    <tbody> 
    <tr> 
     <td><?php echo $customer['customerID']; ?></td> 
     <td><?php echo $customer['fname']; ?></td> 
     <td><?php echo $customer['lname']; ?></td> 
     <td><?php echo $customer['address']; ?></td> 
     <td><?php echo $customer['street']; ?></td> 
     <td><?php echo $customer['county']; ?></td> 
     <td><?php echo $customer['mobile']; ?></td> 
     <td><?php echo $customer['email']; ?></td> 
    </tr> 
    </tbody> 
<?php endforeach; ?> 
</table> 

的index.php行动呼吁

if ($action == 'view_customers') { 

// call functions from the MODEL to get data from DB 
$customers = get_customers(); 


include('./view/view_customers.php'); 

} 

模型

function get_customers(){ 
    global $db; 
    $query = 'SELECT * FROM customers'; 
    $statement = $db->prepare($query); 
    $statement->execute(); 
    return $statement; 
} 

回答

0

您可以遵循这些简单的步骤:

  • th(CustomerID,FirstName,...)添加javascript侦听器。当此元素上的用户点击您创建的特殊的URL的AJAX调用获取数据
  • 添加URL获取数据,I,E /getCustomers?OrderByColumn=CustomerID&OrderDirection=Desc其中OrderByColumn排序定义列和OrderDirection在简单的情况下,该网址会检索所有定义方向
  • HTML表,你只需更换旧表新
  • 在更复杂的情况下,该网址将检索与数据JSON和您与您的JS代码处理它
+0

感谢,这一切才有意义。我在th标签上使用onclick事件。我只是不知道如何实现ajax等。我已经展示的所有东西是如何使用按钮使ajax显示某些东西。 – lukas13x