Preisberechnung mit JavaScript

Fragen und Antworten rund um das Thema JavaScript im LiveCycleDesigner
Bourner
Beiträge: 20
Registriert: 16.02.2017, 19:38

Re: Preisberechnung mit JavaScript

Beitrag von Bourner » 30.09.2019, 14:15

Hallo,

habe schlecht zeit gehabt. Aber möchte das natürlich niemandem vorenthalten.
Habe nun ein weiteres Dropdown Feld, also insgesamt 4 statt 3.
Der Code im Skriptobjekt sieht dann so aus:

Code: Alles auswählen

function oEnter(Objekt) {
	var Pfad = xfa.resolveNode(Objekt.parent.name) ;
	Objekt.fontColor = "0,0,0" ;
	switch(Objekt.name) {
	    case "Produkt":
			Objekt.clearItems() ;
			var Sortiment = Produkte.Details() ;
			for (i = 0; i < Sortiment.length; i++) {
				Objekt.addItem(Sortiment[i][0]) ;
			}
			xfa.host.openList(Objekt.somExpression) ;
			break ;
		case "Variante":
			if (Pfad.Produkt.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Produkt)
			} else if (Objekt.length == 1) {	//	Gibt es nur einen Wert,
				Objekt.selectedIndex = 0 ;		//	wird der sofort eingtragen
				Objekt.execEvent("exit")		//	und weitergemacht.
			} else {
				xfa.host.openList(Objekt.somExpression)
			}
			break ;
		case "Option":
			if (Pfad.Variante.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Variante)
			} else if (Objekt.length == 1) {
				Objekt.selectedIndex = 0 ;
				Objekt.execEvent("exit")
			} else {
				xfa.host.openList(Objekt.somExpression)
			}
			break ;
		case "Modell":
			if (Pfad.Option.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Option)
			} else if (Objekt.length == 1) {
				Objekt.selectedIndex = 0 ;
				Objekt.execEvent("exit")
			} else {
				xfa.host.openList(Objekt.somExpression)
			}
			break ;
		case "Menge":
			if (Pfad.Modell.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Modell)
			}
	}
}

function leere(Objekt) {
	var Pfad = xfa.resolveNode(Objekt.parent.name) ;
	switch(Objekt.name) {	//	Ohne »break«, also wird vom Einsprung bis zum Ende alles abgearbeiet. 
	    case "Produkt":
			Pfad.Variante.rawValue = "" ;
		case "Variante":
			Pfad.Option.rawValue = "" ;
		case "Option":
			Pfad.Modell.rawValue = "" ;
		case "Modell":
			Pfad.Menge.rawValue = null ;
			Pfad.Preis_e.rawValue = null ;
			Pfad.Preis_g.rawValue = null ;
	}
}

function oExit(Objekt) {
	var Pfad = xfa.resolveNode(Objekt.parent.name) ;
	var Sortiment = Produkte.Details() ;
	switch(Objekt.name) {
	    case "Produkt":
			if (Objekt.rawValue) {
				var arr = Sortiment[Objekt.selectedIndex] ;
				Pfad.Variante.clearItems() ;
				for (i = 1; i < arr.length; i++) {
					Pfad.Variante.addItem(arr[i][0]) ;
				}
				if (!Pfad.Variante.rawValue) {
					Pfad.Preis_g.fillColor = Skripte.Palette(2) ;
					xfa.host.setFocus(Pfad.Variante) ;
				}
			}
			break ;
			
		case "Variante":
			if (Objekt.rawValue) {
				var arr = Sortiment[Pfad.Produkt.selectedIndex][Objekt.selectedIndex + 1] ;
				Pfad.Option.clearItems() ;
				for (i = 1; i < arr.length; i++) {
					Pfad.Option.addItem(arr[i][0]); // + ' => ' + Objekt.selectedIndex) ;	//	»+ "|" + i.toString()« ermöglicht, dass sich der Wert in »arr[i][1]« wiederholen darf.
				}
				var selectedVariante = Objekt.selectedIndex;
				if (!Pfad.Option.rawValue) xfa.host.setFocus(Pfad.Option) ;
			}
			break ;
						
		case "Option":
			if (Objekt.rawValue) {
				var arr = Sortiment[Pfad.Produkt.selectedIndex][Pfad.Variante.selectedIndex + 1][Objekt.selectedIndex + 1];
				Pfad.Modell.clearItems() ;
				for (i = 1; i < arr.length; i++) {
					Pfad.Modell.addItem(arr[i][0],arr[i][1]) + "|" + i.toString();// + " => " + Pfad.Produkt.rawValue);	//	»+ "|" + i.toString()« ermöglicht, dass sich der Wert in »arr[i][1]« wiederholen darf.
				}
				if (!Pfad.Modell.rawValue) xfa.host.setFocus(Pfad.Modell) ;
			}
			break ;
		case "Modell":
			if (Objekt.rawValue) {
				Pfad.Preis_e.rawValue = parseFloat(Objekt.rawValue) ;
				if (!Pfad.Menge.rawValue) xfa.host.setFocus(Pfad.Menge) ;
			}
			break ;
		case "Menge":
			Pfad.Preis_g.rawValue = Objekt.rawValue * Pfad.Preis_e.rawValue ;
			Pfad.Preis_g.border.nodes.remove(Pfad.Preis_g.border.fill) ;
			if (Objekt.rawValue && Objekt.parent.instanceManager.count == Objekt.parent.index + 1) Objekt.parent.instanceManager.addInstance(1) ;
	}
	Objekt.execEvent("mouseExit") ;
}

