I wanted to the data be available in the form (editable also) when ever the page loads… as I have multiple forms and only after final submit the form data will clear from the form.
I tried but after serving again the data gets lost…
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController, Platform } from 'ionic-angular';
import { SecTwoPage } from '../sec-two/sec-two';
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
import { FileOpener } from '@ionic-native/file-opener';
import { File } from '@ionic-native/file';
/**
* Generated class for the SecOnePage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-sec-one',
templateUrl: 'sec-one.html',
})
export class SecOnePage {
schoolName: any;
addRess: any;
direcTion: any;
mapLoc: any;
schoolType: any;
typeSchool: any;
headOrg: any;
contSchool: any;
sec1={
SchoolName: '',
Address: '',
Direction:'',
MapLoc:'',
SchoolType:'',
TypeSchool:'',
HeadOrg:'',
ContSchool:'',
}
pdfObj = null;
constructor(public navCtrl: NavController, public navParams: NavParams, public loadingCtrl: LoadingController, private platform: Platform, private file: File, private fileOpener: FileOpener) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad SecOnePage');
if (localStorage.getItem('stat') == '0') {
var sec1 = JSON.parse(localStorage.getItem("sec1"));
this.schoolName = sec1.SchoolName;
this.addRess = sec1.Address;
this.direcTion = sec1.Direction;
this.mapLoc = sec1.MapLoc;
this.schoolType = sec1.SchoolType;
this.typeSchool = sec1.TypeSchool;
this.headOrg = sec1.HeadOrg;
this.contSchool = sec1.ContSchool;
}
}
gotoSecII() {
localStorage.setItem('stat', '0');
var sec1 = [
{
"SchoolName": this.schoolName
},
{
"Address": this.addRess
},
{
"Direction": this.direcTion
},
{
"MapLoc": this.mapLoc
},
{
"SchoolType": this.schoolType
},
{
"TypeSchool": this.typeSchool
},
{
"HeadOrg": this.headOrg
},
{
"ContSchool": this.contSchool
}
];
// Or to get a key/value pair
localStorage.setItem("sec1", JSON.stringify(sec1));
// sec1 = JSON.parse(localStorage.getItem("sec1"));
// console.log(sec1);
var docDefinition = {
content: [
{ text: this.schoolName, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.addRess, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.direcTion, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.mapLoc, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.schoolType, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.typeSchool, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.headOrg, style: 'story', margin: [0, 20, 0, 20] },
{ text: this.contSchool, style: 'story', margin: [0, 20, 0, 20] }
],
styles: {
story: {
italic: true,
alignment: 'center',
width: '50%',
}
}
}
this.pdfObj = pdfMake.createPdf(docDefinition);
if (this.platform.is('cordova')) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: 'application/pdf' });
// Save the PDF to the data Directory of our App
this.file.writeFile(this.file.dataDirectory, 'myform.pdf', blob, { replace: true }).then(fileEntry => {
// Open the PDf with the correct OS tools
this.fileOpener.open(this.file.dataDirectory + 'myform.pdf', 'application/pdf');
})
});
} else {
// On a browser simply use download!
this.pdfObj.download();
// }
let loading = this.loadingCtrl.create({
content: 'Please wait...'
});
loading.present();
loading.dismiss();
this.navCtrl.push(SecTwoPage);
}
}
}