简体   繁体   English

JavaScript&HTML-在动态创建的类中修改动态创建的子类

[英]JavaScript & HTML - Modifying dynamically created subclasses within a dynamically created class

Problem: 问题:

I have a dynamically created HTML table, that is used for filling out time sheets. 我有一个动态创建的HTML表,用于填写时间表。 It is created programmatically - there is no formal control. 它是通过程序创建的-没有正式控制。 The design is a mix of CSS with text boxes being created through JavaScript. 该设计混合了CSS和通过JavaScript创建的文本框。 Now each 'row' of this table is in a class called 'divRow', and is separated from the others by having 'r' and the number of the row assigned to it as the class (ie 'divRow r1', 'divRow r2', etc.). 现在,此表的每个“行”都在一个名为“ divRow”的类中,并且通过将“ r”和为其分配的行号作为该类(例如,“ divRow r1”,“ divRow r2”)与其他表分开'等)。

Within each of these 'divRow's, I have cells in a class called 'divCell cc'. 在每个“ divRow”中,我有一个单元名为“ divCell cc”。 These do not have any identifiers in the class name. 这些在类名称中没有任何标识符。 At the very last cell, I have a 'Total' column, which ideally calculates the total of the row and then adds it into a dynamically created text box. 在最后一个单元格中,我有一个“总计”列,该列理想地计算了行的总数,然后将其添加到动态创建的文本框中。

What I have at the moment: 我目前所拥有的:

// Function to create textboxes on each of the table cells.
$(document).on("click", ".cc", function(){
    var c = this;
    if(($(c).children().length) === 0) { 
        var cellval = "";
        if ($(c).text()) {
            cellval = $(this).text();
            if(cellval.length === 0) {
                cellval = $(this).find('.tbltxt').val();
            }
        }
        var twidth = $(c).width() + 21;
        var tid= 't' + c.id;

        if(tid.indexOf('x17') >= 0){
            var thtml = "<input id='t" + c.id + "' type='text' Class='tbltxt' style='width: " + twidth + "px;' readonly />";
            eval(spproc(spcol(t[getx(c.id)],thtml,tid,twidth)));

            //var getRow = $(this).parent().attr('class'); - this gets the 'divRow r#' that it is currently on.

            var arr = document.getElementsByClassName('cc');
            var tot = 0;
            for(var i = 0; i<arr.length; i++){
                if(parseInt(arr[i].innerHTML) > 0){
                    tot += parseInt(arr[i].innerHTML);}
            }

            $('#t' + c.id).focus();
            $(this).children().val(tot);

        }else{
            var thtml = "<input id='t" + c.id + "' type='text' Class='tbltxt' style='width: " + twidth + "px;' />";
            eval(spproc(spcol(t[getx(c.id)],thtml,tid,twidth)));
            $('#t' + c.id).focus();
            $('#t' + c.id).val(cellval);
        }}
});

As you can see, when the user clicks on the 'divCell cc', it creates a text box if one is not present. 如您所见,当用户单击“ divCell cc”时,如果不存在,则会创建一个文本框。 If the user clicks on the 17th column ('x17'), then it runs the for loop, and assigns the value of the total to the text box. 如果用户单击第17列(x17),则它将运行for循环,并将合计的值分配给文本框。

What I need to happen: 我需要发生的事情:

So what happens now is that the last cell sums the total of each cell that has a value. 所以现在发生的是最后一个单元格求和每个具有值的单元格的总和。 However, they are not row-dependent. 但是,它们与行无关。 I need it to calculate based on the row that it is currently 'on'. 我需要它根据当前处于“打开”状态的行进行计算。 So if I'm calculating the 2nd row, I don't want the sum of the first, second and third being entered into the total, I just want the 2nd rows' values summed. 因此,如果我要计算第二行,则不希望将第一,第二和第三行的总和输入到总数中,而只希望对第二行的值求和。

What I've tried: 我尝试过的

I've tried looping through and using the 'divRow r#' number to try and get the items in the array that end in that number. 我尝试遍历并使用“ divRow r#”数字来尝试获取以该数字结尾的数组中的项目。 (cells are given an id of 'x#y#' and the text boxes assigned to those cells are given an id of 'tx#y#'). (单元格的ID为'x#y#',分配给这些单元格的文本框的ID为'tx#y#')。

I've tried getting elements by the cell class name, and then getting their parent class and sorting by that; 我试过通过单元类的名称获取元素,然后获取其父类并按其排序。 didn't get far though, keep running into simple errors. 并没有走远,请继续遇到简单的错误。

Let me know if you need more explanation. 让我知道是否需要更多说明。

Cheers, 干杯,

Dee.

For anyone else that ever runs into this issue. 对于遇到此问题的其他任何人。 I got it. 我知道了。 I put the elements by the row class into an array, and then using that array, I got the childNodes from the row class. 我将行类的元素放入数组中,然后使用该数组从行类中获取childNodes。 The reason the variable 'i' starts at 2 and not 0 is because I have 2 fields that are not counted in the TimeSheet table (Jobcode and description). 变量“ i”从2而不是0开始的原因是因为我有2个未在TimeSheet表中计数的字段(Jobcode和description)。 It's working great now. 现在效果很好。

Cheers. 干杯。

$(document).on("click", ".cc", function(){
    var c = this;
    if(($(c).children().length) === 0) { 
        var cellval = "";
        if ($(c).text()) {
            cellval = $(this).text();
            if(cellval.length === 0) {
                cellval = $(this).find('.tbltxt').val();
            }
        }
        var twidth = $(c).width() + 21;
        var tid= 't' + c.id;

        if(tid.indexOf('x17') >= 0){
            var thtml = "<input id='t" + c.id + "' type='text' Class='tbltxt' style='width: " + twidth + "px;' readonly />";
            eval(spproc(spcol(t[getx(c.id)],thtml,tid,twidth)));

            // Get current row that has focus
            var getRow = $(this).parent().attr('class');
            // Get the row number for passing through to the next statement
            var rowPos = getRow.split('r', 5)[1];
            // Get all the elements of the row class and assign them to the rowClass array
            var rowClass = document.getElementsByClassName('r' + rowPos)
            // Given the rowClass, get the children of the row class and assign them to the new array.
            var arr = rowClass.item(0).childNodes
            // Initialize the 'total' variable, and give it a value of 0
            var tot = 0;
            // Begin for loop, give 'i' the value of 2 so it starts from the 3rd index (avoid the Req Code and Description part of the table).
            for(var i = 2; i<arr.length; i++){
                if(parseInt(arr[i].innerHTML) > 0){
                    tot += parseInt(arr[i].innerHTML);}
            }
            // Assign focus to the 'Total' cell
            $('#t' + c.id).focus();
            // Assign the 'total' variable to the textbox that is dynamically created on the click.
            $(this).children().val(tot);
        }else{
            var thtml = "<input id='t" + c.id + "' type='text' Class='tbltxt' style='width: " + twidth + "px;' />";
            eval(spproc(spcol(t[getx(c.id)],thtml,tid,twidth)));
            $('#t' + c.id).focus();
            $('#t' + c.id).val(cellval);
        }}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM