| Account | Balance | Limit | Available | |
|---|---|---|---|---|
| 1110-26-000487 Checking Account | -250.000 | 500.000 | 250.000 | Transfer |
| 1110-26-000487 Savings | 1.000.000 | 0 | 1.000.000 | Transfer |
| 1110-26-000487 Joint Account | 150.000 | 0 | 150.000 | Transfer |
| Total (USD): | 900.000 | 500.000 | 1.400.000 |
See the Pen Responsive tables using CSS Grid Layout by Gilli Sigurðs (@gilli) on CodePen.
## Why? Now you may be thinking “But Gilli, why do we use tables at all then? Wouldn't it be simpler to just use CSS grid all the time". Here are some of the reasons to use tables: * This is tabular data so using tables makes perfect sense. * For Accessibilities sake since it should be more understandable for screen readers and shouldn't surprise the user. * This approach can be used on an already existing codebase to make it responsive without having to rebuild everything. In fact you could probably get away with only adding CSS and not touching the html. * People are already familiar with tables and expect them. However if the content is much more readable without it then by all means go for it. ## Conclusion and caveats Overall I think CSS Grid Layout is incredibly powerful to transform content completely and adapt it to whatever scenario you need to. I haven't used this solution in a production environment yet so there might be some more problems that I haven't thought of but mostly I haven't see any big ones so far, (link:https://caniuse.com/#search=css%20grid%20layout http:// text: except for browser support of course). However here are a couple of caveats that you should keep in mind before using this: * You can't dump any table with any content into it and be good to go. You have to actually think about the content for each table and how it should fit. * It doesn't support huge tables without either creating monster stacks or hiding a lot of the columns.]]>{ "rules_out_of_stock" : {
"LABEL" : "Out of Stock",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "rules", "entity" ],
"ON" : [ "commerce_product_presave" ],
"IF" : [
{ "entity_is_of_type" : { "entity" : [ "commerce-product" ], "type" : "commerce_product" } },
{ "data_is" : { "data" : [ "commerce-product:commerce-stock" ], "value" : "0" } }
],
"DO" : [
{ "data_set" : { "data" : [ "commerce-product:status" ], "value" : 0 } }
]
}
}
This rule is the courtesy of akalata and was found [here](http://drupal.org/node/1689458#comment-6619434)
When enabled, this rule will disable product variations that are out of stock. So let's say you have a sweater with 3 different sizes: "Small", "Medium" and "Large". Once one of them reaches 0 in stock, the rule will disable that variation leaving only Medium and Large to choose from. So when the last available size runs out of stock the add to cart button will say: "Product not available"
Another rule found on the same page and created by [kirie](https://www.drupal.org/node/1689458#comment-6761182) is also pretty useful alongside this one since it enables the variation again once it is back in stock. Just in case you forget to enable it when adding more stock:
{ "rules_stock_enable_product_when_back_in_stock" : {
"LABEL" : "Stock: enable product when back in stock",
"PLUGIN" : "reaction rule",
"TAGS" : [ "Emoda" ],
"REQUIRES" : [ "rules", "entity" ],
"ON" : [ "commerce_product_presave" ],
"IF" : [
{ "entity_is_of_type" : { "entity" : [ "commerce-product" ], "type" : "commerce_product" } },
{ "data_is" : {
"data" : [ "commerce-product:commerce-stock" ], "op" : "\u003E", "value" : "0"
}
}
],
"DO" : [
{ "data_set" : { "data" : [ "commerce-product:status" ], "value" : 1 } }
]
}
}
## The Add to cart form
In it's current form the rule is already providing better UX because it causes the variations that are out of stock to disappear from the select list so users don't have to select it on it only to find out that it is out of stock, because it simply isn't there anymore.
So now that we have the rule implemented it is time to make the add to cart badge show up on the product list's so that we can implement the CSS hack and manipulate it to serve as a out of stock badge.
Start by going to "admin/structure/types" and click "Manage Display" for the variation type you want to create the badge for. Next click on "Product list" and find either "Product" or "Product variations" in the list and click on the select list for it's format and choose "Add to Cart form". Finally move the field so that it is positioned right below the product image.
Now save and check your product list. The add to cart form should now be visible below the product image like in the example image here to the right, although most likely it doesn't look very pretty.
(image: add-to-cart.jpg alt:Image saying "products not available)
## The CSS Trickery
Now that we have the add to cart form there we can begin hacking it to serve as a out of stock badge. You will have to add some CSS into your theme in order to hide the add-to-cart form when the product is in stock and only display it when out of stock.
Add the following code to the bottom of your themes stylesheet: (NOTE: If you are not using Commerce Kickstart the following code might not work for you, however you should be able to figure out what is going on and implement it yourself, given that you know some CSS)
.view-collection-products .field-type-commerce-product-reference {
padding: 0;
margin: 0;
}
.view-collection-products .field-type-commerce-product-reference form.commerce-add-to-cart .form-submit.form-button-disabled {
margin: 0;
margin-top: -120px;
background: #FF4747;
text-align: center;
width: 100%;
padding: 10px 0 10px 0;
-moz-border-radius: 0;
-webkit-border-radius: 0;
-khtml-border-radius: 0;
border-radius: 0;
}
.view-collection-products .field-type-commerce-product-reference form.commerce-add-to-cart {
padding: 0;
height: 1px;
}
.view-collection-products .commerce-add-to-cart.in-stock {
display: none;
}
(image: complete.jpg alt:Image showing a product that is sold out)
That's it! Now your products in the product list should look something like the image here to the right. However the text still says "Product not available". So all you have to do is change that to "Out of stock" or something like that, which I did it quickly using the Locale module since the website I was working on was in Icelandic anyway.
Now if you are using Commerce Kickstart you might want to use these styles as well since it will create the badge on the "All Products" page as well.
.page-products .field-type-commerce-product-reference {
padding: 0;
margin: 0;
}
.page-products .field-type-commerce-product-reference form.commerce-add-to-cart .form-submit.form-button-disabled {
margin: 0;
margin-top: -120px;
background: #FF4747;
text-align: center;
width: 100%;
padding: 10px 0 10px 0;
-moz-border-radius: 0;
-webkit-border-radius: 0;
-khtml-border-radius: 0;
border-radius: 0;
}
.page-products .field-type-commerce-product-reference form.commerce-add-to-cart {
padding: 0;
height: 1px;
}
.page-products .commerce-add-to-cart.in-stock {
display: none;
}
This is the first tutorial I create so I would love some feedback and stories from people who try this out. Shoot me a message on [twitter](http://twitter.com/gillisig) if you have any.]]>