Solved

jquery:  select on highlight like excel AutoSum

Posted on 2013-06-03
2
530 Views
Last Modified: 2013-09-07
We are using a table structure and want to Auto SUM the cells that are highlighted.

Similar to this

<table>
<tr>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>

If the user highlights columns 1 and 2 a DIV will do the calculation and return 7.  If the user selects the entire row it will return 12 in the corner DIV.

Any thoughts on how to do this?

Thanks in advance...!

sk
0
Comment
Question by:skillilea
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39217540
Hi, see the working example here: http://jsfiddle.net/robert_schutt/EyD3J/ 

You can click on the individual cells or the row/column headers and selection will toggle, total will be calculated with each change.

The generated jsFiddle code for reference:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo by robert_schutt</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-git.js'></script>
<style type='text/css'>
.sel {
    background-color: red;
}
.tot {
    float: right;
    border: 2px solid green;
}
</style>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$('td').on('click', function(){
    $(this).toggleClass('sel');
    calcTot();
});
$('tr:first th').on('click', function(){
    $('tr:not(:first) td:nth-child('+($(this).index()+1)+')').toggleClass('sel');
    calcTot();
});
$('tr:not(:first) th').on('click', function(){
    $(this).closest('tr').find('td').toggleClass('sel');
    calcTot();
});

function calcTot() {
    var tot = 0;
    $('td.sel').each(function(){
        tot += 1 * $(this).text();
    });
    $('div.tot').html(tot);
}
});//]]>  
</script>
</head>
<body>
<table border="1">
<tr>
<th></th>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
</tr>
<tr>
<th>row 1</th>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<th>row 2</th>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
</table>
<div class="tot">
</div>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:skillilea
ID: 39473224
tnx tons!

sk
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now