object addieren

stefansvw

Grünschnabel
Hallo ich habe folgendes Problem

ich möchte ein Script erstellen was folgende Tabelle erstellt.

Bezeichnung | 2008 | 2007
------------------------------
Prod1 | 3 | 5
Prod2 | 6 | 3
------------------------------
ges | 9 | 8

es klappt alles bis auf das gesamt am Ende

Hier ist das Script:

Code:
Object.prototype.sum = function() {
var sum = 0;
for(var a in this) if(this.hasOwnProperty(a))sum += this[a];
return sum;
};

Array.prototype.sum = function() {
var sum = 0;
for(var a in this) if(this.hasOwnProperty(a)) sum += this[a].sum();
return sum;
};

function datum(x) {
var jahr = x.substring(0,4);
var monat = x.substring(4,6);
var tag = x.substring(6,8);

var dat = ""+ tag +"."+ monat +"."+ jahr +"";

return dat;
}

var werte = new Array();
werte[0] = new Object(); //2006
werte[1] = new Object(); //2007
werte[2] = new Object(); //2008

var produkte = new Array();
produkte[0] = new Object();

var akt_dat_tmp = new Date();
var akt_dat = akt_dat_tmp.getYear();

var first_day = akt_dat + "0101";
var last_year = (akt_dat-1) + "0101";
var sec_year = (akt_dat-2) + "0101";


var viewEx = ocxBase.CreateView("EX");

var RubNumin = ocxBase.GetARub("EX.NUMIN");
ExNumin = ocxBase.GetRubVal(RubNumin, 0);

var viewTvm = ocxBase.CreateView("TV");

var RubMatuti = ocxBase.GetARub("TV.MATUTI"); // Matuti
var RubNom = ocxBase.GetARub("TV.NOM"); // Nachname (ADM)
var RubPreNom = ocxBase.GetARub("TV.PRENOM"); // Vorname (ADM)

TvmMatuiti = ocxBase.GetRubVal(RubMatuti, 0);
TvmNom = ocxBase.GetRubVal(RubNom, 0);
TvmPreNom = ocxBase.GetRubVal(RubPreNom, 0);

var view = ocxBase.CreateView("SQ");

var rc = ocxBase.ViewFirstRecord(view);

var RubLibPrd = ocxBase.GetARub("SQ.LIBPRD"); // Produkt
var RubQty = ocxBase.GetARub("SQ.QTEMVT"); // Quantität
var RubCharge = ocxBase.GetARub("SQ.NUMLOT"); // Charge
var RubDate = ocxBase.GetARub("SQ._DATEMVT"); // Ddatum
var RubNumSq = ocxBase.GetARub("SQ.NUMIN"); // Numin
var RubMatuti = ocxBase.GetARub("SQ.MATUTI"); // Matuti

Matuti = ocxBase.GetRubVal(RubMatuti, 0);

ocxBase.DefRubListVal(view,"SQ.NUMIN", ExNumin, 1); // 25.5.07 rpowner statt matuti!!
ocxBase.DefRubListVal(viewTvm,"TV.MATUTI", Matuti, 1); // 25.5.07 rpowner statt matuti!!

while(rc == 0) {

LibPrd = ocxBase.GetRubVal(RubLibPrd, 0);
Qty = ocxBase.GetRubVal(RubQty, 0);
Charge = ocxBase.GetRubVal(RubCharge, 0);
Ch_Date = ocxBase.GetRubVal(RubDate, 0);
Matuti = ocxBase.GetRubVal(RubMatuti, 0);

if (Qty.charAt(0) == "-")
Qty = Qty.substring(1, Qty.length)

produkte[0][LibPrd] = 0;
// -------------------------------------------------------------
if (Ch_Date >= first_day) { // 2008
if(werte[0][LibPrd] == undefined)
werte[0][LibPrd] = parseInt(Qty);
else
werte[0][LibPrd] += parseInt(Qty);
} else // 2007
// -------------------------------------------------------------
if (Ch_Date >= last_year && Ch_Date < first_day){
if(werte[1][LibPrd] == undefined)
werte[1][LibPrd] = parseInt(Qty);
else
werte[1][LibPrd] += parseInt(Qty);
} else // 2006
// -------------------------------------------------------------
if (Ch_Date >= sec_year && Ch_Date < last_year) {
if(werte[2][LibPrd] == undefined)
werte[2][LibPrd] = parseInt(Qty);
else
werte[2][LibPrd] += parseInt(Qty);
}
// -------------------------------------------------------------

rc = ocxBase.ViewNextRecord(view);

}

/* --------------------------------------------------- Stammdatenblatt --------------------------------------------------------- */

var sHTML = ""; // definition der Ausgabe-Variable

startseite();

function startseite() {

navigation();
header1();

for(var p in produkte[0]) {

for(var i=0; i < 3; i++) {
if (werte[i][p] == undefined)
werte[i][p] = 0;
}
alert(werte[0][p]);
sHTML = sHTML + "<tr class='tdtable'>";
sHTML = sHTML + "<td style='border-bottom: solid 1px; border-right: solid 1px' valign='bottom' align='center'>"+ p +"</td>";
sHTML = sHTML + "<td style='border-bottom: solid 1px; border-left: solid 1px' valign='bottom' align='center'>" + werte[0][p] + "</td>";
sHTML = sHTML + "<td style='border-bottom: solid 1px; border-left: solid 1px' valign='bottom' align='center'>" + werte[1][p] + "</td>";
sHTML = sHTML + "<td style='border-bottom: solid 1px; border-left: solid 1px' valign='bottom' align='center'>" + werte[2][p] + "</td>";
sHTML = sHTML + "</tr>";
}

var ges_akt = werte.sum();
var ges_last = werte.sum();
var ges_vlast = werte.sum();

footer(ges_akt,ges_last,ges_vlast);
}

