Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [jQuery] sortable
Forum PHP.pl > Forum > XML, AJAX
PiotrN
Mam taki skrypt: http://www.shopdev.co.uk/blog/sortable-lis...sing-jquery-ui/
Nie wiem jak zmienić ul i li na div. Próbowałem zmieniać ale nie działało. Proszę o pomoc.
Lars_18
A poprawnie nazywasz te divy? W ostateczności możesz to zrobić na liście nieuporządkowaniej odpowiednio stylując ją CSSem, oczywiście zależy co chcesz w niej umieścić. Zobacz tutaj: http://jqueryui.com/demos/sortable/
A po drugie - pokaż nam swój kod.
PiotrN
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>jQuery UI Sortables</title>
  5. div {
  6. border:1px solid #DADADA;
  7. background-color:#EFEFEF;
  8. padding:3px 5px;
  9. margin-bottom:3px;
  10. margin-top:3px;
  11.  
  12. list-style-type:circle;
  13. font-family:Arial, Helvetica, sans-serif;
  14. color:#666666;
  15. font-size:0.8em;
  16. }
  17.  
  18. div:hover {
  19. background-color:#FFF;
  20. cursor:move;
  21. }
  22. <script src="/js/jquery-1.2.6.min.js" type="text/javascript"></script>
  23. <script src="/js/jquery-ui-1.5.1.packed.js" type="text/javascript"></script>
  24. <script src="/js/jquery.cookie.js" type="text/javascript"></script>
  25. <script type="text/javascript">
  26. /////////////////////////////////////////////////////////////////
  27. ///// EDIT THE FOLLOWING VARIABLE VALUES //////////////////////
  28. /////////////////////////////////////////////////////////////////
  29.  
  30. // set the list selector
  31. var setSelector = "#list1";
  32. // set the cookie name
  33. var setCookieName = "listOrder";
  34. // set the cookie expiry time (days):
  35. var setCookieExpiry = 7;
  36.  
  37. /////////////////////////////////////////////////////////////////
  38. ///// YOU PROBABLY WON'T NEED TO EDIT BELOW ///////////////////
  39. /////////////////////////////////////////////////////////////////
  40.  
  41. // function that writes the list order to a cookie
  42. function getOrder() {
  43. // save custom order to cookie
  44. $.cookie(setCookieName, $(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
  45. }
  46.  
  47. // function that restores the list order from a cookie
  48. function restoreOrder() {
  49. var list = $(setSelector);
  50. if (list == null) return
  51.  
  52. // fetch the cookie value (saved order)
  53. var cookie = $.cookie(setCookieName);
  54. if (!cookie) return;
  55.  
  56. // make array from saved order
  57. var IDs = cookie.split(",");
  58.  
  59. // fetch current order
  60. var items = list.sortable("toArray");
  61.  
  62. // make array from current order
  63. var rebuild = new Array();
  64. for ( var v=0, len=items.length; v<len; v++ ){
  65. rebuild[items[v]] = items[v];
  66. }
  67.  
  68. for (var i = 0, n = IDs.length; i < n; i++) {
  69.  
  70. // item id from saved order
  71. var itemID = IDs[i];
  72.  
  73. if (itemID in rebuild) {
  74.  
  75. // select item id from current order
  76. var item = rebuild[itemID];
  77.  
  78. // select the item according to current order
  79. var child = $("div.ui-sortable").children("#" + item);
  80.  
  81. // select the item according to the saved order
  82. var savedOrd = $("div.ui-sortable").children("#" + itemID);
  83.  
  84. // remove all the items
  85. child.remove();
  86.  
  87. // add the items in turn according to saved order
  88. // we need to filter here since the "ui-sortable"
  89. // class is applied to all ul elements and we
  90. // only want the very first! You can modify this
  91. // to support multiple lists - not tested!
  92. $("div.ui-sortable").filter(":first").append(savedOrd);
  93. }
  94. }
  95. }
  96.  
  97. // code executed when the document loads
  98. $(function() {
  99. // here, we allow the user to sort the items
  100. $(setSelector).sortable({
  101. axis: "y",
  102. cursor: "move",
  103. update: function() { getOrder(); }
  104. });
  105.  
  106. // here, we reload the saved order
  107. restoreOrder();
  108. });
  109. </head>
  110.  
  111.  
  112.  
  113. <div class="list">
  114. <div id="item-1">1</div>
  115. <div id="item-2">2</div>
  116. <div id="item-3">3</div>
  117. <div id="item-4">4</div>
  118. <div id="item-5">5</div>
  119. <div id="item-6">6</div>
  120. </div>
  121. </body>
  122. </html>
fleshgrinder
var setSelector = "#list1";

<div class="list">
PiotrN
Próbowałem do powyższego kodu dadać funkcje helper, ale nic z tego nie wyszło.
Chodzi mi o to, aby pojawiały się takie "podpowiedzi" - boxy z przerywaną linią, gdy zmienia się kolejność.
http://jqueryui.com/demos/sortable/#option-helper

Jestem początkujący w jQuery, póki co korzystam z slide i sortable i mam takie pytanie:
Czy poniżej zamieszczonych plików nie można zmienić na jakiś wspólny, czy np. jak użyje tego 1.2.6 do obu funkcji to będzie działać?
jquery-1.2.3.min.js
jquery-1.2.6.min.js

Bardzo proszę o pomoc.
everth
Czy tobie czasem nie chodzi o coś takiego? Co do bibliotek - zazwyczaj pluginy dobiera się pod biblioteki a nie odwrotnie. Stabilną wersją Jquery na dziś jet 1.4.2 i zaleca się używać najnowszych stabilnych wersji. Odpowiadająca mu Jquery UI to 1.8.2 Zaktualizuj sobie te biblioteki. A @fleshgrinder ma rację, nie "<div class="list">", tylko jeśli już to "<div id='list'>".
PiotrN
tak, o to mi chodziło. Jak dodać tą funkcję do kodu zamieszczonego powyżej.
Proszę o pomoc, albo o jakąś alternatywę z ciaseczkami.

Już sobie poradziłem z placeholder, tylko mam problem z css.



  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>jQuery UI Sortables</title>
  5. div#list {
  6. border:1px solid #DADADA;
  7. background-color:#EFEFEF;
  8. padding:3px 5px;
  9. margin-bottom:3px;
  10. margin-top:3px;
  11.  
  12. list-style-type:circle;
  13. font-family:Arial, Helvetica, sans-serif;
  14. color:#000000;
  15. font-size:0.8em;
  16. }
  17.  
  18. div:hover {
  19. background-color:#FFF;
  20. cursor:move;
  21. }
  22.  
  23. .sortHelper {
  24. border: 2px dotted #666;
  25. margin: 2px 0px 2px 0px;
  26. padding-bottom:0px;
  27. padding-top:0px;
  28. background-color: #ffffff;
  29. width: auto !important;
  30. }
  31.  
  32. <script src="/js/jquery-1.4.2.min.js" type="text/javascript"></script>
  33. <script src="/js/jquery-ui-1.8.2.custom.min.js" type="text/javascript"></script>
  34. <script src="/js/jquery.cookie.js" type="text/javascript"></script>
  35. <script type="text/javascript">
  36. /////////////////////////////////////////////////////////////////
  37. ///// EDIT THE FOLLOWING VARIABLE VALUES //////////////////////
  38. /////////////////////////////////////////////////////////////////
  39.  
  40. // set the list selector
  41. var setSelector = "#list";
  42. // set the cookie name
  43. var setCookieName = "listOrder";
  44. // set the cookie expiry time (days):
  45. var setCookieExpiry = 7;
  46.  
  47. /////////////////////////////////////////////////////////////////
  48. ///// YOU PROBABLY WON'T NEED TO EDIT BELOW ///////////////////
  49. /////////////////////////////////////////////////////////////////
  50.  
  51. //getter
  52. var placeholder = $( ".sortHelper" ).sortable( "option", "placeholder" );
  53. //setter
  54. $( ".sortHelper" ).sortable( "option", "placeholder", 'ui-state-highlight' );
  55.  
  56. // function that writes the list order to a cookie
  57. function getOrder() {
  58. // save custom order to cookie
  59. $.cookie(setCookieName, $(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
  60. }
  61.  
  62. // function that restores the list order from a cookie
  63. function restoreOrder() {
  64. var list = $(setSelector);
  65. if (list == null) return
  66.  
  67. // fetch the cookie value (saved order)
  68. var cookie = $.cookie(setCookieName);
  69. if (!cookie) return;
  70.  
  71. // make array from saved order
  72. var IDs = cookie.split(",");
  73.  
  74. // fetch current order
  75. var items = list.sortable("toArray");
  76.  
  77. // make array from current order
  78. var rebuild = new Array();
  79. for ( var v=0, len=items.length; v<len; v++ ){
  80. rebuild[items[v]] = items[v];
  81. }
  82.  
  83. for (var i = 0, n = IDs.length; i < n; i++) {
  84.  
  85. // item id from saved order
  86. var itemID = IDs[i];
  87.  
  88. if (itemID in rebuild) {
  89.  
  90. // select item id from current order
  91. var item = rebuild[itemID];
  92.  
  93. // select the item according to current order
  94. var child = $("div.ui-sortable").children("#" + item);
  95.  
  96. // select the item according to the saved order
  97. var savedOrd = $("div.ui-sortable").children("#" + itemID);
  98.  
  99. // remove all the items
  100. child.remove();
  101.  
  102. // add the items in turn according to saved order
  103. // we need to filter here since the "ui-sortable"
  104. // class is applied to all ul elements and we
  105. // only want the very first! You can modify this
  106. // to support multiple lists - not tested!
  107. $("div.ui-sortable").filter(":first").append(savedOrd);
  108. }
  109. }
  110. }
  111.  
  112. // code executed when the document loads
  113. $(function() {
  114. // here, we allow the user to sort the items
  115.  
  116. $(setSelector).sortable({
  117. axis: "y",
  118. cursor: "move",
  119. placeholder: "sortHelper",
  120. update: function() { getOrder(); }
  121. });
  122.  
  123. // here, we reload the saved order
  124. restoreOrder();
  125. });
  126. </head>
  127.  
  128. <div id="list">
  129. <div id="item-1">List Item 1</div>
  130. <div id="item-2">List Item 2</div>
  131. <div id="item-3">List Item 3</div>
  132. <div id="item-4">List Item 4</div>
  133. <div id="item-5">List Item 5</div>
  134. <div id="item-6">List Item 6</div>
  135. </div>
  136. </body>
  137. </html>


Co zrobić aby placeholder był pod całym tym boksem - List item-1 itd ? Niezależnie ile tekstu jest w tym boxie, bo przy jednej "linii" tekstu to wiem że wtedy wystarczą height albo padding w sortHelper.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.