通过javascript

时间:2015-11-09 12:12:51

标签: javascript java calendar

我正在编写代码以在选择月份和年份时动态添加行。它应显示包含该月所有日期的标签行。 我已经编写了用于在按钮点击上添加新行的代码,并且卡在添加日期以动态标记

<BODY>                  
    <INPUT type="button" value="Add Row" id="addRow" onclick="addRow('dataTable')" />    

    <TABLE id="dataTable" width="350px" border="1">

    </TABLE>
    <%
        int year= 2015,month = 10;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);       
        int numDays = calendar.getActualMaximum(Calendar.DATE);
        SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy"); 
        calendar.set(Calendar.DAY_OF_MONTH,1);      
      %>      
   <SCRIPT language="javascript">

        var count=1;
        var numDays = "<%=numDays %>"; 
        while(count <= numDays-1 ) { 
            count++;   
           // calendar.set(Calendar.DAY_OF_MONTH,count); //error in this part when uncommented

            $("#addRow").trigger("click");
        }

        function addRow(tableID) {

            var table = document.getElementById(tableID); 
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name="chkbox[]";
            cell1.appendChild(element1);     
            var cell2 = row.insertCell(1);          
            cell2.innerHTML = "<%= df.format(calendar.getTime()) %>"; //new date here 

            var cell3 = row.insertCell(2); 
            var element2 = document.createElement("input");
            element2.type = "text";
            element2.name = "txtbox[]";
            cell3.appendChild(element2); 
        }      

    </SCRIPT>
</BODY>

无法在循环中使用calendar.set(Calendar.DAY_OF_MONTH,count),因此每次点击都会增加日期。运行calendar.set时(Calendar.DAY_OF_MONTH,1);它显示图像中给出的输出 snapshot of output when setting DAY_OF_MONTH outside the loop

1 个答案:

答案 0 :(得分:1)

我找到了答案。实际上它是另一种锻炼方式。

已获取日历中的天数(从服务器日期开始)并为每个计数触发一个循环触发器,增加标签值的addrow按钮。

JS code =&gt;

            numDays = "<%= numDays%>";
            while (count <= numDays - 1) {
                  count = count + 1;
                  $("#btnAddRow").trigger("click");
            }

            function addRow(tableID) {
                var countCol = parseInt($('#dataTable').attr('data-countCol'), 10) || 1;
                countRow = countRow + 1;
                var index = $("#dataTable  tr:last").attr("id").split("_")[1];
                var rid = "r_" + (parseInt(index) + 1);
                var tempRow = $("#dataTable  tr:last").clone(true, true);
                tempRow.attr("id", rid);
                var checkCol = $('#dataTable tr:last td:first input');
                checkCol.prop("check", true);
                checkCol.prop("id", "check-" + index);
                var dateCol = $('#dataTable tr:last td:nth-child(2) input');
                dateCol.prop("readonly", true);
                var pv_dt = dateCol.val().split("/");
                pv_dt[0] = parseInt(pv_dt[0]) + 1;
                var nw_dt = pv_dt.join("/");
                $(tempRow).find('td:nth-child(2) input').val(nw_dt);
                tempRow.find('td:nth-child(3) input').prop('name', "rom_" + parseInt(countRow) + "." + countCol); 
                tempRow.find('td:nth-child(4) input').prop('name', "waste_" + parseInt(countRow) + "." + countCol); 
                tempRow.find('td:nth-child(5) input').prop('name', "fh_" + parseInt(countRow) + "." + countCol); 
                tempRow.find('td:nth-child(6) input').prop('name', "ot-rom_" + parseInt(countRow) + "." + countCol); 
                tempRow.find('td:nth-child(7) input').prop('name', "ot-waste_" + parseInt(countRow) + "." + countCol); 
                tempRow.find('td:nth-child(8) input').prop('name', "ot-FH_" + parseInt(countRow) + "." + countCol); 
                $('#dataTable').append(tempRow);
            }