Greate question, Iâm trying to reproduce the sliding animation on a click too . Unfortunatly I did not figured out how to do it. Did you find a way ?
Thanks for posting I have been trying to find a solution to the same issue as well, I tried out your sample of code however on the click event for toggling the item-slide I got this error: âORIGINAL EXCEPTION: TypeError: Cannot read property âsetCssClassâ of undefinedâ. Would you happen to know a solution to this and if possible would you also share the html used to produce your results? Thank you!
This has been so much help, the problem was as you mentioned that I didnât have the correct parameters in my html. It does work now! Thanks again for the help I am sure that this will be useful to others as well!
First of all, thanks for the code @Mxman06. Your answer helped me a lot.
But, with the changes on Ionic2 rc.0, I had to change my code to work on the new background code. For this, I just changed the âsetCssClassâ to the new method âsetElementClassâ, and âsetCssStyleâ to âsetElementStyleâ. The solution is in the code below.
public open(itemSlide: ItemSliding, item: Item) {
// reproduce the slide on the click
itemSlide.setElementClass("active-sliding", true);
itemSlide.setElementClass("active-slide", true);
itemSlide.setElementClass("active-options-right", true);
item.setElementStyle("transform", "translate3d(-241px, 0px, 0px)");
}
public close(item: ItemSliding) {
item.close();
item.setElementClass("active-sliding", false);
item.setElementClass("active-slide", false);
item.setElementClass("active-options-right", false);
}
This is great @Mxman06 . But I have one issue on close. Unlike swipe that automatically closes on tapping elsewhere within the slider, this one does not automatically close. Plus after my slider is swiped open(via code/manually) the components inside the slider become untappable, ie everything other than ion-item-option buttons are disabled. How do I close this open slider on tapping outside, on the non-enabled part?
What is happening in ionic? ionic actually add a gesture listener to the slider. and the listener calls the slider to change the UI accordingly.
I guess the approach of changing the CSS can simulate the effect but it is quite tricky if ionic changes itâs CSS class nameâŚ
Also the reason why it wont be able to hide the options automatically because of the gesture listenerâs variable is null, therefore, it cannot âcloseâ the slider.
Anyway, anyone who is interested in how it work, search for [item-sliding-guesture.js] and [item-sliding.js] for more detail.
I hope this can make a good start, looking for the experts enhance the implementation.
Ok, lets go straight to the SolutionsâŚ
in the html.
public open(itemSlide: ItemSliding, item: Item, $event) {
itemSlide.startSliding(360); //any number can do
itemSlide.moveSliding(330); // any number small than the previous one
this.selectedItemSlide = itemSlide;
setTimeout(()=>{
itemSlide.moveSliding(320); // any number smaller than the previous one
itemSlide.endSliding(0.3); // you can mock anything less than 0.3
}, 100); // the reason why i delay 0.1s, that is because i need to give sometime for the logic inside moveSliding to set a flag to false, in order to make it automatically slide.
}
public close($event) {
if (this.selectedItemSlide) {
this.selectedItemSlide.close();
}
}
feel free to change the â100â to whatever you wish. it will open with the animation, and will conserve every feature baked inside. the number you provide is the number of pixel you want it to be opened ( a number that you have to chose according to the width of the sliding-buttons underneath the ion-item
What about keeping the same behaviour and animations as if you were sliding?
openSlide(itemSlide: ItemSliding, item: Item) {
// Keep track of the last item slided so i can duplicate behaviour when another is being opened
if(this.lastItemSliding){
if(this.lastItemSliding.item['opened']){
this.closeSlide(this.lastItemSliding);
}
}
let eleRef =itemSlide.item.getElementRef();
//Get number of siblings buttons in ion-options to calculate the width to slide
let options = eleRef.nativeElement['nextElementSibling']['children'].length;
itemSlide.item['opened'] = true;
itemSlide.setElementClass("active-sliding", true);
itemSlide.setElementClass("active-slide", true);
itemSlide.setElementClass("active-options-right", true);
this.lastItemSliding = itemSlide;
// I used a FIXED width for ion-options buttons. Siblings Width can be used
item.setElementStyle("transform", `translate3d(-${options*70}px, 0px, 0px)`);
}
// I added a <button> in <ion-options> to close the slide, as the item doesn't trigger events when opened
closeSlide(itemSlide: ItemSliding) {
itemSlide.item['opened'] = false;
itemSlide.close();
setTimeout(()=>{
itemSlide.setElementClass("active-slide", false);
itemSlide.setElementClass("active-sliding", false);
itemSlide.setElementClass("active-options-right", false);
},600);
}
I had issues with this and came up with another possible solution.
clickSlide(itemSlide: ItemSliding) {
if (itemSlide.getSlidingPercent() == 0) {
// Two calls intentional after examination of vendor.js function
itemSlide.moveSliding(-1000);
itemSlide.moveSliding(-1000);
}
else {
itemSlide.close();
}
}
The moveSliding(amount: string) method calls _setOpenAmount as described by @iliasbhal
My ion-item slides from right to left so the amount needed to be negative. Any value beyond the max slide amount should work.
The call is made twice because moveSliding has a check at the beginning that will return the first time it is called, but the second time it will pass and reach the _setOpenAmount call.