Add CSS styles to the three first rows of a table - css

Given the following HTML, I want to add some CSS styles ONLY to the first .gold, .silver and .bronze rows, (having in mind some special behaviors I have commented in the code).
<table>
<tbody>
<!--
IMPORTANT:
Notice that here can appear an extra <tr class="my-rank"></tr>
(i.e.: if he user is logged in and he/she have a rank)
-->
<tr class="gold"></tr>
<tr class="silver"></tr>
<tr class="bronze"></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<!--
From this point, these <tr> elements are injected with AJAX (by a pager),
and the three first of ones, includes .gold, .silver and .bronze classes again.
I can't remove these "repeated" classes.
-->
<tr class="gold"></tr>
<tr class="silver"></tr>
<tr class="bronze"></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<!--
Here is another AJAX injection block, including again undesired .gold, .silver and .bronze classes...
-->
<tr class="gold"></tr>
<tr class="silver"></tr>
<tr class="bronze"></tr>
<tr></tr>
...
...
</tbody>
</table>
I tried several times with pseudo-selectors like ':first-of-type', :first-child, etc; (also combined with :not), with no luck.
Can someone point me to the right direction?

Just after posting this question, I found a solution (although it might not be the more elegant of the solutions):
Finally, I went with these CSS selectors:
tr.gold:nth-child(1),
tr.gold:nth-child(2) {
/* my custom styles */
}
tr.silver:nth-child(2),
tr.silver:nth-child(3) {
/* my custom styles */
}
tr.bronze:nth-child(3),
tr.bronze:nth-child(4) {
/* my custom styles */
}

Related

semantic ui - how to style table headings

So I have built a table in semantic-ui. very simple. however styles dont seem to apply to my table in certain aspects and i cant get the headings to map over the columns. see the below screenshot
the L column is fine but the others are a bit wonky and name is just well off. how can i adjust the styles so make this work?
I have tried to add a width to the div my table sits in but it just extends it without altering the table body or head
here is the code:
<div className="tableContainer">
<h1> Table </h1>
<table className="ui striped table">
<thead>
<tr>
<th className="headings">Ranks</th>
<th className="headings">Name</th>
<th className="headings">P</th>
<th className="headings">W</th>
<th className="headings">L</th>
</tr>
</thead>
<tbody>
{this.props.players.sort(function(a, b) {
return (a.rank) - (b.rank);
}).map(function(player, index) {
index +=1;
return <tr key={index} className="table-rows">
<td className="stats">{player.rank}</td>
<td className="stats">{player.name}</td>
<td className="stats">{player.played}</td>
<td className="stats">{player.wins}</td>
<td className="stats">{player.losses}</td>
</tr>
}, this)}
</tbody>
</table>
</div>
If you are working in Reactjs, you should use Semantic-UI's reactjs package: Semantic-UI React. In the Table section, you can see that certain properties are passed through props. You can set the column number with columns prop. And under the Table.Header tab, you can see that there's a className prop. You can use it to style your header. For reference, visit: Semantic-UI React Table.

Changing CSS classes within Ember and SortMixin

