× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Deleting Items From Ionic Lists

Sasha
Aug 15, 2016
<p>There are many times when an application may need the ability to remove an item from a list. For example, you might be making a to do list and decide you want to remove a completed task.</p> <p>The&nbsp;<code>&lt;ion-list&gt;</code>&nbsp;makes it easy to remove items in a few simple steps.</p> <hr> <h3>DIRECTIONS</h3> <ol> <li> <p>Add an icon to the header that will be used to toggle the delete button for all items. Add a button before the&nbsp;<code>&lt;h1&gt;</code>&nbsp;heading:</p> <pre><code><span style="background-color:rgba(110, 226, 40, 0.298039)">&lt;button class="button button-icon icon ion-ios-minus-outline" ng-click="showDelete = !showDelete"&gt;&lt;/button&gt;</span> &lt;h1 class="title"&gt;Ionic List Delete&lt;/h1&gt; </code></pre> <p><em><code>showDelete</code>&nbsp;will be set to&nbsp;<code>true</code>&nbsp;on the initial click of the button, and then set to&nbsp;<code>false</code>&nbsp;on the next click. This will work as a toggle to show/hide the delete buttons.</em></p> </li> <li> <p>Add the&nbsp;<code>&lt;ion-delete-button&gt;</code>&nbsp;inside the<code>&lt;ion-item&gt;</code>, so that each item will have the delete button:</p> <pre><code>&lt;ion-item ng-repeat="item in items"&gt; <span style="background-color:rgba(110, 226, 40, 0.298039)">&lt;ion-delete-button class="ion-minus-circled"&gt;&lt;/ion-delete-button&gt;</span> Item {{ item.id }} &lt;/ion-item&gt; </code></pre> <p><em>You can give the&nbsp;<code>&lt;ion-delete-button&gt;</code>&nbsp;any icon class you'd like.</em></p> </li> <li> <p>Modify the&nbsp;<code>&lt;ion-list&gt;</code>&nbsp;to add&nbsp;<code>show-delete</code>&nbsp;to tell the list when it should show the delete buttons:</p> <pre><code>&lt;ion-list <span style="background-color:rgba(110, 226, 40, 0.298039)">show-delete="showDelete"</span>&gt; </code></pre> <p><em>The delete button for all items will show when clicking on the button in the header.</em></p> </li> <li> <p>Add the&nbsp;<code>ng-click</code>&nbsp;directive to the<code>&lt;ion-delete-button&gt;</code>, so that clicking on the delete button will call a function called&nbsp;deleteItem:</p> <pre><code>&lt;ion-delete-button class="ion-minus-circled" <span style="background-color:rgba(110, 226, 40, 0.298039)">ng-click="deleteItem(item)"</span>&gt;&lt;/ion-delete-button&gt; </code></pre> </li> <li> <p>In&nbsp;app.js, add the&nbsp;deleteItem&nbsp;function inside of theAppCtrl&nbsp;Controller:</p> <pre><code>.controller('AppCtrl', function($scope) { <span style="background-color:rgba(110, 226, 40, 0.298039)">$scope.deleteItem = function (item) { $scope.items.splice($scope.items.indexOf(item), 1); };</span> </code></pre> <p><em>Toggling the delete buttons and clicking on one will now remove the item from the list!</em></p> </li> </ol>