I want to swipe Ionic list items to both sides. (i.e left-right AND right-left). It works perfectly for right-left swipe but I am not able to swipe list item to left side.
I used $ionicGesture
for left-right swipe, and it also gives me an alert when i use swiperight
event: event($ionicGesture.on('swiperight', scope.reportEvent, elem))
, but I am not able to let it show the ion-option-button
at the left side.
Here is my directive and controller code:
.directive('onSwipeRight', function($ionicGesture) {
return {
restrict : 'A',
link : function(scope, elem, attrs) {
var gestureType = attrs.gestureType;
switch(gestureType) {
case 'swipeRight':
$ionicGesture.on('swiperight', scope.reportEvent, elem);
break;
case 'swipeleft':
$ionicGesture.on('swipeleft', scope.reportEvent, elem);
break;
case 'doubletap':
$ionicGesture.on('doubletap', scope.reportEvent, elem);
break;
case 'tap':
$ionicGesture.on('tap', scope.reportEvent, elem);
break;
}
}
}
})
.controller('ChatsCtrl', function($scope, Chats) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//
//$scope.$on('$ionicView.enter', function(e) {
//});
$scope.chats = Chats.all();
$scope.remove = function(chat) {
Chats.remove(chat);
}
$scope.reportEvent = function (event) {
alert("hi");
console.log('Reporting : ' + event.type);
event.preventDefault();
};
})
Here is my html code.
<ion-view view-title="Chats">
<ion-content>
<ion-list can-swipe="true">
<ion-item gesture-type="swipeRight" on-swipe-right="swipeRight()" class="item-remove-animate item-avatar item-icon-right" ng-repeat="chat in chats" type="item-text-wrap" href="#/tab/chats/{{chat.id}}">
<img ng-src="{{chat.face}}">
<h2>{{chat.name}}</h2>
<p>{{chat.lastText}}</p>
<i class="icon ion-chevron-right icon-accessory"></i>
<ion-option-button class="button-assertive" ng-click="share(item)" side="left">
Share
</ion-option-button>
<ion-option-button class="button-assertive" ng-click="remove(chat)" side="right">
Delete
</ion-option-button>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
So I want to display share button at left side and delete button at right side.
Can anybody provide me specific solution for it?
Unlike other answers, i've created an angular wrapper for swiper (that seems to be the slider lib used in ionic 2) focused on ionic v1 instead of editing the framework itself.
My wrapper is avaliable here, and there's an demo here.
You can use
npm install ionic-swiper
to install it, and import like instructed on README.md:In javascript with webpack (you can import the whole bundle too like a normal js):
Edit:
I've made some changes since i wrote this answer, so here's a more correct way to use my lib:
And here's the original answer usage example:
In HTML (you will need to adjust some css too):
Here's an gif from the demo (i've recorded this in a touchpad, that's why it seems 'sticky')
I made
item-swipe-pane
directive which creates a container inside aion-item
, which is visible when the item is swiped to the left or to the right.Attribute
direction
controls swipe direction. Possible values areleft
orright
. Default direction is left.You can place any content it the directive, button, text, image, icon, avatar, background image, etc.
The container is quite raw in a sense that everything you place in it has to be formatted by CSS or by other means.
item-swipe-pane
is compatible withion-option-button
,ion-delete-button
andion-reorder-button
directives.It is possible to combine two
item-swipe-pane
s on the sameion-item
. Each one with different swipe direction.Example with two item-swipe-panes, one is on the left and one on the right:
More item-swipe-pane examples are on Codepen.
Important note:
Unfortunately
Ionic Framework
does not allow right swipe (from left to right) of a list item, so I had to make few modifications to theIonic
library. Here is summary of modifications toIonic Framework
.Links:
Modified Ionic library download.
item-swipe-pane directive on Github.
I've edited ionic lib to do something like that. But i couldn't do a JSFiddle or a Code Pen i Will give you the link to my modified ionic.css and ionic.bundle.js!
TL;DR
https://gist.githubusercontent.com/booris/847f044d2ef2a05101ce/raw/2274365384f5eed3e4538b269f3a7d7998eb22ed/ionic.csshttps://gist.githubusercontent.com/booris/847f044d2ef2a05101ce/raw/2274365384f5eed3e4538b269f3a7d7998eb22ed/ionic.bundle.jsJust replace it with yours, start an ionic project blank. And put this HTML in it:
You can specify the wipe direction with left, right or both. And in the ion-options-button you can give it a side.
Hope it helps, anything you need just ask! I will try to comment my changes in the code later on!
EDIT: I will try to explain what i did.
First change the ionOptionButton directive to create to div for the button, one left and one right
Add CSS to left buttons in ionic.css file
Now change the ion-list controller to accept swipe directions attribute
To end, you should replace slideDrag function with this one, just search for it in ionic.bundle.js
My solution is not perfect, but it works. and there are others ways of doing this, i did it this way to understand better how Ionic works and how they do Ionic directives.
Any feedback is welcome, and with this you can try to make your own or improve this one.
here is an sample code using that u can achieve it
swipe-pane.html
swipe-pane.js
swipe-pane.scss