I have a table which I have applied sorting to but to complete this task I would like the classes to change to show carets going up or down based on the sorting.
As you can see I have used the standard SortableMixin within Ember to get the sorting functionality but I'm having trouble changing the class of the individual element which has been clicked.
App.CampaignsController = Ember.ArrayController.extend({
sortProperties: ["id"],
sortAscending: true,
actions: {
sortBy: function(property){
if (this.get("sortProperties")[0] === property){
this.toggleProperty("sortAscending");
} else {
this.set("sortProperties", [property]);
this.set("sortAscending", true)
}
}
}
});
The table I'm I've applied the actions to is below:
<table class="table table-striped table-hover">
<thead>
<tr>
<th {{action "sortBy" "name"}}><i {{bind-attr class=":fa sortAscending:fa-caret-up:fa-caret-down"}}></i>Campaign Name</th>
<th {{action "sortBy" "campaign_code"}}><i {{bind-attr class=":fa sortAscending:fa-caret-up:fa-caret-down"}}></i>Campaign Code</th>
</tr>
</thead>
<tbody>
<td>{{name}}</td>
<td>{{campaign_code}}</td>
</tbody>
</table>
I'm using the sortAscending boolean to dictate what CSS class will appear. My problem is if I click the first heading, the classes on the second heading also change.
How do I get the CSS to change only on the heading that I have clicked?
Your <th> now has state (whether it's sorted, and whether it's ascending or descending), so you should wrap it up in a component. Something like this
<table>
<thead>
<tr>
{{#sortable-th property='name' action='sortBy'}}
Campaign Name
{{/#sortable-th}}
</tr>
</thead>
</table>
The component's template
// templates/components/sortable-th.js
<th>
<i {{bind-attr class=":fa sortAscending:fa-caret-up:fa-caret-down"}}></i>
{{yield}}
</th>
and code
// components/sortable-th.js
export default Ember.Component.extend({
sortAscending: true,
click: function() {
this.toggleProperty('sortAscending');
this.sendAction('action', this.get('property'), this.get('sortAscending'));
}
}
That's just a rough outline, try the implementation yourself. But that's how I would start thinking about it.

Twitter bootstrap - Make a table row inactive

Hi I want to make a row in a table inactive. Even if it is easy to hack by myself and add a custom css with the same colour of the inactive elements of my current theme I think it can be handles by bootstrap itself.
Any suggestions?
Bootstrap has a few elements you might find helpful:
Use contextual classes to change row colors:
<!-- On rows -->
<tr class="active">...</tr>
<tr class="success">...</tr>
<tr class="warning">...</tr>
<tr class="danger">...</tr>
<tr class="info">...</tr>
<!-- On cells (`td` or `th`) -->
<tr>
<td class="active">...</td>
<td class="success">...</td>
<td class="warning">...</td>
<td class="danger">...</td>
<td class="info">...</td>
</tr>
http://getbootstrap.com/css/#tables-contextual-classes
Use contextual colors to change the color of text:
<p class="text-muted">...</p>
<p class="text-primary">...</p>
<p class="text-success">...</p>
<p class="text-info">...</p>
<p class="text-warning">...</p>
<p class="text-danger">...</p>
http://getbootstrap.com/css/#helper-classes-colors
The disabled element is primary for form elements and buttons. Check that out here,
I've created a custom class table-inactive:
.table-inactive td {
background-color: #ececec;
color: #b9b9b9;
}
Does the table row contain any inputs? If so, add the disabled HTML5 attribute to the input and perhaps make the text in that row greyed out to show it is inactive? Just an idea..
I.E.
<tr>
<td style="color:grey;">Sample Text</td>
<td><input type="text" disabled/></td>
</tr>
I have worked with Twitter Bootstrap a bit and did not see any of these features though.
Bootstrap 4 requires you to add "table-" to the class.
<tr class="table-danger">...</tr>
https://getbootstrap.com/docs/4.4/content/tables/#contextual-classes

Popovers don't work correctly on table rows using table-striped style

After upgrading to bootstrap 3 generating a popover on table rows has an unexpected hovering effect on the table row. Popovers don't behave correctly on table rows using table-striped style. Removal of the table-striped style is one fix but you lose that effect. Only tested in Chrome. The last working version was 2.1.1.
Bootstrap 2.2.2: http://jsfiddle.net/tomshaw/9N4fs/
Is there a way to give the popover a container to stop this effect? Or some similar technique?
<table class="table table-striped">
<thead>
<tr>
<th>Module</th>
<th>Controller</th>
<th>Action</th>
<th>Recorded</th>
</tr>
</thead>
<tbody>
<tr id="popover" data-content="Content One" title="System Information">
<td>Utilities</td>
<td>Transaction</td>
<td>Export</td>
<td>2012-12-26 11:58:10</td>
</tr>
<tr id="popover" data-content="Content Two" title="System Information">
<td>Utilities</td>
<td>Reports</td>
<td>Edit</td>
<td>2012-12-26 12:47:30</td>
</tr>
<tr id="popover" data-content="Content Three" title="System Information">
<td>Utilities</td>
<td>User</td>
<td>Edit</td>
<td>2012-12-26 12:48:27</td>
</tr>
</tbody>
</table>
// Weird table row table-striped hover effect. Broken in bootstrap
// 2.2.2 working in 2.1.1 Using selector option has same effect.
$('body').popover({placement:"top",trigger:"hover",selector:'tr[id=popover]'});
table {
margin:50px 5px 0 5px;
}
I'm not sure what behavior you are going for. When I removed the table-striped the behavior of the pop-up was the exact same. Also you are using mutliple ID tags with the same name in your tr s. I updated your example to fix a few of these things and I rearranged the popup to an area where I assumed that you wanted it. Let me know if that helps and please remember not to use multiple id's with the same name.
Here is what I did to move the popover:
Added:
.popover{
left: 0px !important;
}
Here is a working fiddle make sure you look at the changes in the HTML and the JS:
http://jsfiddle.net/9N4fs/30/

Style for last td of last tr of certain class in table

I need to apply style to last cell of last row of class "fose_table_row".
Table is next
<table class="fose_table">
<tr>
<th>1</th>
<th>2</th>
</tr>
<tr class="fose_table_row">
<td>3</td>
<td>4</td>
</tr>
<tr class="fose_edit_row">
<td colspan="4">56</td>
</tr>
<tr class="fose_table_row">
<td>7</td>
<td>8</td>
</tr>
<tr class="fose_edit_row">
<td colspan="4">90</td>
</tr>
</table>
Rows of class fose_edit_row are not visible rows so I need last of fose_table_row to be styled.
I tried .fose_table .fose_table_row:last-child td:last-child and .fose_table tr.fose_table_row:last-child td:last-child but that doesn't seem to work. Last or of class fose_edit_row gets styled. What is right way to do this? And is it even possible?
W3C :last-child
The :last-child pseudo-class represents an element that is the last child of some other element.
:last-child will get the last element of its parent and then it will apply style if it has class .fose_table_row. So CSS can't solve your problem.
A possible jQuery solution:
$('table.fose_table').find('tr.fose_table_row').eq(-1).addClass('extra-style');
DEMO
The only way, currently, to solve your problem (as I noted in the comments) is to use JavaScript. Using plain JavaScript, rather than a library, I'd suggest:
var foseTableRows = document.querySelectorAll('.fose_table_row'),
last = foseTableRows[foseTableRows.length - 1];
console.log(last);
last.classList.add('lastFoseTableRow');
JS Fiddle demo.
Coupled with CSS, to apply the specific styles in order that they're reusable elsewhere without manipulating the style property of the specific nodes/elements.

Resources