Bourner
Beiträge: 20
Registriert: 16.02.2017, 19:38

Re: Preisberechnung mit JavaScript

Beitrag von Bourner » 07.10.2019, 15:54

Hallo ich bin es nochmal...

Mit meinem oben geposteten Code kann ich zwar 4 Dropdowns hintereinander ausfüllen und das klappt ansich auch alles ganz gut.
Wenn ich das PDF nun aber speicher und wieder öffne, steht dort nicht der gewünschte Wert (das Modell), sondern der Preis.

Er vertauscht also arr[0] und arr[1].
Ich arbeite mit Foxit und Adobe Reader. Im Foxit läuft es bereits so, aber im Reader gibt es immer wieder diese Probleme.
Es sollte natürlich am besten in beiden laufen. Wundert mich grade, dass es mit Foxit besser läuft, aber anderes Thema...

Kann sich das bitte jemand anschauen und ein 4. Dropdown mit einbauen?

Bitte so, dass folgendes genutzt werden kann.

Code: Alles auswählen

[['Vertrag 1',
	['Smartphone',							
		['Apple iPhone',					
			['iPhone 8 - Space Grau - 64GB - 4,7"',	"268.87€"],		
			['iPhone 8 - Silber - 64GB - 4,7"',	"268.87€"],
			['iPhone 8 Plus - Space Grau - 64GB - 4,7"',	"369.71€"],		
			['iPhone 8 Plus - Silber - 64GB - 4,7"',	"369.71€€"],
			['iPhone 11 - Schwarz - 64GB - 6,1"',	"453.74€"],
			['iPhone 11 - Weiß - 64GB - 6,1"',	"453.74€"],
			['iPhone 11 Pro - Space Grau - 64GB - 5,8"',	"747.86€"],
			['iPhone 11 Pro - Silber - 64GB - 5,8"',	"747.86€"],
			['iPhone 11 Pro Max - Space Grau - 64GB - 6,5"',	"7831.89€"],
			['iPhone 11 Pro Max - Silber - 64GB - 6,5"',	"831.89€"]],
		['Samsung Smartphone',				
			['Samsung A20e - Schwarz - 32 GB - 5,9"',			"6.69€"],	
			['Samsung A20e - Weiß - 32 GB - 5,9"',	"6.69€"],
			['Samsung A70 - Schwarz - 128 GB - 6,7"',	"53.75€"],
			['Samsung A70 - Weiß - 128 GB - 6,7"',	"53.75€"],
			['Samsung Galaxy S10e - Schwarz - 128 GB - 5,8"',	"188.20"],
			['Samsung Galaxy S10e - Weiß - 128 GB - 5,8"',	"188.20"],
			['Samsung Galaxy S10 - Schwarz - 128 GB - 6,1"',	"289.04€"],
			['Samsung Galaxy S10 - Weiß - 128 GB - 6,1"',	"289.04€"],
			['Samsung Galaxy S10+ - Schwarz - 128 GB - 6,4"',	"369.71€"],
			['Samsung Galaxy S10+ - Weiß - 128 GB - 6,4"',	"369.71€"]]],
		['Handy ohne Smartphone Funktion',							
			['Nokia',					
			['3310',	".15"],		
			['6450',	".14"]],
		['CAT',					
			['X1',			".15"],		
			['X2',	".14"]]]],
						
					
['Vertrag 2',
	['Smartphone',							
		['Apple iPhone',				
			['iPhone 8 - Space Grau - 64GB - 4,7"',	".15"],		
			['iPhone 8 - Silber - 64GB - 4,7"',	".12"],
			['iPhone 11 - Schwarz - 64GB - 6,1"',	".12"],
			['iPhone 11 - Weiß - 64GB - 6,1"',	".12"],
			['iPhone 11 Pro - Space Grau - 64GB - 5,8"',	".12"],
			['iPhone 11 Pro - Silber - 64GB - 5,8"',	".12"],
			['iPhone 11 Pro Max - Space Grau - 64GB - 6,5"',	".12"],
			['iPhone 11 Pro Max - Silber - 64GB - 6,5"',	".14"]],
		['Samsung Smartphone',				
			['Samsung A20e - Schwarz - 32 GB - 5,9"',			".15"],	
			['Samsung A20e - Weiß - 32 GB - 5,9"',	".12"],
			['Samsung A70 - Schwarz - 128 GB - 6,7"',	".12"],
			['Samsung A70 - Weiß - 128 GB - 6,7"',	".12"],
			['Samsung Galaxy S10e - Schwarz - 128 GB - 5,8"',	".12"],
			['Samsung Galaxy S10e - Weiß - 128 GB - 5,8"',	".12"],
			['Samsung Galaxy S10 - Schwarz - 128 GB - 6,1"',	".12"],
			['Samsung Galaxy S10 - Weiß - 128 GB - 6,1"',	".12"],
			['Samsung Galaxy S10+ - Schwarz - 128 GB - 6,4"',	".12"],
			['Samsung Galaxy S10+ - Weiß - 128 GB - 6,4"',	".14"]]],
	['Handy ohne Smartphone Funktion',							
		['Nokia',					
			['3310',	".15"],		
			['6450',	".14"]],
		['CAT',					
			['X1',			".15"],		
			['X2',	".14"]]]],
					
['Vertrag 3',
	['Tablet',							
		['Apple iPad',					
			['iPad Mini - Space Grau - 64GB - 7,9"',	".15"],		
			['iPad Mini - Silber - 64GB - 7,9"',	".12"],
			['iPad 7. Generation - Space Grau - 32GB - 10,2"',	".12"],
			['iPad 7. Generation - Silber - 32GB - 10,2"',	".12"],
			['iPad Pro 11" - Space Grau - 64GB - 11"',	".12"],
			['iPad Pro 11" - Silber - 64GB - 11"',	".12"],
			['iPad Pro 12,9" - Space Grau - 64GB - 12,9"',	".12"],
			['iPad Pro 12,9" - Silber - 64GB - 12,9"',	".14"]],
		['Samsung Tablet',					
			['Samsung Galaxy Tab A 10.1 - Schwarz - 32GB - 10,1"',			".15"],		
			['Samsung Galaxy Tab A 10.5 - Schwarz - 32GB - 10,5"',	".12"],
			['Samsung Galaxy Tab S5e - Schwarz - 64GB - 10,5"',	".12"],
			['Samsung Galaxy Tab S6 - Grau - 128GB - 10,5"',	".14"]]]]]
	

