瀏覽代碼

infinte scroll for user page

David Thornley 6 年之前
父節點
當前提交
12a1236d0a

+ 5
- 3
zlg/src/pages/home/home.html 查看文件

@@ -2,7 +2,7 @@
2 2
   <ion-navbar>
3 3
         <br>
4 4
         <button ion-button menuToggle><ion-icon name="menu"></ion-icon></button>
5
-    <ion-title align="middle">The Vault</ion-title>
5
+    <ion-title align=right>The Vault</ion-title>
6 6
   </ion-navbar>
7 7
 </ion-header>
8 8
 
@@ -20,9 +20,11 @@
20 20
     <button (click)="sendPost()" id="myVault-button18" style="font-weight:600;font-size:20px;" class="codystar button button-royal button-block button-small">Post</button>
21 21
   </div>
22 22
   
23
-  <div id="myVault-markdown4" class="show-list-numbers-and-dots overflow-box" style="overflow-y: scroll; height: 55vh;">
23
+  <div id="myVault-markdown4" class="show-list-numbers-and-dots overflow-box" style="overflow-y: scroll; height: 50vh;">
24 24
       <div style="color:#FFFFFF;list-style-type: none;" *ngFor="let p of posts">
25
-        <button (click)="goToUsersFeed(p)" class="button button-block" style="background-color:lightgray;text-align: left; word-wrap: break-word; white-space: normal"><strong>{{ p.userName }}: </strong>{{ p.message }}</button>
25
+        <button (click)="goToUsersFeed(p)" class="button button-block" style="background-color:lightgray;text-align: left; word-wrap: break-word; white-space: normal"><strong>{{ p.userName }}: </strong>{{ p.message }}
26
+        <p> Posted: {{ p.createdAt |date:'dd/MM/yyyy H:mm' }}</p>
27
+      </button>
26 28
       </div>
27 29
   </div>
28 30
 

+ 1
- 18
zlg/src/pages/home/home.ts 查看文件

@@ -37,23 +37,6 @@ export class HomePage implements OnInit{
37 37
     this.connect();  
38 38
   }
39 39
 
40
-//   connect() {
41
-//     const socket = new SockJS('https://fast-headland-39537.herokuapp.com/vault-socket');
42
-//     this.stompClient = Stomp.over(socket);
43
-
44
-//     this.stompClient.connect({}, (frame) => {
45
-//       console.log('Connected: ' + frame);
46
-//       this.stompClient.subscribe('/topic/posts', (post) => {
47
-//         this.posts.unshift(JSON.parse(post.body)));
48
-//       _this.stompClient.subscribe('/topic/posts', (post) => {
49
-//         _this.posts.unshift(JSON.parse(post.body));
50
-//      });
51
-//     }
52
-//   }
53
-//   );
54
-  
55
-// }
56
-
57 40
 connect() {
58 41
   const socket = new SockJS('https://fast-headland-39537.herokuapp.com/vault-socket');
59 42
   this.stompClient = Stomp.over(socket);
@@ -106,7 +89,7 @@ connect() {
106 89
 
107 90
   goToUsersFeed(post) {
108 91
     console.log(post.id);
109
-    let params = {id: post.userId};
92
+    let params = {id: post.userId, name: post.userName};
110 93
     this.navCtrl.push('UserfeedPage', params);
111 94
   }
112 95
 

+ 20
- 7
zlg/src/pages/userfeed/userfeed.html 查看文件

@@ -7,24 +7,37 @@
7 7
 <ion-header>
8 8
 
9 9
   <ion-navbar>
10
-    <ion-title align = "middle"><strong>{{user.name}}'s</strong> Vault</ion-title>
10
+    <ion-title align=right><strong>{{userName}}'s</strong> Vault</ion-title>
11 11
   </ion-navbar>
12 12
 </ion-header>
13 13
 
14 14
 
15 15
 <ion-content padding="true" style="background: url(../assets/img/0iNAJ9CJQ1qT57JTEweX_palePurpVault.png) no-repeat center;background-size:cover;" class="has-header">
16 16
   <ion-card-content text-center>
17
-      <ion-img src = "/assets/img/tom.jpg" width="250" height = "250"></ion-img>
17
+      <ion-img src = "/assets/img/tom.jpg" width="125" height = "125"></ion-img>
18 18
   </ion-card-content>
19 19
 
20
-
20
+<!-- 
21 21
   <ion-list>
22
-    <!-- <ion item *ngFor= "let post of feed"> -->
23
-    <ion-item>
22
+    <div style= "overflow-y:scroll">
23
+    <ion-item *ngFor= "let post of postArr">
24 24
       <h2>{{post.message}}</h2>
25 25
       <p>
26
-          {{post.timestamp}}
26
+          {{post.createdAt}}
27 27
       </p>
28 28
     </ion-item>
29
-  </ion-list>
29
+  </div>
30
+  </ion-list> -->
31
+  <div id="myVault-markdown4" class="show-list-numbers-and-dots overflow-box" style="overflow-y: scroll; height: 60vh;">
32
+      <div style="color:#FFFFFF;list-style-type: none;" *ngFor="let post of postArr">
33
+        <ion-item class="button button-block" style="background-color:lightgray;text-align: left; word-wrap: break-word; white-space: normal; cursor:none; ">{{ post.message }}
34
+        <p> Posted: {{post.createdAt |date:'MM/dd/yyyy H:mm'}}</p>
35
+        </ion-item>
36
+      </div>
37
+  </div>
38
+
39
+<ion-infinite-scroll (ionInfinite)="doInfinite($event)" *ngIf="page < totalPage">
40
+  <ion-infinite-scroll-content loadingSpinner="bubbles" loadingText="Loading more data..." style="color:lightgray"></ion-infinite-scroll-content>
41
+</ion-infinite-scroll>
42
+
30 43
 </ion-content>

+ 51
- 8
zlg/src/pages/userfeed/userfeed.ts 查看文件

@@ -15,28 +15,71 @@ import { UserProvider } from '../../providers/user/UserProvider';
15 15
   templateUrl: 'userfeed.html',
16 16
 })
