在表中汇总列值

时间:2017-01-20 05:48:15

标签: javascript php jquery

我试图将“Total Work Assigned”和“Not Called”列的值相加。

这是我的table HTML

<table border=1 id="category">
    <tr>
        <th>User Name</th>
        <th>User Belongs</th>
        <th>Total Work Assigned</th>
        <th>Not Called</th>
    </tr>
    <tr>
        <td>vidyaranyapura</td>
        <td>Category</td>
        <td>172</td>
        <td>156</td>
    </tr>
    <tr>
        <td>sahasra</td>
        <td>Company</td>
        <td>500</td>
        <td>350</td>
    </tr>
    <tr>
        <td>global</td>
        <td>Not Assigned</td>
        <td>0</td>
        <td>0</td>
    </tr>
</table>

这里是片段

$('#category tr td').text(function(i,el){
   console.log(parseInt(el,10));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table border=1 id="category">
    <tr>
        <th>User Name</th>
        <th>User Belongs</th>
        <th>Total Work Assigned</th>
        <th>Not Called</th>
    </tr>
    <tr>
        <td>vidyaranyapura</td>
        <td>Category</td>
        <td>172</td>
        <td>156</td>
    </tr>
    <tr>
        <td>sahasra</td>
        <td>Company</td>
        <td>500</td>
        <td>350</td>
    </tr>
    <tr>
        <td>global</td>
        <td>Not Assigned</td>
        <td>0</td>
        <td>0</td>
    </tr>
</table>
<h3>MY EXPECTED OUTPUT</h3>
<table border=1 id="same_id_wont_work_changed_for_demo_only">
    <tr>
        <th>User Name</th>
        <th>User Belongs</th>
        <th>Total Work Assigned</th>
        <th>Not Called</th>
    </tr>
    <tr>
        <td>vidyaranyapura</td>
        <td>Category</td>
        <td>172</td>
        <td>156</td>
    </tr>
    <tr>
        <td>sahasra</td>
        <td>Company</td>
        <td>500</td>
        <td>350</td>
    </tr>
    <tr>
        <td>global</td>
        <td>Not Assigned</td>
        <td>0</td>
        <td>0</td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td>672</td>
        <td>506</td>
    </tr>
</table>

jsfiddle我的预期输出:https://jsfiddle.net/2g29c8uv/16/

2 个答案:

答案 0 :(得分:1)

我想你想做的是这样的事情:

DEMO:https://jsfiddle.net/zxooa1j4/1/

var sum1 = 0;
var sum2 = 0;
$("#category tr").not(':first').not(':last').each(function() {
  sum1 +=  getnum($(this).find("td:eq(2)").text());
  sum2 +=  getnum($(this).find("td:eq(3)").text());
  function getnum(t){
    if(isNumeric(t)){
        return parseInt(t,10);
    }
    return 0;
    function isNumeric(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
    }
  }
});
$("#sum1").text(sum1);
$("#sum2").text(sum2);

isNumeric()函数引自这个答案:

Is there any function like IsNumeric in javascript to validate numbers

HTML:

<table border=1 id="category">
  <tr>
    <th>User Name</th>
    <th>User Belongs</th>
    <th>Total Work Assigned</th>
    <th>Not Called</th>
  </tr>
  <tr>
    <td>vidyaranyapura</td>
    <td>Category</td>
    <td>172</td>
    <td>156</td>
  </tr>
  <tr>
    <td>sahasra</td>
    <td>Company</td>
    <td>500</td>
    <td>350</td>
  </tr>
  <tr>
    <td>global</td>
    <td>Not Assigned</td>
    <td>0</td>
    <td>0</td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td id="sum1"></td>
    <td id="sum2"></td>
  </tr>
</table>

希望这有帮助。

答案 1 :(得分:0)

根据您在小提琴中提供的PHP。实际上,当您循环输出值以输出到显示时,您可以实际累计值。

只需替换此foreach部分

即可
$table2 = '<table  id="all_category_data" class="table table-striped table-bordered table-hover"><tr><th>User Name</th><th>User Belongs to</th><th>Total Work Assigned</th><th>Not Called</th><th>Follow Up</th><th>Intrested</th><th>Not Intrested</th></tr>';

foreach($totalresult as $totalresults) {
    $table2 .= "<tr><td>".$totalresults['username'].
    "</td><td>".$totalresults['userbelongs'].
    "</td><td>".$totalresults['totalvalue'].
    "</td><td>".$totalresults['Notcalled'].
    "</td><td>".$totalresults['followUp'].
    "</td><td>".$totalresults['intrested'].
    "</td><td>".$totalresults['notIntrested'].
    "</td></tr>";
}

$table2. = '</table>';

echo $table2;

以下内容包括添加和输出

$table2 = '<table  id="all_category_data" class="table table-striped table-bordered table-hover"><tr><th>User Name</th><th>User Belongs to</th><th>Total Work Assigned</th><th>Not Called</th><th>Follow Up</th><th>Intrested</th><th>Not Intrested</th></tr>';
$totalValue = 0;
$notCalledValue = 0;
foreach($totalresult as $totalresults) {
    // addition of totalValue
    $totalValue = $totalValue + $totalresults['totalvalue'];
    // addition of notCalledValue
    $notCalledValue = $notCalledValue + $totalresults['Notcalled'];

    $table2 .= "<tr><td>".$totalresults['username'].
    "</td><td>".$totalresults['userbelongs'].
    "</td><td>".$totalresults['totalvalue'].
    "</td><td>".$totalresults['Notcalled'].
    "</td><td>".$totalresults['followUp'].
    "</td><td>".$totalresults['intrested'].
    "</td><td>".$totalresults['notIntrested'].
    "</td></tr>";
}
// output of totalValue and notCalledValue
$table2 .= "<tr><td>".
    "</td><td>".
    "</td><td>".$totalValue.
    "</td><td>".$notCalledValue.
    "</td><td>".
    "</td><td>".
    "</td><td>".
    "</td></tr>";

$table2. = '</table>';
echo $table2;

注意:这假设数据中的$totalresults['totalvalue']$totalresults['Notcalled']键是数字。