1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| import { LightningElement, track, api, wire } from 'lwc'; import getCovidData from '@salesforce/apexContinuation/Covid19TrackerController.getCov19Data'; import WechatPublicLogo_1 from '@salesforce/resourceUrl/WechatPublicLogo_1';
const columns = [ { label: '国家', fieldName: 'Country', type: 'text', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } }, { label: '新增确诊', fieldName: 'NewConfirmed', type: 'number', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } }, { label: '累积确诊', fieldName: 'TotalConfirmed', type: 'number', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } }, { label: '新增死亡', fieldName: 'NewDeaths', type: 'number', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } }, { label: '累积死亡', fieldName: 'TotalDeaths', type: 'number', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } }, { label: '新增治愈', fieldName: 'NewRecovered', type: 'number', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } }, { label: '累积治愈', fieldName: 'TotalRecovered', type: 'number', sortable: "true", cellAttributes: { class: { fieldName: 'covidStatus' } } } ];
export default class Covid19TrackerCompt extends LightningElement { FooterLogoUrl = WechatPublicLogo_1;
@track data = []; @track showSpinner = true; @track Initialized = false; @track columns = columns; @track sortDirection; @track sortBy;
renderedCallback() { console.log('enter renderedCallback');
getCovidData().then(data => { if (this.Initialized) { return; } this.Initialized = true;
console.log('***getCovidData1.0 **'); console.log(data.Global); this.data = []; for (let i = 0; i < data.Countries.length; i++) { let covidStatusVal = 'slds-text-color_success'; if (data.Countries[i].TotalDeaths > 0) { covidStatusVal = 'slds-text-color_error'; }
var row = { id: data.Countries[i].Country, Country: data.Countries[i].Country, NewConfirmed: data.Countries[i].NewConfirmed, TotalConfirmed: data.Countries[i].TotalConfirmed, NewDeaths: data.Countries[i].NewDeaths, TotalDeaths: data.Countries[i].TotalDeaths, NewRecovered: data.Countries[i].NewRecovered, TotalRecovered: data.Countries[i].TotalRecovered, covidStatus: covidStatusVal } this.data.push(row); } console.log('Done'); this.showSpinner = false; this.sortData('NewConfirmed', 'desc');
}).catch(error => { console.log(' ** error ** '); console.log(error); }); }
handleSortdata(event) { this.showSpinner = true; this.sortBy = event.detail.fieldName; this.sortDirection = event.detail.sortDirection; console.log('fieldName : '+event.detail.fieldName + ' sortDirection : ' + event.detail.sortDirection); this.sortData(event.detail.fieldName, event.detail.sortDirection); }
sortData(fieldname, direction) { let parseData = JSON.parse(JSON.stringify(this.data)); let keyValue = (a) => { return a[fieldname]; }; let isReverse = direction === 'asc' ? 1 : -1; parseData.sort((x, y) => { x = keyValue(x) ? keyValue(x) : ''; y = keyValue(y) ? keyValue(y) : '';
return isReverse * ((x > y) - (y > x)); }); this.data = parseData; this.showSpinner = false; } }
|