如何使用javascript添加类似事件id的计数值?

时间:2016-04-04 19:33:23

标签: javascript jquery html asp-classic

我有一个表格,其中有三列,即事件ID,事件和计数。可以有多个具有相同事件ID的行在Counts列下具有自己的计数值。我必须找出不同的事件ID,然后将它们相应的计数值相加,并显示具有不同事件ID,事件和计数的输出表,其中计数值将是可用计数的总和。我尝试按以下方式显示我想要做的表格形式:

+--------+--------------+----------+
|           Ashley Janes           |   
+--------+--------------+----------+
|Eventid | Event        | Count    |   
+--------+--------------+----------+
|  5     | Wash apple   |    10    |
|  2     | cook potatoes|    20    |
|  8     | cut chillies |     5    | 
+--------+--------------+----------+
|         Taylor Perry             |   
+--------+---------------+---------+
|  2     | cook potatoes |  30     |    
|  5     | Wash apple    |  40     |
|  8     | cut chillies  |  60     |

Now I have to add count values of corresponding event id and display it in a result table as following::

+--------+--------------+----------+
|Eventid | Event        | Count    |   
+--------+--------------+----------+
|  5     | Wash apple   |    50    |
|  2     | cook potatoes|    50    |
|  8     | cut chillies |    65    | 
+--------+--------------+----------+

到目前为止,我已经做了以下事情:

Response.Write "<div><table><tr>"
Response.Write"<td class='tdcell1'>Event ID</td><td class='tdcell1'>Count</td></tr>"
Response.Write"<tr><td align=center width= '15%'></td><td><div class= 'resultSum'></div></td></tr>"
Response.Write"</table><div>"

<script>
$(function() {
        GetEventIds('#displaytable')
    });
function GetEventIds(tableIdWithHash) {
    var eventIds = [];
    var eventSum = Number(0);
        var count = Number(0);

    $(tableIdWithHash).find('.Idevent').each(function(i){
    var eId = $(this).html();
    if (eventIds.indexOf(eId) === -1)
        eventIds.push($(this).html());

        if (eId = eventIds)
            count++;
                eventSum += Number($(this).html());
            $(".resultSum").html(eventSum);
    });
    alert('EventIds: ' + JSON.stringify(eventIds)); 
    return eventIds;
}           
</script>
    Code for the table need to be parsed.
<table id= 'displaytable' width='35%' border=1 style= 'margin-bottom:35px; margin-top:35px;'>
    <tr>
        <td class='tdcell1'>Event ID</td>
        <td class='tdcell1'>Event</td>
        <td class='tdcell1'>Count</td>
    </tr>
    <tr style='font-size:13px;'>
        <td align='right' class='tdcell1' colspan='3'>Ashley Janes</td>
    </tr>
    <tr style='font-size:13px;'>
        <td align=center class='Idevent' width= '15%'> 5 </td>
        <td align=center class='eventNames' width= '35%'> Wash Apple </td>
        <td class='eventCounts' align=center width= '15%'>11</td>
    </tr>
    <tr style='font-size:13px;'>
        <td align=center class='Idevent' width= '15%'> 2 </td>
        <td align=center class='eventNames' width= '35%'> Cook Potatoes </td>
        <td class='eventCounts' align=center width= '15%'>20</td>
    </tr>
    <tr style='font-size:13px;'>
        <td align='right' class='tdcell1' colspan='3'>Taylor Perry</td>
    </tr>
    <tr style='font-size:13px;'>
        <td align=center class='Idevent' width= '15%'> 2 </td>
        <td align=center class='eventNames' width= '35%'> Cook Potatoes </td>
        <td class='eventCounts' align=center width= '15%'>20</td>
    </tr>
    <tr style='font-size:13px;'>
        <td align=center class='Idevent' width= '15%'> 5 </td>
        <td align=center class='eventNames' width= '35%'> Wash Apple </td>
        <td  class='eventCounts' align=center width= '15%'>106</td>
    </tr>
</table>

我的代码为我提供了事件ID和计数之和的输出。我如何使用JavaScript?请帮忙。

1 个答案:

答案 0 :(得分:1)

看看这段代码,并按照您的预期运作。

Value

仍然需要有关$(function() { GetEventIds('#displaytable') }); //this is a bad function, because it does way more than it says. function GetEventIds(tableIdWithHash) { //a little helper //convert a value to int32. any non-numeric value -> 0 //only the low 32-bits are kept, if the value exceeds the range //and removes all bits after the dot (2.9 -> 2 and -2.9 -> -2) function int(v){ return v|0; } var eventSum = 0; var events = {}; $(tableIdWithHash).find('.Idevent').each(function(i){ var $this = $(this); var eId = $this.text().trim(); //finding the .eventCount-node of this row var count = int( $this.siblings('.eventCounts').text().trim() ); events[eId] = int( events[eId] ) + count; //still don't get what #resultSum should contain (logically) //the total amount of events, independent of the event-id? eventSum += count; }); $("#resultSum").html(eventSum); var eventIds = Object.keys(events); console.log('EventIds: ', eventIds); console.log('Event counts by Id: ', JSON.stringify(events)); return eventIds; } 的目的/含义的一些信息。我对此做了一个假设;我是对的吗?

正如我对该功能所评论的那样,这个功能的目的是什么?它比它说的更多。这可能会导致混淆/错误。也许你想将它拆分成多个函数,或者重命名它并删除返回值。

或者您可能想要将表解析为某些数据结构并对此进行操作?但是再次将JSON粘贴到后端可能比解析一些标记更明智。