-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No easy way to add/remove folds after construction #16
Comments
When you say "init recently added folds", what are you referring to exactly? |
@Alhadis, sorry, I explained it a bit unclear. |
Short answer: no it's not possible because it shouldn't be needed. Accordions can be disabled and reenabled programmatically after construction, which is (almost) the same as destroying and recreating it. What you're experiencing is a limitation caused by a pretty stupid oversight on my behalf... one I'm surprised I never noticed earlier — there's no way of dynamically adding or removing accordion folds. I'll need to fix this. In the meantime, you can use this ugly and illogical workaround: Click to show// Usage:
let el = document.createElement("li");
el.innerHTML = "<h3>Foo</h3><div>Bar</div>";
addFold(el, document.querySelector(".accordion"));
function addFold(element, accordion){
if(!(accordion instanceof Accordion))
accordion = Accordion.getAccordion(accordion);
const prev = accordion.folds[accordion.folds.length - 1];
const fold = new prev.constructor(accordion, element);
accordion.folds.push(fold);
prev.nextFold = fold;
fold.previousFold = prev;
if(element.parentElement !== accordion.el)
accordion.el.appendChild(element);
accordion.update();
return fold;
}
function removeFold(fold){
if(fold instanceof HTMLElement)
fold = Accordion.getFold(fold);
const {parentElement} = fold.el;
parentElement && parentElement.removeChild(fold.el);
const {folds} = fold.accordion;
const index = folds.indexOf(fold);
if(~index){
const prev = folds[index - 1];
const next = folds[index + 1];
folds.splice(index, 1);
prev.nextFold = next;
if(next)
next.previousFold = prev;
}
fold.accordion.update();
} As for destroying an |
Hi! Is it possible to fully destroy and init again accordion with the same container? (In case if items there have been changed)
When I call just
update
, it doesn't init recently added folds, and when I init a new accordion, I see the all previous are not deleted.The text was updated successfully, but these errors were encountered: