rniemeyer / knockout-sortable

A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality
MIT License
547 stars 128 forks source link

If/Ifnot virtual elements don't work #158

Open ricardobrandao opened 8 years ago

ricardobrandao commented 8 years ago

If we use the if/ifnot binding inside a virtual element the binding does not work. All works fine if we use container elements (e.g. < div >).

A sample can be seen here.

ricardobrandao commented 8 years ago

@rniemeyer I'm facing a problem because of this issue (and I believe that fixing this issue might help solving other problems like the one in #163).

The problem is that since I cannot use if/ifnot virtual elements, the hidden rows are still showing in the DOM and therefore they are processed when sorting the rows.

A fiddle representing this problem can be found here. In this scenario, when we try to move row 1 to row 4 we actually end up moving row 2 to row 6 because we are counting the hidden rows which ends up messing with the table.

rniemeyer commented 8 years ago

@ricardobrandao - there needs to be a single element for each sortable thing inside your container. Have you tried maybe something like this: https://jsfiddle.net/rniemeyer/664134y4/

ricardobrandao commented 8 years ago

Hi @rniemeyer, thanks for the reply.

I am unable to fully understand what you mean with "single element for each sortable thing". Firstly, because in my container I only have a tr element (this was my first approach but didn't worked as expected, see my original comment). Moreover, I can have, for instance, multiple div elements inside a table cell.

I was trying not to do something like what you are suggesting because I want to apply different actions (e.g. bindings) and styling according to the row type (i.e. view only or editing mode) and it's easier to have two different tr elements (e.g. using templates).

rniemeyer commented 8 years ago

@ricardobrandao - I meant that your template should only have one top-level element defined or the index tracking while sorting gets messed up.

ricardobrandao commented 8 years ago

Yes, the problem is exactly that.

However I think this would work for my case if we could use the virtual DOM which would actually remove the extra rows. However, in order for that to work we need to enable the virtual DOM for the sortable plugin and probably it would require some refactoring.