简体   繁体   English

数据表每行特定单元的总和

[英]Datatable sum specific cells every row

I have a table that has some data on it and some of their cell must be sum into some other cells in the same row for each data row present. 我有一个表,上面有一些数据,对于每个存在的数据行,它们的一些单元格必须加到同一​​行的其他单元格中。 I'd like to make it dinamically at clientside to reduce some of the serverload since there are some millions of family rows paginated in some cases. 我想在客户端上使其动态地减少一些服务器负载,因为在某些情况下分页了数百万个家庭行。

The table is generated as follows: 该表生成如下:

<rich:subTable id="tb" var="fila" value="#{AdministrarDMPermanente.listaDistribucionRecHum}" rowKeyVar="rowk">
    <rich:column style="text-align:center">
        <h:outputText value="#{fila.familiasP}"
        id="family1#{rowk+1}" />
    </rich:column>
    <rich:column style="text-align:center">
        <h:outputText value="#{fila.familiasPA}"
        id="family2#{rowk+1}" />
    </rich:column>
    <rich:column style="text-align:center">
        <h:outputText value="#{fila.familiasPAS}"
        id="family3#{rowk+1}" />
    </rich:column>
    <rich:column style="text-align:center">
        <h:outputText value="cargando..." id="totalfam#{rowk+1}" />
        <h:outputText value="#{fila.totalFamilias}" />
    </rich:column>
</rich:subTable>

And I want it to be generated in some way more like: 我希望它以某种方式生成:

<rich:subTable id="tb" var="fila" value="#{AdministrarDMPermanente.listaDistribucionRecHum}" rowKeyVar="rowk">
    <rich:column style="text-align:center">
        <h:outputText value="#{fila.familiasP}"
        id="family1#{rowk+1}" />
    </rich:column>
    <rich:column style="text-align:center">
        <h:outputText value="#{fila.familiasPA}"
        id="family2#{rowk+1}" />
    </rich:column>
    <rich:column style="text-align:center">
        <h:outputText value="#{fila.familiasPAS}"
        id="family3#{rowk+1}" />
    </rich:column>
    <rich:column style="text-align:center">
        <h:outputText value="cargando..." id="totalfam#{rowk+1}" />
        <rich:jQuery
        selector="[id^='frmCalculoRHP'][id$='totalfam#{rowk+1}']"
        query="[id^='frmCalculoRHP'][id$='family1#{rowk+1}']+   
        [id^='frmCalculoRHP'][id$='family2#{rowk+1}']+
        [id^='frmCalculoRHP'][id$='family3#{rowk+1}']" />
        <ui:remove>
            <h:outputText value="#{fila.totalFamilias}" />
        </ui:remove>
    </rich:column>
</rich:subTable>

When I try this approach get some javascript code with brackets changed to entities: 当我尝试这种方法时,将一些带有括号的javascript代码更改为实体:

<script type="text/javascript">//<![CDATA[
 {
    var selector = "[id^=\'frmCalculoRHP\'\x5D[id$=\'totalfam1\'\x5D";
    try {
        selector = eval("[id^=\'frmCalculoRHP\'\x5D[id$=\'totalfam1\'\x5D");
    } catch (e) {}
    jQuery(selector).[id^='frmCalculoRHP'][id$='family1']+[id^='frmCalculoRHP'][id$='family2']+[id^='frmCalculoRHP'][id$='family3'];
}
//]]>
</script>

On my research I found that it could be done in some similar way to: 在我的研究中,我发现可以通过以下类似方式完成此操作:

function totalFrom() {
 var table = document.getElementById(document.querySelector('[id^="frmCalculoRHP:table"]').id);
 for (var i = 3; i < table.rows.length; i++) {
     var total = 0;
     var firstColumn = table.rows[i].cells[6]
     var input = firstColumn.getElementsByTagName('span')[0];
     var value = parseInt(input.textContent);
     if (!isNaN(value)) {
      total += value;
     }
     var secondColumn = table.rows[i].cells[7];
     var input = secondColumn.getElementsByTagName('span')[0];
     var value1 = parseInt(input.textContent);
     if (!isNaN(value1)) {
      total += value1;
     }
     var thirdColumn = table.rows[i].cells[8];
     var input = thirdColumn.getElementsByTagName('span')[0];
     var value2 = parseInt(input.textContent);
     if (!isNaN(value2)) {
      total += value2;
     }
     var qry="[id^='frmCalculoRHP'][id$='totalfam"+(i-2)+"']";
     var resu=document.getElementById(document.querySelector(qry).id);
     resu.textContent=total;
 }
}

