My Approach is
- default 6 items, using api query of the $top=6
- If a button is pressed, clear screen, get all data using qpi query without $top6
Algorithm:
- Define a function to loadNews(boolean=false) This defaults to get 6 items only, API query with $top=6
- Create a button and onClick event pass true to the function
- If true is passed to the loadNews();
- this.forceUpdate();
- clear the contents of the data array
- fetch all data using another API query without $top=6
What will be another better approach that can boost performance? Is it possible to add on show additional items without having to load all data again?
This is my code.
import * as React from 'react';
import styles from './A.module.scss';
import { IAProps } from './IAProps';
import { escape } from '@microsoft/sp-lodash-subset';
import { PageContext } from "@microsoft/sp-page-context";
import { HttpClient, IHttpClientOptions, HttpClientResponse, SPHttpClient, ISPHttpClientOptions, SPHttpClientResponse } from '@microsoft/sp-http';
import {
Environment,
EnvironmentType
} from '@microsoft/sp-core-library';
import {Button} from 'office-ui-fabric-react/lib/Button';
export interface ISPLists {
value: ISPList[];
};
export interface Istate {
ID:string;
Title: string;
isLoad?: boolean;
Url:string;
AttachmentFiles:{
Name:string;
Url:string;
ServerRelativeUrl: string;
};
}
export interface ISPList {
ID:string;
Title: string;
Summary : string;
NewsCategory: string;
Created:string;
AttachmentFiles:{
Name:string,
Url:string,
ServerRelativeUrl: string,
};
}
/*
MAIN STARTS HERE
2020-01-28
*/
export default class A extends React.Component<IAProps> {
constructor(props){
super(props);
this.loadNews();
this.getListData=this.getListData.bind(this);
this.geturl=this.geturl.bind(this);
this.loadNews=this.loadNews.bind(this);
}
//variables to render html tags
private data:any =[];
private loadNews(showall=false){
if(showall){
this.data.length = 0;
this.forceUpdate();
this.getListData(showall);
}else{
this.getListData(showall); // show 6 item by default
}
}
public geturl(query:string){
var path="/Lists/News/DispForm.aspx?ID=";
var currdir=this.props.pagecontext.web.absoluteUrl;
var result=currdir+path+query;
return result;
}
private async getListData(ShowAll) {
var qurl;
if (ShowAll)
{
qurl="/_api/web/lists/getbytitle('News')/items?$select=*,AttachmentFiles&$expand=AttachmentFiles/Title&$orderby=ID desc";
}else{
qurl="/_api/web/lists/getbytitle('News')/items?$select=*,AttachmentFiles&$expand=AttachmentFiles/Title&$orderby=ID desc&$top=6";
}
const opt: ISPHttpClientOptions = { headers: { 'Content-Type': 'application/json;odata=verbose' } };
this.props.SPHttpClient.get(this.props.pagecontext.web.absoluteUrl + qurl, SPHttpClient.configurations.v1, opt).then((response: SPHttpClientResponse) => {
response.json().then((json: any) => {
for(let i=0;i<json.value.length;i++){
var url=this.geturl(json.value[i].ID);
this.data.push({
ID:json.value[i].ID,
Title:<a href={url} target="_blank">{json.value[i].Title}</a>,
Desc:json.value[i].Description,
More:url,
summ:json.value[i].Summary,
Url:<img className={styles.image} src={json.value[i].AttachmentFiles[0] ? json.value[i].AttachmentFiles[0].ServerRelativeUrl: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQy7LYu4CcprObfi6O9Vt7TMhFfT0jKju7koQOdUKH3_ZXmqq2G&s'} />
});
// debugger;
this.forceUpdate();
}//close for
});// close response.json
});//close private getListData method
}
public render(): React.ReactElement<IAProps>{
return (
<div className={styles.thumbnails}>
<div className={styles.title}>
最新消息
</div>
{this.data.map((data)=>
<div className={styles.box}>
{data.Url}
<div className={styles.p}>
{data.Title}
</div>
<div className={styles.h3}>
{data.summ}
</div>
<div className={styles.more}>
<a href={data.More} target="_blank">More...</a>
</div>
</div>
)}
<div className={styles.showall}>
<button onClick={() => this.loadNews(true)}>More </button>
</div>
</div>
);
}
}