meine Funktionen sehen bisher so aus. Keine Ahnung, warum das nicht richtig gespeichert wird

Code: Alles auswählen

function oEnter(Objekt) {
	var Pfad = xfa.resolveNode(Objekt.parent.name) ;
	Objekt.fontColor = "0,0,0" ;
	switch(Objekt.name) {
	    case "Produkt":
			Objekt.clearItems() ;
			var Sortiment = Produkte.Details() ;
			for (i = 0; i < Sortiment.length; i++) {
				Objekt.addItem(Sortiment[i][0]) ;
			}
			xfa.host.openList(Objekt.somExpression) ;
			break ;
		case "Variante":
			if (Pfad.Produkt.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Produkt)
			} else if (Objekt.length == 1) {	//	Gibt es nur einen Wert,
				Objekt.selectedIndex = 0 ;		//	wird der sofort eingtragen
				Objekt.execEvent("exit")		//	und weitergemacht.
			} else {
				xfa.host.openList(Objekt.somExpression)
			}
			break ;
		case "Option":
			if (Pfad.Variante.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Variante)
			} else if (Objekt.length == 1) {
				Objekt.selectedIndex = 0 ;
				Objekt.execEvent("exit")
			} else {
				xfa.host.openList(Objekt.somExpression)
			}
			break ;
		case "Modell":
			if (Pfad.Option.selectedIndex == -1) {
				xfa.host.setFocus(Pfad.Option)
					} else if (Objekt.length == 1) {
				Objekt.selectedIndex = 0 ;
				Objekt.execEvent("exit")
			} else {
				xfa.host.openList(Objekt.somExpression)
			}
	}
}

