ionic 列表操作


列表是一个应用广泛在几乎所有移动app中的界面元素。ionList 和 ionItem 这两个指令还支持多种多样的交互模式,比如移除其中的某一项,拖动重新排序,滑动编辑等等。

用法

  1. <ion-list>
  2. <ion-item ng-repeat="item in items">
  3. Hello, {{item}}!
  4. </ion-item>
  5. </ion-list>

高级用法: 缩略图,删除按钮,重新排序,滑动

  1. <ion-list ng-controller="MyCtrl"
  2. show-delete="shouldShowDelete"
  3. show-reorder="shouldShowReorder"
  4. can-swipe="listCanSwipe">
  5. <ion-item ng-repeat="item in items"
  6. class="item-thumbnail-left">
  7.  
  8. <img ng-src="{{item.img}}">
  9. <h2>{{item.title}}</h2>
  10. <p>{{item.description}}</p>
  11. <ion-option-button class="button-positive"
  12. ng-click="share(item)">
  13. 分享
  14. </ion-option-button>
  15. <ion-option-button class="button-info"
  16. ng-click="edit(item)">
  17. 编辑
  18. </ion-option-button>
  19. <ion-delete-button class="ion-minus-circled"
  20. ng-click="items.splice($index, 1)">
  21. </ion-delete-button>
  22. <ion-reorder-button class="ion-navicon"
  23. on-reorder="reorderItem(item, $fromIndex, $toIndex)">
  24. </ion-reorder-button>
  25.  
  26. </ion-item>
  27. </ion-list>

API

属性 类型 详情
delegate-handle
(可选)
字符串

该句柄定义带有$ionicListDelegate的列表。

show-delete
(可选)
布尔值

列表项的删除按钮当前是显示还是隐藏。

show-reorder
(可选)
布尔值

列表项的排序按钮当前是显示还是隐藏。

can-swipe
(可选)
布尔值

列表项是否被允许滑动显示选项按钮。默认:true。

实例

HTML 代码:

  1. <html ng-app="ionicApp">
  2. <head>
  3. <meta charset="utf-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  5. <title>Ionic List Directive</title>
  6. <link href="http://www.shouce.ren/static/ionic/css/ionic.min.css" rel="stylesheet">
  7. <script src="http://www.shouce.ren/static/ionic/js/ionic.bundle.min.js"></script>
  8. </head>
  9.  
  10. <body ng-controller="MyCtrl">
  11. <ion-header-bar class="bar-positive">
  12. <div class="buttons">
  13. <button class="button button-icon icon ion-ios-minus-outline"
  14. ng-click="data.showDelete = !data.showDelete; data.showReorder = false"></button>
  15. </div>
  16. <h1 class="title">Ionic Delete/Option Buttons</h1>
  17. <div class="buttons">
  18. <button class="button" ng-click="data.showDelete = false; data.showReorder = !data.showReorder">
  19. Reorder
  20. </button>
  21. </div>
  22. </ion-header-bar>
  23.  
  24. <ion-content>
  25.  
  26. <!-- The list directive is great, but be sure to also checkout the collection repeat directive when scrolling through large lists -->
  27. <ion-list show-delete="data.showDelete" show-reorder="data.showReorder">
  28.  
  29. <ion-item ng-repeat="item in items"
  30. item="item"
  31. href="#/item/{{item.id}}" class="item-remove-animate">
  32. Item {{ item.id }}
  33. <ion-delete-button class="ion-minus-circled"
  34. ng-click="onItemDelete(item)">
  35. </ion-delete-button>
  36. <ion-option-button class="button-assertive"
  37. ng-click="edit(item)">
  38. Edit
  39. </ion-option-button>
  40. <ion-option-button class="button-calm"
  41. ng-click="share(item)">
  42. Share
  43. </ion-option-button>
  44. <ion-reorder-button class="ion-navicon" on-reorder="moveItem(item, $fromIndex, $toIndex)"></ion-reorder-button>
  45. </ion-item>
  46.  
  47. </ion-list>
  48.  
  49. </ion-content>
  50. </body>
  51. </html>

CSS 代码

  1. body {
  2. cursor: url('http://www.shouce.ren/try/demo_source/finger.png'), auto;
  3. }

JavaScript 代码

  1. angular.module('ionicApp', ['ionic'])
  2.  
  3. .controller('MyCtrl', function($scope) {
  4. $scope.data = {
  5. showDelete: false
  6. };
  7. $scope.edit = function(item) {
  8. alert('Edit Item: ' + item.id);
  9. };
  10. $scope.share = function(item) {
  11. alert('Share Item: ' + item.id);
  12. };
  13. $scope.moveItem = function(item, fromIndex, toIndex) {
  14. $scope.items.splice(fromIndex, 1);
  15. $scope.items.splice(toIndex, 0, item);
  16. };
  17. $scope.onItemDelete = function(item) {
  18. $scope.items.splice($scope.items.indexOf(item), 1);
  19. };
  20. $scope.items = [
  21. { id: 0 },
  22. { id: 1 },
  23. { id: 2 },
  24. { id: 3 },
  25. { id: 4 },
  26. { id: 5 },
  27. { id: 6 },
  28. { id: 7 },
  29. { id: 8 },
  30. { id: 9 },
  31. { id: 10 },
  32. { id: 11 },
  33. { id: 12 },
  34. { id: 13 },
  35. { id: 14 },
  36. { id: 15 },
  37. { id: 16 },
  38. { id: 17 },
  39. { id: 18 },
  40. { id: 19 },
  41. { id: 20 },
  42. { id: 21 },
  43. { id: 22 },
  44. { id: 23 },
  45. { id: 24 },
  46. { id: 25 },
  47. { id: 26 },
  48. { id: 27 },
  49. { id: 28 },
  50. { id: 29 },
  51. { id: 30 },
  52. { id: 31 },
  53. { id: 32 },
  54. { id: 33 },
  55. { id: 34 },
  56. { id: 35 },
  57. { id: 36 },
  58. { id: 37 },
  59. { id: 38 },
  60. { id: 39 },
  61. { id: 40 },
  62. { id: 41 },
  63. { id: 42 },
  64. { id: 43 },
  65. { id: 44 },
  66. { id: 45 },
  67. { id: 46 },
  68. { id: 47 },
  69. { id: 48 },
  70. { id: 49 },
  71. { id: 50 }
  72. ];
  73. });