// Tabelle für den Kopf

function header1() {
sHTML = sHTML + "<table align='center' class='prettytable' width='98%'>";
sHTML = sHTML + " <tr>";
sHTML = sHTML + " <td style='border-bottom: solid 1px; border-right: solid 1px' valign='bottom' align='center'>Produkt</td>";
sHTML = sHTML + " <td style='border-bottom: solid 1px; border-left: solid 1px' valign='bottom' align='center'>" + akt_dat + "</td>";
sHTML = sHTML + " <td style='border-bottom: solid 1px; border-left: solid 1px' valign='bottom' align='center'>" + (akt_dat-1) + "</td>";
sHTML = sHTML + " <td style='border-bottom: solid 1px; border-left: solid 1px' valign='bottom' align='center'>" + (akt_dat-2) + "</td>";
sHTML = sHTML + " </tr>";
}

// Fußzeile mit dem gesamt

function footer(w1,w2,w3) {
sHTML = sHTML + "<tr>";
sHTML = sHTML + " <td style='border-bottom: solid 1px; border-right: solid 1px' align='center'><strong>Summe:</strong></td>";
sHTML = sHTML + " <td style='border-bottom: solid 1px' valign='bottom' align='center'>"+ w1 +"</td>";
sHTML = sHTML + " <td style='border-bottom: solid 1px' valign='bottom' style='border-left: solid 1px' align='center'>"+ w2 +"</td>";
sHTML = sHTML + " <td style='border-bottom: solid 1px' valign='bottom' style='border-left: solid 1px' align='center'>"+ w3 +"</td>";
sHTML = sHTML + "</tr>";
}

function navigation () {
sHTML = sHTML + "<table class='maintable' width='98%' align='center'>";
sHTML = sHTML + " <tr>";
sHTML = sHTML + " <td width='20%'>&nbsp;</td>";
sHTML = sHTML + " <td>";
sHTML = sHTML + "<table align='center' width='50%' border='0' cellspacing='0' cellpadding='0'>";
sHTML = sHTML + " <tr>";
sHTML = sHTML + " <td>&nbsp;</td>";
sHTML = sHTML + " <td>&nbsp;</td>";
sHTML = sHTML + " <td>&nbsp;</td>";
sHTML = sHTML + " <td>&nbsp;</td>";
sHTML = sHTML + " <td>&nbsp;</td>";
sHTML = sHTML + " </tr>";
sHTML = sHTML + "</table>";
sHTML = sHTML + "</td>";
sHTML = sHTML + "<td width='2%'>&nbsp;</td>";
sHTML = sHTML + "<td width='15%'>";
sHTML = sHTML + "<table align='right' class='prettytable' width='100%' border='0' cellspacing='0' cellpadding='0'>";
sHTML = sHTML + " <tr>";
sHTML = sHTML + " <td><a href='charge_detail.html'><div class='navi' align='center'>Detail</div></a></td>";
sHTML = sHTML + " </tr>";
sHTML = sHTML + "</table>";
sHTML = sHTML + "</td>";
sHTML = sHTML + "</tr>";
sHTML = sHTML + "</table> ";
}

ocxBase.deleteView(view);
ocxBase.deleteView(viewEx);
ocxBase.deleteView(viewTvm);

eltAction.innerHTML = sHTML;
</SCRIPT>

<BODY>

<DIV id=eltAction></div>
</BODY>
</HTML>

ich weiß nicht was ich falsch mache die ausgabe sieht
so aus


Bezeichnung | 2008 | 2007
------------------------------
sum | function() { var sum = 0; for(var a in this) if(this.hasOwnProperty(a))sum += this[a]; return sum; }|function() { var sum = 0; for(var a in this) if(this.hasOwnProperty(a))sum += this[a]; return sum; }
Prod1 | 3 | 5
Prod2 | 6 | 3
------------------------------
ges | 17 | 17

also der Prototyp wird falscherweise als eigene zeile eingefügt und die Summe ist die Summe aller (also 3+6+5+3)

Ich verzweifle noch. Hat hier jemand einen Tipp für mich

Würde mich sehr freuen

Viele Grüße
Stefan
 
Moin Stefan,

ohne mir das jetzt im Detail angesehen zu Haben, würde ich einen Namenskonflikt vermuten. Benenne mal die Variable "sum" , die deine Methoden zurückgeben sollen, um, denn sie heisst genauso wie die Methode selbst.
 
das ist der komplette Code, muss dazu sagen das über das object ocxBase die Werte aus der datenbank rausgelesen werden. Die Werte stimmen nach alert-Überprüfung

nur der erste alert ist falscherweise die funktion:

Alerttext :
"function() { var sum2 = 0; for(var a in this) if(this.hasOwnProperty(a))sum2 += this[a]; return sum2; }"

danach kommen z.B. 2,3,4,5 .....
 

Neue Beiträge

Zurück