0

我有这个 JS 文件,它使用 for 循环在数组(“arrSubjStrengthGrowthData”和 arrTeacherComments)中生成表格数据,并将这些数据发送到另一个 HTML 文件中。

问题是 for 循环正在用重复的数据填充数组。我需要一种方法来防止这个脚本用重复的 td 填充数组。

在下图中,您可以在表格中看到重复的 td

https://i.stack.imgur.com/CXWjV.png

        
    var html='';

    html = html + '<table cellpadding="0" cellspacing="0" border="0" style="width:100%;font-size:13px;">';
    html = html + '<tr>';
    html = html + '<td style="width:30%;">&nbsp;</td>';
    html = html + '<td style="width:35%;background-color: #cecece;text-align:center;padding-top:3px;padding-bottom:3px;">'+studFirstMiddle+'\'s areas of strength</td>';
    html = html + '<td style="width:35%;background-color: #cecece;text-align:center;padding-top:3px;padding-bottom:3px;">'+studFirstMiddle+'\'s areas for continued growth</td>';
    html = html + '</tr>';
    html = html + '<tr>';
    html = html + '<td style="width:30%;border-right: 1px solid #000000;border-bottom: 1px solid #000000;" valign="top">&nbsp;</td>';
    html = html + '<td style="width:35%;border-right: 1px solid #000000;border-bottom: 1px solid #000000;" valign="top">';
    html = html + '<table cellpadding="0" cellspacing="0" border="0" style="width:100%;">';

    for(var i=0;i<studStrengthData.length - 1;i++)
    {
        html = html + '<tr>';
        html = html + '<td style="padding-left:3px;padding-right:3px;">'+studStrengthData[i].strength_data+'</td>';
        html = html + '</tr>';
    }

    html = html + '</table>';
    html = html + '</td>';

    html = html + '<td style="width:35%;border-bottom: 1px solid #000000;" valign="top">';
    html = html + '<table cellpadding="0" cellspacing="0" border="0" style="width:100%;">';

    for(var i=0;i<studGrowthData.length - 1;i++)
    {
        html = html + '<tr>';
        html = html + '<td style="padding-left:3px;padding-right:3px;">'+studGrowthData[i].growth_data+'</td>';
        html = html + '</tr>';
    }

    html = html + '</table>';
    html = html + '</td>';
    html = html + '</tr>';

    html = html + '<tr>';
    html = html + '<td style="width:30%;background-color: #cecece;border-bottom: 1px solid #000000;text-align:center;padding-top:3px;padding-bottom:3px;">Subjects</td>';
    html = html + '<td style="width:35%;background-color: #cecece;border-bottom: 1px solid #000000;text-align:center;padding-top:3px;padding-bottom:3px;">'+studFirstMiddle+'\'s areas of strength</td>';
    html = html + '<td style="width:35%;background-color: #cecece;border-bottom: 1px solid #000000;text-align:center;padding-top:3px;padding-bottom:3px;">'+studFirstMiddle+'\'s areas for continued growth</td>';
    html = html + '</tr>';

    for(var i=0;i<arrSubjStrengthGrowthData.length;i++)
    {
        html = html + '<tr>';
        html = html + '<td valign="top" style="width:30%;border-right: 1px solid #000000;border-bottom: 1px solid #000000;padding:3px;">'+arrSubjStrengthGrowthData[i].crsName+'</td>';

        if(typeof arrSubjStrengthGrowthData[i].strengthVal == 'undefined' || typeof arrSubjStrengthGrowthData[i].strengthVal == '')
        {
            html = html + '<td valign="top" style="width:35%;border-right: 1px solid #000000;border-bottom: 1px solid #000000;padding:3px;">N/A</td>';
        }
        else
        {
            html = html + '<td valign="top" style="width:35%;border-right: 1px solid #000000;border-bottom: 1px solid #000000;padding:3px;">'+arrSubjStrengthGrowthData[i].strengthVal+'</td>';
            

        if(typeof arrSubjStrengthGrowthData[i].growthVal == 'undefined' || typeof arrSubjStrengthGrowthData[i].growthVal == '')
        {
            html = html + '<td valign="top" style="width:35%;border-bottom: 1px solid #000000;padding:3px;">N/A</td>';
        }
        else
        {
            html = html + '<td valign="top" style="width:35%;border-bottom: 1px solid #000000;padding:3px;">'+arrSubjStrengthGrowthData[i].growthVal+'</td>';
        }

        html = html + '</tr>';
    }

    html = html + '</table>';

    return html;
});

Handlebars.registerHelper('displayTeacherComments', function(arrTeacherComments) {
    var html='';

    html = html + '<table cellpadding="0" cellspacing="0" border="0" style="width:100%;font-size:12px;">';
    html = html + '<tr>';
    html = html + '<td style="padding-bottom: 3px;"><b>Comments:</b></td>';
    html = html + '</tr>';

    for(var i=0;i<arrTeacherComments.length;i++)
    {
        html = html + '<tr>';
        html = html + '<td>'+arrTeacherComments[i].crsName+': '+arrTeacherComments[i].commentVal+' - '+arrTeacherComments[i].teacherName+'</td>';
        html = html + '</tr>';

    }

    html = html + '</table>';

    return html;
});

