Adding +/- to collapse menu - html

Here's what I was able to get working with help from someone on here. However, I'd like to add it to include the -/+ when expanding/collapsing the rows.
Collapsing table with html
Code: http://jsfiddle.net/5BRsy/3/
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table2">
<th></th><th>server 1</th><th>server 2</th>
<tr><td class="btn">used</td><td>1gb</td><td>2gb</td></tr>
<tr><td class="expand1">drive 1</td><td class="expand1">0.5gb</td><td class="expand1">1gb</td></tr>
<tr><td class="expand1">drive 2</td><td class="expand1">0.5gb</td><td class="expand1">1gb</td></tr>
<tr><td class="btn2">available</td><td>1gb</td><td>2gb</td></tr>
<tr><td class="expand2">drive 1</td><td class="expand2">0.5gb</td><td class="expand2">1gb</td></tr>
<tr><td class="expand2">drive 2</td><td class="expand2">0.5gb</td><td class="expand2">1gb</td></tr>
<tr><td>total</td><td>2gb</td><td>4gb</td></tr>
</table>
CSS
.expand1 { display: none;
}
.expand2 { display: none;
}
JS
$(document).ready(function(){
$(".btn").click(function(){
$(".expand1").toggle();
});
$(".btn2").click(function(){
$(".expand2").toggle();
});
})

