Hi I want to use a sortable accordion (http://jqueryui.com/accordion/#sortable ) together with the knockout-js "data-bind: foreach"-loop.
For each loop my will need a unique "id", but how do I generate that for the jQuery to process?
Knockoutcode:
self.Tasks = ko.observableArray(); // has TaskId, Taskname, Description...
HTML-ACCORDIAN:
<div id="accordion" data-bind="foreach: Tasks">
<div id="THIS HAS TO BE DYNAMICALLY GENERATED???" class="group">
<h3><b><span data-bind="text: Taskname"></span></b></h3>
<div>
<textarea name="Description" data-bind="value: Description"></textarea>
</div>
</div>
</div>
The javascript is:
$(function() {
$( "#accordion" )
.accordion({
header: "> div > h3"
})
.sortable({
axis: "y",
handle: "h3",
stop: function( event, ui ) {
var items=[];
ui.item.siblings().andSelf().each( function(){
//compare data('index') and the real index
if($(this).data('index')!=$(this).index()){
items.push(this.id);
}
});
// IE doesn't register the blur when sorting
// so trigger focusout handlers to remove .ui-state-focus
ui.item.children( "h3" ).triggerHandler( "focusout" );
// Print out the sequence of tasks for further processing
if(items.length) $("#sequence").text(items.join(','));
ui.item.parent().trigger('stop');
}
})
.on('stop',function(){
$(this).siblings().andSelf().each(function(i){
$(this).data('index',i);
});
})
.trigger('stop');
});
A part of me tells me this should be easy, but I´m obviously to thick to figure it out. Anybody have a good idea?