function processStudProgReport(studentObject,key)
{
    var HbObject={};

    HbObject.studLastFirst = studentObject.studLastFirst;
    HbObject.studFirstMiddle = studentObject.studFirstMiddle;
    HbObject.homeroom = studentObject.homeroom;
    HbObject.schoolyearabbr = studentObject.schoolyearabbr;
    HbObject.storecode = studentObject.storecode;

    if(HbObject.storecode == 'Q1')
        HbObject.rptTerm = 'Quarter 1';
    else if(HbObject.storecode == 'Q2')
        HbObject.rptTerm == 'Quarter 2';
    else if(HbObject.storecode == 'Q4')
        HbObject.rptTerm = 'Quarter 3';
    else if(HbObject.storecode == 'Q4')
        HbObject.rptTerm = 'Quarter 4';
    else if(HbObject.storecode == 'S1')
        HbObject.rptTerm = 'Semester 1';
    else if(HbObject.storecode == 'S2')
        HbObject.rptTerm = 'Semester 2';

    if(studentObject.grade_level < 0)
        HbObject.gradeLevel = 'Pre-kindergarten Grade';
    else if(studentObject.grade_level == 0)
        HbObject.gradeLevel = 'Kindergarten Grade';
    else if(studentObject.grade_level == 1)
        HbObject.gradeLevel = 'First Grade';
    else if(studentObject.grade_level == 2)
        HbObject.gradeLevel = 'Second Grade';
    else if(studentObject.grade_level == 3)
        HbObject.gradeLevel = 'Third Grade';
    else if(studentObject.grade_level == 4)
        HbObject.gradeLevel = 'Fourth Grade';
    else if(studentObject.grade_level == 5)
        HbObject.gradeLevel = 'Fifth Grade';

    HbObject.principalName = studentObject.principal;
    HbObject.period = studentObject.ftermday + "-" + studentObject.ltermday;
    HbObject.schtermdays = studentObject.schtermdays;
    HbObject.currDate = run_date;

    HbObject.presentDaysCnt = studentObject.present_days;

    if(HbObject.presentDaysCnt == '' || HbObject.presentDaysCnt == 0)
        HbObject.presentDaysCnt = 0;

    HbObject.tardyDaysCnt = studentObject.att_days_tardy;

    if(HbObject.tardyDaysCnt == '' || HbObject.tardyDaysCnt == 0)
        HbObject.tardyDaysCnt = 0;

    HbObject.studStrengthData = studentObject.studStrengthData;
    HbObject.studGrowthData = studentObject.studGrowthData;

    var studCrsData = studentObject.courseDetails;
    var studSRLSData = studentObject.studSRLSData;
    var arrSubjStrengthGrowthData = [];

    for(var i=0;i<studCrsData.length - 1;i++)
    {
        var crsNumber = studCrsData[i].course_number;
        var crsName = studCrsData[i].course_name;

        tempObject = {};
        tempObject.crsName = crsName;

        for(var j=0;j<studSRLSData.length - 1;j++)
        {
            if(crsNumber == studSRLSData[j].crs_number)
            {
                tempObject.strengthVal = studSRLSData[j].strength_val;
                tempObject.growthVal = studSRLSData[j].growth_val;
            }
        }

        arrSubjStrengthGrowthData.push(tempObject);
    }

    HbObject.arrSubjStrengthGrowthData = arrSubjStrengthGrowthData;

    var teacherComments = studentObject.teacherComments;
    var arrTeacherComments = [];
    
       for(var i=0;i<studCrsData.length - 1;i++)
    {
        var crsNumber = studCrsData[i].course_number;
        var crsName = studCrsData[i].course_name;
        var teacherName = studCrsData[i].teacher_name;

        for(var j=0;j<teacherComments.length - 1;j++)
        {
            if(crsNumber == teacherComments[j].crs_number)
            {
                tempObject = {};
                tempObject.crsName = studCrsData[i].course_name;
                tempObject.commentVal = teacherComments[j].comment_val;
                tempObject.teacherName = studCrsData[i].teacher_name;

                arrTeacherComments.push(tempObject);
            }

        }
    }

  HbObject.arrTeacherComments = arrTeacherComments;


    //console.log('HbObject');
    //console.log(HbObject);

    populateReportHTML(HbObject,key);
}

function populateReportHTML(HbObject,key)
{
    var theTemplateScript = jQuery("#print-template-progrpt").html();
    var theTemplate = Handlebars.compile(theTemplateScript);
    var theCompiledHtml = theTemplate(HbObject);
    var articleID="#stu-"+key.slice(8, key.length);
    jQuery(articleID).html(theCompiledHtml);
    jQuery(articleID).show();
    
}```
4

0 回答 0