Solved

jquery:  select on highlight like excel AutoSum

Posted on 2013-06-03
2
521 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now