Re: [問題] 自定排序

看板Ajax作者 (藍藍紫黃橘 粉灰白綠咖)時間14年前 (2011/06/08 16:26), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/2 (看更多)
你的問題不夠明確 有太多種解答了  不過既然你是在ajax版問 我就回答你ajax解法 http://jsfiddle.net/tp9Rz/ HTML: No        Order<br /> <input type="text" class="no" value="2"><input type="text" class="order" value=""><br /> <input type="text" class="no" value="11"><input type="text" class="order" value=""><br /> <input type="text" class="no" value="6"><input type="text" class="order" value=""><br /> <input type="text" class="no" value="3"><input type="text" class="order" value=""><br /> <input type="button" id="sort" value="new order"> JS: function sortNumber(a,b){ return a - b; } function orderSort(){ var _array = []; $(".no").each(function(){ _array.push(this.value); }); _array.sort(sortNumber); $(".order").each(function(){ var index = $(".order").index(this);//start from 0 var no = $(".no"); //find the current index of no in the sorted array var order = jQuery.inArray($(no[index]).val(), _array)+1; $(this).val(order); }); } $("#sort").click(function(){ orderSort(); }); ※ 引述《magicfx (去南半球度假)》之銘言: : 下面的 code 是假設資料庫的 idno(primary key) 是連續的且從1開始 : 例如: : idno order : ---- ----- : 1 2 : 2 3 : 3 1 : 4 5 : 5 4 : ---- : 不過,因為我把idno設為auto increment : 當某筆資料被刪除以後,idno就會變成不連續的,且不一定從1開始 : 例如: : idno : ---- : 2 : 3 : 5 : ---- : 請問我該怎麼修改,好讓order欄位可以從1開始且數字是連續的? : 例如: : idno order : ---- ----- : 2 1 : 3 2 : 5 3 : ---- ----- : <jQuery> : ======================================== : $(document).ready(function(){ : function slideout(){ : setTimeout(function(){ : $("#response").slideUp("slow", function () { : }); : }, 2000); : } : $("#response").hide(); : $(function() { : $("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() { : var order = $(this).sortable("serialize") + '&update=update'; : $.post("updateList.php", order, function(theResponse){ : $("#response").html(theResponse); : $("#response").slideDown('slow'); : slideout(); : }); : } : });});}); : ======================== : <HTML> : ======================== : <body> : <div id="container"> : <div id="list"> : <div id="response"> </div> : <ul> : <?php : include("connect.php"); : // $query = "SELECT id, text FROM dragdrop ORDER BY listorder ASC"; : $query = "SELECT id, name, type FROM project_list ORDER BY 'order' ASC"; : $result = mysql_query($query); : while($row = mysql_fetch_array($result, MYSQL_ASSOC)) : { : $id = stripslashes($row['id']); : $name = stripslashes($row['name']); : $type = stripslashes($row['type']); : ?> : <li id="arrayorder_<?php echo $id ?>"> <?php echo $name?> <?php echo $type; ?> : <div class="clear"></div> : </li> : <?php : } : ?> : </ul> : </div> : </div> : </body> : ========================== : updateList.php : ========================== : <?php : include("connect.php"); : $array = $_POST['arrayorder']; : if ($_POST['update'] == "update") { : $count = 1; : foreach ($array as $idval) { : $query = "UPDATE project_list SET 'order' = " . $count . " WHERE id = " : . $idval; : mysql_query($query) or die('Error, insert query failed'); : $count ++; : } : echo 'Updated!'; : } : ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.122.30.198

06/08 23:29, , 1F
謝謝!
06/08 23:29, 1F

06/08 23:30, , 2F
這個網頁真不錯
06/08 23:30, 2F
文章代碼(AID): #1Dxp8qPz (Ajax)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
1
2
文章代碼(AID): #1Dxp8qPz (Ajax)