content table row starts on 3rd row that's why in the script you can see var i = 3 . 内容表行从第三行开始,这就是为什么在脚本中可以看到var i = 3

Your concrete problem is caused by using EL in id attribute which depends on a variable which is only available during view render time and not during view build time. 您的具体问题是由在id属性中使用EL引起的,该属性取决于仅在视图渲染时可用而在视图构建时不可用的变量。

<rich:subTable ... rowKeyVar="rowk">
    <rich:column>
        <h:outputText id="family1#{rowk+1}" ... />
    </rich:column>
    <rich:column>
        <h:outputText id="family2#{rowk+1}" ... />
    </rich:column>
    <rich:column>
        <h:outputText id="family3#{rowk+1}" ... />
    </rich:column>
</rich:subTable>

The id attribute is evaluated during view build time. id属性是在视图构建期间评估的。 If you had inspected the generated HTML output, you'd have noticed that <h:outputText id="family1#{rowk+1}"> etc all generate the ID without the evaluted value of the EL expression like so <span id="formId:tableId:0:family1"> . 如果您检查了生成的HTML输出,则会发现<h:outputText id="family1#{rowk+1}">等都生成了ID,而没有EL表达式的求值,例如<span id="formId:tableId:0:family1"> Basically, the actual IDs in HTML DOM tree are not what your JavaScript code expected. 基本上,HTML DOM树中的实际ID 不是您的JavaScript代码所期望的。

This is elaborated in below related questions: 在以下相关问题中对此进行了详细说明:


As to the concrete functional requirement, summing a few integers is extremely cheap in modern hardware (done in less than a microsecond; thousand times would be still less than a millisecond). 至于具体的功能要求,在现代硬件中将几个整数相加是非常便宜的(完成时间不到一微秒;千次仍不到一毫秒)。 It doesn't make sense to delegate it to the client side. 将其委托给客户端没有任何意义。 You'd better spend time on fixing performance holes in other areas, such as attempting to eagerly display millions of records unfiltered and/or unpaginated. 您最好将时间花在修复其他方面的性能漏洞上,例如尝试急切显示数百万未过滤和/或未分页的记录。 Fixing that has a much bigger positive impact in order of magnitude. 修复在数量级上具有更大的积极影响。

Below should do good for you. 下面应该对你有好处。

<rich:subTable ...>
    <rich:column>
        <h:outputText value="#{fila.familiasP}" />
    </rich:column>
    <rich:column>
        <h:outputText value="#{fila.familiasPA}" />
    </rich:column>
    <rich:column>
        <h:outputText value="#{fila.familiasPAS}" />
    </rich:column>
    <rich:column>
        <h:outputText value="#{fila.familiasP + fila.familiasPA + fila.familiasPAS}" />
    </rich:column>
</rich:subTable>

If you really insist in delegating it to the client side, then better do like below with help of "abstract" style classes instead of narrow-mindedly with IDs, given that the parent <rich:dataTable> has a client ID matching your attempted selector [id^="frmCalculoRHP:table"] , and that you apparently can't use $ due to some awkward RichFaces-related jQuery conflict: 如果您确实坚持将其委托给客户端,那么最好在下面的“抽象”样式类的帮助下进行操作,而不是狭narrow地使用ID,因为父<rich:dataTable>的客户端ID与您尝试的选择器匹配[id^="frmCalculoRHP:table"] ,由于与RichFaces相关的一些jQuery冲突,您显然无法使用$

<rich:subTable ...>
    <rich:column>
        <h:outputText styleClass="family" value="#{fila.familiasP}" />
    </rich:column>
    <rich:column>
        <h:outputText styleClass="family" value="#{fila.familiasPA}" />
    </rich:column>
    <rich:column>
        <h:outputText styleClass="family" value="#{fila.familiasPAS}" />
    </rich:column>
    <rich:column>
        <h:outputText styleClass="family-total" />
    </rich:column>
</rich:subTable>
<script>
    jQuery("[id^='frmCalculoRHP:table'] .rich-subtable-row").each(function(index, row) {
        var total = 0;
        jQuery(".family", row).each(function(index, cell) {
            total += parseInt(jQuery(cell).text()) || 0;
        });
        jQuery(".family-total", row).text(total);
    });
</script>

The .rich-subtable-row is just the autogenerated <tr class> of a <rich:subTable> . .rich-subtable-row只是<rich:subTable>的自动生成的<tr class> <rich:subTable>

Obviously, run above script after the table in the DOM (eg inline, or on document ready, or on window load. 显然,在DOM中的表之后运行上述脚本(例如,内联,在文档就绪或在窗口加载时)。

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

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