2016-02-23 24 views
0

我開發了一個系統,以便我的客戶端可以通過php表單將條目添加到mysql表中。他希望能夠在任何給定時間排序(或重新排列)網站上顯示的條目的順序。我無法以任何特定順序顯示它,他必須能夠隨意自定義它。我不知道如何以用戶友好的方式來做到這一點。我找到的解決方案基於類別(即:按字母順序排列,按ID編號排序等),這是行不通的,我需要它是100%可定製的,如果他只想移動一個他需要的項目至。任何想法將不勝感激。允許用戶使用mysql重新安排表數據php

謝謝!

+0

這裏是[提示](https://jqueryui.com/sortable/) – roullie

+1

[如何對從MySQL調用的HTML表格行進行排序]的可能重複(http://stackoverflow.com/questions/3489783/how-to-sort-rows-of-html-table-that-c​​alled-from-mysql) –

+0

@roullie這實際上非常整潔,但不會有人能夠重新訂購物品,包括訪客? – JackPirate

回答

0

你可以使用jQuery UI或滾動你自己的分類器讓用戶拖放。如果您需要保存,則可以使用ajax將結果順序發送回服務器。

此解決方案除了您提到的其他搜索選項之外還有效,例如按字母排序。

現場演示:https://plnkr.co/edit/8YIkN6idxc0d2cH4TbTf?p=preview

<!DOCTYPE html> 
<html> 

<head> 
    <script data-require="[email protected]" data-semver="2.1.3" src="https://code.jquery.com/jquery-2.1.3.min.js"></script> 
    <script src="https://code.jquery.com/ui/1.12.0-beta.1/jquery-ui.min.js"></script> 
    <style> 
    table, 
    th, 
    td { 
     text-align: center; 
    } 
    </style> 
</head> 

<body> 
    <h2>Drag and drop a row</h2> 
    <table style="width: 500px;"> 
    <thead> 

    </thead> 
    <tbody> 
     <tr> 
     <td style="background-color: #41CCFA;">1</td> 
     </tr> 
     <tr> 
     <td style="background-color: #FA6F41;">2</td> 
     </tr> 
     <tr> 
     <td style="background-color: #3DFFA8;">3</td> 
     </tr> 
     <tr> 
     <td style="background-color: #70FA41;">4</td> 
     </tr> 
     <tr> 
     <td style="background-color: #FA4441;">5</td> 
     </tr> 
    </tbody> 
    </table> 

    <script> 
    var help = function(e, tr) { 
     var trch = tr.children(); 
     var trcl = tr.clone(); 
     trcl.children().each(function(i) { 
      $(this).width(trch.eq(i).width()); 
     }); 
     return trcl; 
     }, 
     updateI = function(e, ui) { 
     $('tr td:first-child', ui.item.parent()).each(function(i) { 
      $(this).html(i + 1); 
     }); 
     }; 

    $("tbody").sortable({ 
     helper: help, 
     stop: updateI 
    }).disableSelection(); 
    </script> 
</body> 

</html> 

新注: 我注意到你提到不想讓一般用戶使用該功能的意見。您可以圍繞該代碼添加一個檢查像

if (user.authenticated) { 
    ... code to make rows draggable 
} 

你會開關,無論你在你的代碼知道用戶使用user.authenticated被允許使用拖放。

無論您最終使用哪種解決方案,請在保存任何行順序更改之前再次檢查服務器上的用戶身份驗證。

+0

謝謝!這將有助於解決大部分問題。 – JackPirate