2009-10-21 6 views
40

मेरे पास है:jQuery यूआई क्रमबद्ध, अद्यतन घटना में वर्तमान स्थान और नया स्थान कैसे निर्धारित करें?

<ul id="sortableList"> 
    <li>item 1</li> 
    <li>item 2</li> 
    <li>item 3</li> 
</ul> 

मैं update: function(event, ui) { } में तार लेकिन यकीन है कि तत्व की मूल और नई स्थिति पाने के लिए नहीं कर रहा हूँ है। यदि मैं आइटम 1 को ऊपर आइटम 1 से ऊपर ले जाता हूं, तो मैं मूल स्थिति (0 आधारित इंडेक्स) और आइटम 3 की नई स्थिति होना चाहता हूं।

+3

रिचर्ड से पिछले जवाब upvotes की कम से कम संख्या के साथ सबसे अच्छा और साफ समाधान है, खासकर जब आप कर रहे हैं AngularJS या किसी अन्य एमवीसी फ्रेमवर्क का उपयोग करके जहां आप अपने नियंत्रक के अंदर एक कस्टम विशेषता नहीं पढ़ना चाहते हैं! – Whizkid747

उत्तर

79
$('#sortable').sortable({ 
    start: function(e, ui) { 
     // creates a temporary attribute on the element with the old index 
     $(this).attr('data-previndex', ui.item.index()); 
    }, 
    update: function(e, ui) { 
     // gets the new and old index then removes the temporary attribute 
     var newIndex = ui.item.index(); 
     var oldIndex = $(this).attr('data-previndex'); 
     $(this).removeAttr('data-previndex'); 
    } 
}); 
+0

उत्पन्न करता है जो मैं खोज रहा था। धन्यवाद! – gsharp

+0

आप 'ui.item.data ('previndex') ' – adamj

9

पुरानी और नई स्थिति की जांच करने के लिए आपके पास कई संभावनाएं हैं। मैं उन्हें सरणी में डाल दूंगा।

$('#sortable').sortable({ 
    start: function(e, ui) { 
     // puts the old positions into array before sorting 
     var old_position = $(this).sortable('toArray'); 
    }, 
    update: function(event, ui) { 
     // grabs the new positions now that we've finished sorting 
     var new_position = $(this).sortable('toArray'); 
    } 
}); 

और फिर आप आसानी से जो कुछ भी चाहते हैं उसे निकाल सकते हैं।

+0

सूची आइटम में आईडी होना चाहिए। अन्यथा यह खाली सरणी – Atara

5

मैं एक ही मुद्दे का उत्तर ढूंढ रहा था। फ्रेंकी ने जो योगदान दिया, उसके आधार पर, मैं शुरुआत और अंत दोनों "आदेश" प्राप्त करने में सक्षम था। मैं वर का उपयोग कर चर गुंजाइश के साथ एक मुद्दा था, तो मैं बस .data() के बजाय स्थानीय वार्स के रूप में उन्हें संग्रहीत:

$(this).data("old_position",$(this).sortable("toArray")) 

और

$(this).data("new_position",$(this).sortable("toArray")) 
अब

आप इसे इस तरह से कॉल कर सकते हैं (अद्यतन/अंत कार्यों से):

console.log($(this).data("old_position")) 
console.log($(this).data("new_position")) 

क्रेडिट अभी भी फ्रेंकी को जाता है :)

15

जब अद्यतन फ़ंक्शन को लागू किया जाता है तो ui.item.sortable को अद्यतन नहीं किया गया है, हालांकि UI तत्व दृष्टि से स्थानांतरित हो गया है।
यह आपको पुरानी स्थिति और नई स्थिति पाने के लिए अद्यतन फ़ंक्शन में अनुमति देता है।

$('#sortable').sortable({  
     update: function(e, ui) { 
      // ui.item.sortable is the model but it is not updated until after update 
      var oldIndex = ui.item.sortable.index; 

      // new Index because the ui.item is the node and the visual element has been reordered 
      var newIndex = ui.item.index(); 
     }  
}); 
+0

का उपयोग कर सकते हैं यह सबसे अच्छा जवाब और सबसे साफ समाधान है! धन्यवाद रिचर्ड! – Whizkid747

+4

jQuery 1.7, jQuery UI 1.8 पर काम नहीं करता है - हालांकि यह समाधान बहुत ही आशाजनक लगता है – user1702401

+0

विशिष्ट होने के लिए: ui.item.sortable.index मौजूद नहीं है; और ui.item.sortable()। अनुक्रमणिका() सभी अद्यतन सूचकांक लौटाता है। – user1702401

3

यह मैं

$('#sortable').sortable({ 
start: function(e, ui) { 
    // puts the old positions into array before sorting 
    var old_position = ui.item.index(); 
}, 
update: function(event, ui) { 
    // grabs the new positions now that we've finished sorting 
    var new_position = ui.item.index(); 
} 
}); 
+0

प्राप्त करने के लिए 'ui.item.sortable.dropindex' का उपयोग करना शुरू किया यह मेरे लिए काम किया! यह ध्यान देने योग्य है कि अगर आपको अद्यतन() के भीतर old_position को संदर्भित करने की आवश्यकता है तो इसे क्रमबद्ध करने से पहले घोषित करें। – nick

1

यह मेरे लिए काम करता लिए काम किया,

$('#app').sortable({ 
    handle: '.handle', 

    start: function(evt, ui){ 
     $(ui.item).data('old-ndex' , ui.item.index()); 
    }, 

    update: function(evt, ui) { 
     var old_index = $(ui.item).data('old-ndex'); 
     var new_index = ui.item.index(); 

     alert('old_index -'+old_index+' new index -'+new_index); 

    } 
}); 
संबंधित मुद्दे