riktar / jkanban

Vanilla Javascript plugin for manage kanban boards
https://www.riccardotartaglia.it/jkanban/
Apache License 2.0
1.06k stars 296 forks source link

addElement API function doesn't insert at desired position #147

Open bairog opened 2 years ago

bairog commented 2 years ago

Hello. According to docs addElement API function adds element in the board with ID boardID, element is the standard format. If position is set, inserts at position starting from 0. I've tried the following code but element still insterted at the very bottom:

KanbanTest.addElement(sourceBoardId, {
        id: itemId,
        title: itemText
    }, 1);

123 Sample application - https://jsfiddle.net/bairog/wtha2d3f/79/

Is that a bug or there is a mistake in my code? Thank you in advance.

xscode-auto-reply[bot] commented 2 years ago

Thanks for opening a new issue. The team has been notified and will review it as soon as possible. For urgent issues and priority support, visit https://xscode.com/riktar/jkanban

marcosrocha85 commented 2 years ago

I tried to reproduce your issue, but it worked fine locally. I noticed that https://cdn.jsdelivr.net/npm/jkanban@1.3.1/dist/jkanban.js is using "appendChild" instead of "insertBefore". Maybe that version is not properly compiled. @riktar can you confirm that latest dist files are correct?

bairog commented 2 years ago

BTW. To return draggedItem to the original position on sourceBoard (if user clicked Cancel drag button) I need to determine draggedItem index on the sourceBoard. For today I use the following code:

function boardDragEl(jKanban, el, source) {
    var sourceBoardId = jKanban.getParentBoardID(el.dataset.eid);
    var sourceBoardItems = Array.from(jKanban.getBoardElements(sourceBoardId));
    sourceBoardDraggedItemIndex = sourceBoardItems.indexOf(el);
}

Is that piece of code fine or may there are some optimizations/shortcuts (I'm not so familiar with jKanban API yet)?

marcosrocha85 commented 2 years ago

As jKanban uses and manipulates DOM elements. You can use:

dragEl: function(el, source) {
    var sourceBoardId = source.parentElement.dataset.id;
    var sourceBoardItems = Array.from(jKanban.getBoardElements(sourceBoardId));
    sourceBoardDraggedItemIndex = sourceBoardItems.indexOf(el);
}

But for instance that is the best way to get an element position from a board. 🙁

bairog commented 2 years ago

I tried to reproduce your issue, but it worked fine locally. I noticed that https://cdn.jsdelivr.net/npm/jkanban@1.3.1/dist/jkanban.js is using "appendChild" instead of "insertBefore". Maybe that version is not properly compiled.

Just tested with dist\jkanban.min.css and dist\jkanban.min.js files from this repo and they work as expected. Looks like to jsdelivr files really incorrect. @riktar looks like jsdelivr files reupload is needed.. Thank you in advance.