How to order pages in CMS?
I wrote a simple CMS for one of my clients which does specifically what he needs (without the bloat of mainstream content management systems he would never use).
The CMS enables him to create and manage two types of content: pages and news (short messages that show in the sidebar).
The pages are displayed in the navigation menu of the website sorted alphabetically. My client would like to be able to sort the pages himself in a custom order.
What is the best way to do this programatically? I'm thinking of adding a column called "priority" to the pages table. It will be a number and the pages will be sorted based on this number eith开发者_StackOverflow社区er ascendingly or descendingly. And the client could edit this number for every page?
Just curious what are your thoughts about this? Is there a better way to do this? Some design pattern for this purpose?
I personally use the jQuery ui sortable plugin. The client can drag and drop the items from a list, changing the order.
Next I use jQuery again to loop through the list of items, compile CSV list of id's and send it to the server.
var imageList;
function init() {
imageList = $("#imagelist");
if(imageList.length > 0) {
imageList.sortable({stop:onStopSorting});
}
}
function onStopSorting() {
var datas = imageList.find("span.data");
var result = new Array();
for(var i = 0; i < datas.length; i++) {
result.push(datas.eq(i).html());
}
$("#imageSort").val(result.join(","));
}
and on the server:
if(isset($_REQUEST["imageSort"]) && $_REQUEST["imageSort"] != "") {
$imageIdList = explode(",", $_REQUEST["imageSort"]);
for($i = 0; $i < count($imageIdList); $i++) {
$this->setImagePriority($imageIdList[$i], $i+1);
}
}
It might be more "quick 'n dirty" than a nice Design Pattern, but it works.
If I was on a budget, I'd give the user a field to edit or otherwise control. The easy method is an integer field where the values can be duplicated - you let them pick the number and type it manually into a field. Obviously, this is a quick and dirty fix - if the users sets up a big sequence of pages, then wants to add something to the middle in a specific slot, that's a problem - the priority value is equal, so then you have to sort on something else.
Using float values is another possibility. You don't let the user see the number, in this case. If the user wants to put item C between items A and B, you set the priority of C=(A+B)/2
knowing that the decimal precision will let them do this kind of operation many (many) times over before you run out of precision. You can always make a maintenance script to get the data in order and assign new values - this could be run once a month or even once a year.
It's not fancy, but it's cheap to implement.
精彩评论