17 17
 export class UserfeedPage {
18
-  //user: any;
19
-  user= {name:'Froilanda'};
18
+  userName: string;
20 19
   post= {timestamp: '3:19PM Sun August 5th 2018',message:'hello world'};
21 20
   userid: any;
22 21
   postArr: Array<any>;
22
+  data: any;
23
+  errorMessage: string;
24
+  page = 0;
25
+  perPage = 0;
26
+  totalData = 0;
27
+  totalPage = 0;
23 28
 
24 29
   constructor(public navCtrl: NavController, public navParams: NavParams,
25 30
               public UserProvider: UserProvider) {
26 31
     //this.user.name = this.navParams.get('name');
27 32
     this.userid = this.navParams.get('id');
33
+    this.userName = this.navParams.get('name');
34
+    this.getAllPostsByUserId();
28 35
   }
29 36
 
30 37
   ionViewDidLoad() {
31 38
     console.log('ionViewDidLoad UserfeedPage');
39
+    this.getAllPostsByUserId();
32 40
   }
33 41
 
34
-  getAllPostsByUserId(){
35
-    return this.UserProvider.getAllPostsByUserId(this.userid).subscribe(response => {
36
-      console.log("response: " + response);
37
-      this.postArr = response;
38
-      console.log("postarr: " + this.postArr);
39
-    });
42
+  // getAllPostsByUserId(){
43
+  //   return this.UserProvider.getAllPostsByUserId(this.userid).subscribe(response => {
44
+  //     console.log("response: " + response);
45
+  //     this.postArr = response;
46
+  //     console.log("postarr: " + this.postArr);
47
+  //   });
48
+  // }
49
+
50
+  getAllPostsByUserId() {
51
+    this.UserProvider.getAllPostsByUserId(this.page, this.userid)
52
+       .subscribe(
53
+         res => {
54
+           console.log(res)
55
+           this.data = res;
56
+           this.postArr = this.data.content;
57
+           this.perPage = this.data.size;
58
+           this.totalData = this.data.totalElements;
59
+           this.totalPage = this.data.totalPages;
60
+         },
61
+         error =>  this.errorMessage = <any>error);
62
+  }
63
+
64
+  doInfinite(infiniteScroll) {
65
+    this.page = this.page+1;
66
+    setTimeout(() => {
67
+      this.UserProvider.getAllPostsByUserId(this.page, this.userid)
68
+         .subscribe(
69
+           res => {
70
+             this.data = res;
71
+             this.perPage = this.data.size;
72
+             this.totalData = this.data.totalElements;
73
+             this.totalPage = this.data.totalPages;
74
+             for(let i=0; i<this.data.content.length; i++) {
75
+               this.postArr.push(this.data.content[i]);
76
+             }
77
+           },
78
+           error =>  this.errorMessage = <any>error);
79
+  
80
+      console.log('Async operation has ended');
81
+      infiniteScroll.complete();
82
+    }, 1000);
40 83
   }
41 84
 
42 85
 

+ 2
- 2
zlg/src/providers/user/userProvider.ts 查看文件

@@ -12,7 +12,7 @@ export class UserProvider{
12 12
   constructor(public http: HttpClient) {
13 13
   }
14 14
 
15
-  getAllPostsByUserId(id: string):Observable<any> {
16
-    return this.http.get(this.USER_API + id);
15
+  getAllPostsByUserId(page: number, id: string):Observable<any> {
16
+    return this.http.get(this.USER_API + id + "/?page="+ page);
17 17
   }
18 18
 }