Dart Example

Just another WordPress site

AngularDart v4 Pagination

AngularDart v4 Sample Code,Demo
  • AngularDart : 4.0.0
  • Dart : 1.24.1
Loading…
(html)
<head>
.....
  <script defer src="main.dart" type="application/dart"></script>
  <script defer src="packages/browser/dart.js"></script>
</head>
<body>
  <my-app>Loading...</my-app>
</body>

(pubspec.yaml)
.....
environment:
  sdk: '>=1.24.0 <2.0.0'
dependencies:
  angular: ^4.0.0
  angular_forms: ^1.0.0
  angular_router: ^1.0.2
  http: ^0.11.0
  stream_transform: ^0.0.6

dev_dependencies:
  angular_test: ^1.0.0
  browser: ^0.10.0
  dart_to_js_script_rewriter: ^1.0.1
  mockito: ^2.0.2
  test: ^0.12.21

transformers:
- angular:
    entry_points:
    - web/main.dart
    - test/**_test.dart
- test/pub_serve:
    $include: test/**_test.dart
- dart_to_js_script_rewriter
import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';

class Friend {
  int id;
  String name;
  int age;
  String address;
  
  Friend(this.id,this.name,this.age,this.address);
}

@Component(
  selector: 'my-app',
  template: '''
<form class="form-inline">
  <div class="form-group">
    <label>Items Per Page:</label>
    <input [(ngModel)]="itemsPerPage" type="number">
  </div>
</form>
<table class="table table-striped">
  <thead>
    <tr>
      <th>№</th><th>Name</th><th>Age</th><th>Address</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let item of friends | slice:(currentPage-1)*conv(itemsPerPage):currentPage*conv(itemsPerPage)">
      <td>{{item.id}}</td>
      <td>{{item.name}}</td>
      <td>{{item.age}}</td>
      <td>{{item.address}}</td>
    </tr>
  </tbody>
  <tfoot>
    <td colspan="4">
      <ul class="pagination">
        <li [ngClass]="prevPageDisabled()">
          <a href="#step2" (click)="prevPage()">≪ Prev</a>
        </li>
        <li *ngFor="let n of range()" 
            [ngClass]="{active: n === currentPage}" 
            (click)="setPage(n)">
          <a href="#step2">{{n}}</a>
        </li>
        <li [ngClass]="nextPageDisabled()">
          <a href="#step2" (click)="nextPage()">Next ≫</a>
        </li>
      </ul>
    </td>
  </tfoot>
</table>
    ''',
  directives: const [CORE_DIRECTIVES, formDirectives],
  pipes: const [COMMON_PIPES]
)
class AppComponent {
  List<Friend> friends = [
    new Friend(1,'John',20,'1st Ave Seattle WA'),
    new Friend(2,'Jessie',55,'2nd Ave Seattle WA'),
    new Friend(3,'Peter',20,'Mercer St Seattle WA'),
    new Friend(4,'Erika',37,'Alaskan Way Seattle WA'),
    new Friend(5,'Tom',20,'Abcd St Seattle WA'),
    new Friend(6,'Richard',32,'Xyza Way Seattle WA'),
    new Friend(7,'Ron',20,'11st Ave Seattle WA'),
    new Friend(8,'Herold',55,'22nd Ave Seattle WA'),
    new Friend(9,'Scott',20,'Nercer St Seattle WA'),
    new Friend(10,'Brian',37,'Olaskan Way Seattle WA'),
    new Friend(11,'Emily',20,'Ubcd St Seattle WA')
  ];
  int itemsPerPage = 3;
  int currentPage = 1;
  int _maxPage;
  int i;
  int conv(idx) {
    if (idx is String) {
      this.i = int.parse(idx);
    } else if (idx == null) {
      this.i = 1;
    } else {
      this.i = idx;
    }
    return this.i;
  }
  List range() {
    this._maxPage = (this.friends.length/conv(this.itemsPerPage)).ceil();
    List ret = [];
    for (var i=1; i<=this._maxPage; i++) {
      ret.add(i);
    }
    return ret;
  }
  void setPage(n) {
    this.currentPage = n;
  }
  void prevPage() {
    if (this.currentPage > 1) {
      --this.currentPage;
    }
  }
  void nextPage() {
    if (this.currentPage < this._maxPage) {
      ++this.currentPage;
    }
  }
  String prevPageDisabled() {
    return this.currentPage == 1 ? "disabled" : "";
  }
  String nextPageDisabled() {
    return this.currentPage == this._maxPage ? "disabled" : "";
  }
}

void main() {
  bootstrap(AppComponent);
}