I updated your fiddle: http://jsfiddle.net/5BRsy/7/
Basically, you are adding some kind of html that you will use to represent your toggle. In your case I used <span>+</span>.
<tr><td class="btn"><span>+</span> used</td><td>1gb</td><td>2gb</td></tr>
Then, when you click the <span> (or the + sign I should say) you toggle the display of your content, as you were already doing, and then change your + to a -. Clicking it again toggles it back to the way things were.
$(document).ready(function(){
$(".btn span").click(function(){
if($(".btn span").html() == "+") {
$(".btn span").html("-");
} else {
$(".btn span").html("+");
}
$(".expand1").toggle();
});

To render the + and - you can try using :before pseudo-element, however it's just a trifle problem, the problem is your code, it's too messy indeed, I modified it much to make it more perfect. Fistly you don't need the classes expand1 and expand2 assigned to each td that way. You can take benefit of jQuery selectors to hide the rows initially and toggle the rows on click. Here is the modified code:
HTML:
<table>
<th></th><th>server 1</th><th>server 2</th>
<tr><td class="btn">used</td><td>1gb</td><td>2gb</td></tr>
<tr><td>drive 1</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td>drive 2</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td class="btn">available</td><td>1gb</td><td>2gb</td></tr>
<tr><td>drive 1</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td>drive 2</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td>total</td><td>2gb</td><td>4gb</td></tr>
</table>
CSS:
.btn:before {
content:'+ ';
vertical-align:middle;
}
.btn.expanded:before {
content:'- ';
vertical-align:top;
}
JS:
//hide the rows initially
$('.btn').closest('tr').nextUntil('tr:has(td.btn)')
.not(':last-child').toggle();
//register click handler on ready
$(document).ready(function(){
$(".btn").click(function(){
$(this).closest('tr').nextUntil('tr:has(td.btn)')
.not(':last-child').toggle();
$(this).toggleClass('expanded');
});
});
Demo.
You can see that the code is much more concise and cleaner. Here is some related references:
.closest()
:has()
.nextUntil()

Related

!important in css isn't working

I'm trying to override the background of my rows in the table
This is what I did:
<tr style="background-color:#000099 !important;">
and this was the result:
http://onlinehandytools.com/blah.php?board=EDE&level=AL&subject=biology&year=2010
I've spent 2 hours on that, I've googled everywhere nothing seems to work
The only thing that have worked is
<style>
td{ background-color:#000099 !important;" }
</style>
But I don't want to do this, I will have different background colors of rows.
I tried to make seperate classes for each kind of row but td.classname didn't work either
You don't have to add !important to inline css as it has the highest priority.
Just do this:
<tr style="background-color:#000099">
I think you want to add this to your kindo css file , because kindo script will overwrite your css:
kindo.common.min.css:line # 9
.k-grid td
this is the css for the td just add your background color.
Edit:
Maybe this is what you are searching for:
$(document).ready(function(){$(".k-grid td:contains('Jan')").css("background-color","#000099");});
this is how it looks
Modify your html to this
<tr class="grayRow" > ... </tr>
<tr > ... </tr>
and then just set your style in css file like this
tr{
background-color: #fff;
}
tr.grayRow{
background-color: #ddd;
}
It shout be worked.
If you will change style from your css file NEVER use !important; flag on element in your html.
like this
<tr style="background-color:#000099 !important;">
I have check your alternative rows is added a class "k-alt" in <tr> with color #f5f5f5; Even you have <tr style="background-color:#000099 !important;"> , it will override by the class, because HTML parse out 1st then only insert the class "k-alt" by function. I suggest that you using jquery remove the "k-alt" or replace the "k-alt" background color by jquery.( I have see ur webpage have install Jquery Libary)
Replace the background
<script type="text/javascript">
$(function() {
$(".k-alt").css("background","#000099");
});
</script>
Or Remove the background
<script type="text/javascript">
$(function() {
if($("tr").hasClass('k-alt')){
$("tr").removeClass('k-alt');
}
});
</script>

Coloring rows in a table with n colors

I know I can use odd and even to color alternating rows in a table. However, I'd like to be able to color every third row, so the coloring goes like red-green-blue-red-green-blue.
Furthermore, I'd like to make that general and use n colors to style every n:th row.
At the moment, I generate the table dynamically and for each iteration, I put in a class name like modulo0, modulo1 etc. on every td tag.
Is there a better way? A more automagical one, that is.
You want to use the :nth-child selector with 3n, a number can be added here to get every second starting from the start, second or third.
jsFiddle
HTML
<table>
<tr><td>R</td></tr>
<tr><td>G</td></tr>
<tr><td>B</td></tr>
<tr><td>R</td></tr>
<tr><td>G</td></tr>
<tr><td>B</td></tr>
</table>
CSS
tr:nth-child(3n+1) td {
background-color:red;
}
tr:nth-child(3n+2) td {
background-color:green;
}
tr:nth-child(3n) td {
background-color:blue;
}
Generally
More generally, replace 3 with the number of rows in which to change the colour.
tr:nth-child(2n) td { } /* every second row */
tr:nth-child(4n) td { } /* every fourth row */
tr:nth-child(10n) td { } /* every tenth row */
Support
Note that full support for CSS3 selectors only comes in at IE9. If support for IE8 is essential then an alternate solution is required, like manual classes or JavaScript.
Just use nth-child(3n+3). Keep in mind, that this is not a cross-browser solution. Here's a working example
May be the older way but if you don't have any problem with javascript can try below as well.
<body>
<script language="JavaScript" type="text/javascript">
function altrows(tableid,firstcolor,secondcolor,thirdcolor)
{
var rows = document.getElementById(tableid).getElementsByTagName("tr");
for(var i = 0; i < rows.length; i++)
{
i -= 1;
for (var b=0;b<=2;b++) {
i +=1;
if(b==0){ rows[i].bgColor = firstcolor; }
if(b==1){ rows[i].bgColor = secondcolor; }
if(b==2){ rows[i].bgColor = thirdcolor; }
}
}
}
</script>
<body onLoad="altrows('tb_detail','#FF0321','#44FF03','#2103FF')">
<table cellpadding="0" cellspacing="0" border="0" width="90%" id="tb_detail">
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
<tr><td>Item1</td></tr>
</table>
</body>

Replacing some values with an hyperlink with jQuery

I have the following html code:
<table id="MatrixTable">
<tr>
<td id="321"> 0 </td>
</tr>
</table
A. How can I replace the '0' text with an hyperlink when mouseover with jQuery like the following:
<table id="MatrixTable">
<tr>
<td id="321">
<a class="modal-dialog-link" href="Edit?matrixID=321" updatefunction="UpdateMatrix">
0
</a>
</td>
</tr>
</table>
$("table#MatrixTable td").mouseover(function () {
// doing something here...
});
B. How can I come back to the original '0' when mouseleave with jQuery like the following:
$("table#MatrixTable td").mouseleave(function () {
// doing something here...
});
Thanks.
Use jQuery.hover
$("table#MatrixTable #321").hover(function () {
$(this).html('<a class="modal-dialog-link" href="Edit?matrixID=321"'+
'updatefunction="UpdateMatrix">0</a>');
},function(){
$(this).text('0');
});
You can use hover to bind an event handler to the mouseenter and mouseleave events, and you can use wrap and unwrap to wrap the contents in an a element:
​$("#321").hover(function() {
$(this).contents().wrap("<a>");
}, function() {
$(this).find("a").contents().unwrap();
});​​​​​
Here's a working example. Inspect the DOM to see the changes as you hover over the element.
This seems like a very strange way to use a link though... why can't the link always be in the DOM?

html - table row like a link

I can't set my table row as link to something. I can use only css and html. I tried different things from div in row to something another, but still can't make it works.
You have two ways to do this:
Using javascript:
<tr onclick="document.location = 'links.html';">
Using anchors:
<tr><td>text</td><td>text</td></tr>
I made the second work using:
table tr td a {
display:block;
height:100%;
width:100%;
}
To get rid of the dead space between columns:
table tr td {
padding-left: 0;
padding-right: 0;
}
Here is a simple demo of the second example: DEMO
I made myself a custom jquery function:
Html
<tr data-href="site.com/whatever">
jQuery
$('tr[data-href]').on("click", function() {
document.location = $(this).data('href');
});
Easy and perfect for me. Hopefully it helps you.
(I know OP want CSS and HTML only, but consider jQuery)
Edit
Agreed with Matt Kantor using data attr. Edited answer above
If you're on a browser that supports it you can use CSS to transform the <a> into a table row:
.table-row { display: table-row; }
.table-cell { display: table-cell; }
<div style="display: table;">
<a href="..." class="table-row">
<span class="table-cell">This is a TD... ish...</span>
</a>
</div>
Of course, you're limited to not putting block elements inside the <a>.
You also can't mix this in with a regular <table>
If you have to use a table, you can put a link into each table cell:
<table>
<tbody>
<tr>
<td>John Smith</td>
<td>123 Fake St</td>
<td>90210</td>
</tr>
<tr>
<td>Peter Nguyen</td>
<td>456 Elm Ave</td>
<td>90210</td>
</tr>
</tbody>
</table>
And make the links fill up the entire cells:
table tbody tr td a {
display: block;
width: 100%;
height: 100%;
}
If you are able to use <div>s instead of a table, your HTML can be a lot simpler, and you won't get "gaps" in the links, between the table cells:
<div class="myTable">
<a href="person1.html">
<span>John Smith</span>
<span>123 Fake St</span>
<span>90210</span>
</a>
<a href="person2.html">
<span>Peter Nguyen</span>
<span>456 Elm Ave</span>
<span>90210</span>
</a>
</div>
Here is the CSS that goes with the <div> method:
.myTable {
display: table;
}
.myTable a {
display: table-row;
}
.myTable a span {
display: table-cell;
padding: 2px; /* this line not really needed */
}
The usual way is to assign some JavaScript to the onClick attribute of the TR element.
If you can't use JavaScript, then you must use a trick:
Add the same link to each TD of the same row (the link must be the outermost element in the cell).
Turn links into block elements: a { display: block; width: 100%; height: 100%; }
The latter will force the link to fill the whole cell so clicking anywhere will invoke the link.
Answer from sirwilliam fits me best. I improved the Javascript with support for hotkey Ctrl + LeftClick (opens page in new tab). Event ctrlKey is used by PC's, metaKey by Mac.
Javascript
$('body').on('mousedown', 'tr[url]', function(e){
var click = e.which;
var url = $(this).attr('url');
if(url){
if(click == 2 || (click == 1 && (e.ctrlKey || e.metaKey))){
window.open(url, '_blank');
window.focus();
}
else if(click == 1){
window.location.href = url;
}
return true;
}
});
Example
http://jsfiddle.net/vlastislavnovak/oaqo2kgs/
You can't wrap a <td> element with an <a> tag, but you can accomplish similar functionality by using the onclick event to call a function. An example is found here, something like this function:
<script type="text/javascript">
function DoNav(url)
{
document.location.href = url;
}
</script>
And add it to your table like this:
<tr onclick="DoNav('http://stackoverflow.com/')"><td></td></tr>
I know this question is already answered but I still don't like any solution on this page. For the people who use JQuery I made a final solution which enables you to give the table row almost the same behaviour as the <a> tag.
This is my solution:
jQuery You can add this for example to a standard included javascript file
$('body').on('mousedown', 'tr[url]', function(e){
var click = e.which;
var url = $(this).attr('url');
if(url){
if(click == 1){
window.location.href = url;
}
else if(click == 2){
window.open(url, '_blank');
window.focus();
}
return true;
}
});
HTML Now you can use this on any <tr> element inside your HTML
<tr url="example.com">
<td>value</td>
<td>value</td>
<td>value</td>
<tr>
When i want simulate a <tr> with a link but respecting the html standards, I do this.
HTML:
<table>
<tr class="trLink">
<td>
Something
</td>
</tr>
</table>
CSS:
tr.trLink {
cursor: pointer;
}
tr.trLink:hover {
/*TR-HOVER-STYLES*/
}
tr.trLink a{
display: block;
height: 100%;
width: 100%;
}
tr.trLink:hover a{
/*LINK-HOVER-STYLES*/
}
In this way, when someone go with his mouse on a TR, all the row (and this links) gets the hover style and he can't see that there are multiple links.
Hope can help someone.
Fiddle HERE
This saves you having to duplicate the link in the tr - just fish it out of the first a.
$(".link-first-found").click(function() {
var href;
href = $(this).find("a").attr("href");
if (href !== "") {
return document.location = href;
}
});
//Style
.trlink {
color:blue;
}
.trlink:hover {
color:red;
}
<tr class="trlink" onclick="function to navigate to a page goes here">
<td>linktext</td>
</tr>
Something along these lines perhaps? Though it does use JS, but that's only way to make a row (tr) clickable.
Unless you have a single cell with an anchor tag that fills the entire cell.
And then, you shouldn't be using a table anyhow.
After reading this thread and some others I came up with the following solution in javascript:
function trs_makelinks(trs) {
for (var i = 0; i < trs.length; ++i) {
if (trs[i].getAttribute("href") != undefined) {
var tr = trs[i];
tr.onclick = function () { window.location.href = this.getAttribute("href"); };
tr.onkeydown = function (e) {
var e = e || window.event;
if ((e.keyCode === 13) || (e.keyCode === 32)) {
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
this.click();
}
};
tr.role = "button";
tr.tabIndex = 0;
tr.style.cursor = "pointer";
}
}
}
/* It could be adapted for other tags */
trs_makelinks(document.getElementsByTagName("tr"));
trs_makelinks(document.getElementsByTagName("td"));
trs_makelinks(document.getElementsByTagName("th"));
To use it put the href in tr/td/th that you desire to be clickable like: <tr href="http://stackoverflow.com">.
And make sure the script above is executed after the tr element is created (by its placement or using event handlers).
The downside is it won't totally make the TRs behave as links like with divs with display: table;, and they won't be keyboard-selectable or have status text. Edit: I made keyboard navigation work by setting onkeydown, role and tabIndex, you could remove that part if only mouse is needed. They won't show the URL in statusbar on hovering though.
You can style specifically the link TRs with "tr[href]" CSS selector.
I have another way. Especially if you need to post data using jQuery
$(document).on('click', '#tablename tbody tr', function()
{
var test="something";
$.post("ajax/setvariable.php", {ID: this.id, TEST:test}, function(data){
window.location.href = "http://somepage";
});
});
Set variable sets up variables in SESSIONS which the page you are going to can read and act upon.
I would really like a way of posting straight to a window location, but I do not think it is possible.
Thanks for this. You can change the hover icon by assigning a CSS class to the row like:
<tr class="pointer" onclick="document.location = 'links.html';">
and the CSS looks like:
<style>
.pointer { cursor: pointer; }
</style>
This method is here to give you a choice. Old css trick: filling the parent with position absolute.
<table>
<tr style=position:relative>
<td><a href=# style=position:absolute;inset:0></a>some
<td>cells
<td>in
<td>a row
</table>
inset:0 is a shorthand for top:0;right:0;bottom:0;left:0
we put <a> inside first <td> because this is a good chance to keep validity: only <td> can be a child of <tr>. But you can place it anywhere in the table and it will work.
Can you add an A tag to the row?
<tr><td>
</td></tr>
Is this what you're asking?

How to position a liquid/elastic textbox next to a button?

I want to position a textbox and a button next to each other in an 'elastic' or 'liquid' way (what's the correct term?) like so:
(source: ocactus.org)
When placed in a container of arbitrary width (incl. browser window resizing), the button should right align and take up as much width as it requires while the textbox should use the remaining width. Unfortunately the button's width cannot be fixed in CSS as it depends on the caption (different actions, languages etc.).
What is a good solution for the above that works cross browser?
I was able to get this to work within a table (I know, but it works) where it would correctly handle page resizing as well as the value of the button changing:
<table class="elastic">
<tr>
<td><input class="textbox" type="text"></td>
<td class="button"><input type="button" value="Test Button"></td>
</tr>
</table>
There may be a <div> alternative out there for styling.
EDIT: I revamped my example to use the following style sheet:
.elastic { width:100%; }
.elastic .textbox { width: 100%; }
.elastic .button { width: 1%; }
It's hard to give a definitive answer without seeing some code but the following will size the input 50% of the browser width with the button next to it.
input {width:50%}
<input>
<button>save</button>
HTML:
<div class="widebox">
<input type="text" value="" />
<button>Button</button>
</div>
jQuery:
<script type="text/javascript">
$(document).ready(function(){
var w = $(".widebox");
$(".widebox input:text").css({ width: (w.width - (w.children("button:first").width + 20)) });
});
</script>
Note: Be sure to include the jQuery library in your document's <head>. For speed I recommend using the one hosted by Google: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
Here's a jQuery extension I wrote based on NGJ Graphics' answer. It takes into account multiple buttons such as Ok/Cancel options.
(function($) {
$.fn.extend({
widebox: function() {
var el = $(this);
var width = 0;
el.children("button").each(function() {
width += $(this).outerWidth( true );
});
el.children("input:text").css({ width: (el.width() - (width + 40)) });
}
});
})(jQuery);