Solved

jquery:  select on highlight like excel AutoSum

Posted on 2013-06-03
2
565 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)

627 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