function leere(Objekt) {
	var Pfad = xfa.resolveNode(Objekt.parent.name) ;
	switch(Objekt.name) {	//	Ohne »break«, also wird vom Einsprung bis zum Ende alles abgearbeiet. 
	    case "Produkt":
			Pfad.Variante.rawValue = "" ;
		case "Variante":
			Pfad.Option.rawValue = "" ;
		case "Option":
			Pfad.Modell.rawValue = "" ;
		case "Modell":
			Pfad.Preis_e.rawValue = null ;
			Pfad.Preis_g.rawValue = null ;
	}
}

function oExit(Objekt) {
	var Pfad = xfa.resolveNode(Objekt.parent.name) ;
	var Sortiment = Produkte.Details() ;
	switch(Objekt.name) {
	    case "Produkt":
			if (Objekt.rawValue) {
				var arr = Sortiment[Objekt.selectedIndex] ;
				Pfad.Variante.clearItems() ;
				for (i = 1; i < arr.length; i++) {
					Pfad.Variante.addItem(arr[i][0]) ;
				}
				if (!Pfad.Variante.rawValue) {
					Pfad.Preis_g.fillColor = Skripte.Palette(2) ;
					xfa.host.setFocus(Pfad.Variante) ;
				}
			}
			break ;
			
		case "Variante":
			if (Objekt.rawValue) {
				var arr = Sortiment[Pfad.Produkt.selectedIndex][Objekt.selectedIndex + 1] ;
				Pfad.Option.clearItems() ;
				for (i = 1; i < arr.length; i++) {
					Pfad.Option.addItem(arr[i][0]); // + ' => ' + Objekt.selectedIndex) ;	//	»+ "|" + i.toString()« ermöglicht, dass sich der Wert in »arr[i][1]« wiederholen darf.
				}
				if (!Pfad.Option.rawValue) xfa.host.setFocus(Pfad.Option) ;
			}
			break ;
						
		case "Option":
			if (Objekt.rawValue) {
				var arr = Sortiment[Pfad.Produkt.selectedIndex][Pfad.Variante.selectedIndex + 1][Objekt.selectedIndex + 1];
				Pfad.Modell.clearItems() ;
				for (i = 1; i < arr.length; i++) {
					Pfad.Modell.addItem(arr[i][0],arr[i][1]) + "|" + i.toString() ;// + " => " + Pfad.Produkt.rawValue);	//	»+ "|" + i.toString()« ermöglicht, dass sich der Wert in »arr[i][1]« wiederholen darf.
				}
				if (!Pfad.Modell.rawValue) xfa.host.setFocus(Pfad.Modell) ;
			}
			break ;
		case "Modell":
			if (Objekt.rawValue) {
				Pfad.Preis_e.rawValue = parseFloat(Objekt.rawValue) ;
			}
}
}

Gibt es da vielleicht einen Trick?

Danke im Voraus
Gruß
Björn

Bourner
Beiträge: 20
Registriert: 16.02.2017, 19:38

Re: Preisberechnung mit JavaScript

Beitrag von Bourner » 07.10.2019, 16:17

Wahrscheinlich ist es besser, wenn man dazu auch die richtige Datei hat. Deswegen hier mein fast fertiges Formular.
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.

armine
Beiträge: 2690
Registriert: 16.05.2009, 10:24

Re: Preisberechnung mit JavaScript

Beitrag von armine » 08.10.2019, 07:53

Hallo Björn,

Datei hochladen bringt’s! Da kann man sehen, dass du bei den Voreinstellungen von Adobe das Papierformat „Default“ übernommen hast, statt DIN A4 einzustellen.
Das böse automatische Speichern konntest du nicht hinnehmen, das musstest du ins manuelle zerbessern. Folglich wird auch nicht automatisch gespeichert und beim Schließen des PDFs verlierst du Information.

vg armine
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.

Antworten