Solved

jquery:  select on highlight like excel AutoSum

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
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)
Suggested Courses

734 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