Clicking on a radio button on product pages (for grouped product attributes) forces a little ajax loader (presumably to change over the single product for the form) and removes any JQ UI classes used for custom styling.

I have attached the original (before a radio is selected) and after.
Reading up on another blog about JQ UI itself, it mentioned something about 'bind' - but it's all a bit of my depth.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Oooh, that looks very sharp. It would be a very nice solution to #1180016: Attributes (colors e.g.) as images in product display ? as well.

What you're experiencing is the Forms API's AJAX refresh. When the throbber shows, it's because the form is being submitted, rebuilt, and re-rendered server side. We then return part of the form to be replaced in the page. It appears the re-rendered form does not include your classes, and the question is why. To know that I'd need to see how you're attempting to add the classes. If you do it through a form alter, the classes should be re-applied when the form is rebuilt. If you were just doing it via JavaScript after the page loaded, then you'd lose the classes.

latulipeblanche’s picture

Yes @gamepaused, show us how you did it !! :)

I'm looking for, how to get "Images as "select-list" of Add-to-cart-form" (http://www.drupalcommerce.org/node/466#comment-1411).

gamepaused’s picture

Hey!

Ok, so I used the standard method of adding the button JQ UI stuff as found on http://jqueryui.com/demos/button/
I think it inserts a few classes and labels dynamically, which you can then style up really easy.

Code added to top of the product tpl -

drupal_add_library('system', 'ui.tabs');
drupal_add_library('system', 'ui.button');

drupal_add_js('jQuery(document).ready(function(){jQuery("#tabs").tabs();});', 'inline');
drupal_add_js('jQuery(document).ready(function(){jQuery("#radio").buttonset();});', 'inline');

I also had to overide the radio and radios to add a radio ID to the forms.
I'm such a newb but it's almost working!

gamepaused’s picture

Again, I read something somewhere about 'keeping' dynamic classes when using external libraries using something called 'bind' - but I'm a designer and it gets a bit over my head.
Ubercart did things differently, so the classes remain.

gamepaused’s picture

Did anybody find a solution to this?

Thinking - it could be useful if we could add classes to sold out items too - so they could be styled differently (with no mouseover for example).

Damien Tournoud’s picture

Category: bug » support

@gamepaused: you are probably not loading your Javascript properly. It needs to go into a Drupal.behavior thingie.

See http://drupal.org/node/756722#behaviors for more.

gamepaused’s picture

Yeah - behavior was needed so that the jqui will work after any other calls.

Basically - adding this to a js file was all that was needed -

(function ($) {

Drupal.behaviors.mytheme = {
attach: function (context, settings) {
$('#radio', context).buttonset() ;
}
};

}(jQuery));

So in theory - you can style away to your hearts content :)

The little ajax spinner thing though - any ideas on how to style/position that? It happens so fast I can't see any output.

gamepaused’s picture

Status: Active » Fixed
gamepaused’s picture

Where is the best place to post how I achieved all this? Personal site with screenshots?
Hopefully somebody can direct it further (and less of an hack) if I post the results.

rszrama’s picture

If you do that we could link it in from DC.org and wherever we need. I'll tweet it up, too. : )

Maybe we can just keep a handbook here on d.o linking to various tutorials like Views Slideshow did for the one I posted to my blog.

latulipeblanche’s picture

@rszrama Do you think that this could work with the image-colors representing the colors of the products ?

gamepaused’s picture

I'm going to look into this too so will post up with the rest.
Ideally - would be great for somebody to check out my template.php stuff

xicom3’s picture

i dont knw.. not working for me :| any suggestion

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

MickL’s picture

first this didn't worked for me, too. then i figured out that it is important to set the drupal_add_library and the drupal_add_js!

so the full code to put in your productdisplay.tpl.php is:

<?php
drupal_add_library('system', 'ui.button');
drupal_add_js('jQuery(document).ready(function(){jQuery(".form-radio").button();});', 'inline');
?>

  	<script>
	(function ($) {
Drupal.behaviors.mytheme = {
attach: function (context, settings) {
$('.form-radio', context).button() ;
}
};

}(jQuery));
	</script>

(setted this up to the class .form-radio instead of #button)

AJen’s picture

Newbie question: So where do the images go? in the tpl?

timodwhit’s picture

@aussiejen did you figure out where images go?

A bigger newb question though: Where does the code go? I have looked at quite a bit of documentation but nothing quite covers how to handle this.

Thanks for the help.

MickL’s picture

Status: Closed (fixed) » Needs work

what i posted was in my productnode.tpl.php

then it is possible to give the labels background images with css.

but there was a big problem maybe someone could work this out?

every input has a id, e.g.: "edit-attributes-field-color-red". So in css the only way to give the label a background is:

label[for="edit-attributes-field-color-red"] {
   background-image:...
}

when clicking on a color all colors are rerendered and the id changes to "edit-attributes-field-color-red--2", "edit-attributes-field-color-red--3", "edit-attributes-field-color-red--4", ... and so on.

So after clicking on another color all background images are gone. so i needed to do some javascript for all of my 30 colors:
$('label[for^="edit-attributes-field-farbe-color-red"]').css('background-image','url(files/color/00.jpg)');

the little hook "^" says "all labels that start with edit-attributes-field-farbe-color-red"

I would please you "commerce team" to give the input's a class too which does not change. so its <input id="edit-attributes-field-color-red" class="edit-attributes-field-color-red"> and the class does not change after reloading the input fields.

when this is worked out i will write tutorial how to theme the (color) dropwdown into pictures.

rszrama’s picture

Status: Needs work » Closed (fixed)

I'm pretty sure that's a different issue than this one. This is just a core Drupal issue in that it increments IDs on AJAX refreshes. To get around it, I rely on classes (even otherwise unique classes that match element IDs) to target my styles. See for example our targeting of product fields injected into node displays.

palamidaras50’s picture

Great issue, it really helped me.

I would like also to add my final solution (thanks to all your posts):

1) I used a standard select list field for the color.
2) I added all the predefined web color values to that list (X11 color names - Aliceblue, Antique white,.....)
3) The client who inserts a product just selects a color from the list.
4) I used the following code to the node--product-display.tpl.php: (thanks to all your posts)

<?php
drupal_add_library('system', 'ui.button');
drupal_add_js('jQuery(document).ready(function(){jQuery(".form-radio").button({label:"."});});', 'inline');
?>

<script>
(function ($) {
Drupal.behaviors.mytheme = {
attach: function (context, settings) {
$('.form-radio', context).button();

var myradios = $('label.ui-button');
    myradios.each(function() {
      $(this).css('background',$(this).children(':first').text());
	  $(this).css('color',$(this).children(':first').text());    	  
    });
}
};

}(jQuery));
</script>
<?php

----------------

5) Style the ui.button in the css

You could use taxonomy instead of standard list of course, but my main point is that you don't have to use an image field. The little box gets the respective color just with the following jquery code :
$(this).css('background',$(this).children(':first').text());

Therefore the client doesn't have to upload separate images for each product (or external jpgs like 'url(files/color/00.jpg)). unless you really need it.

MishaZP’s picture

palamidaras50, I want to add this code to my e-shop. Write me please, what i need to edit in this. thanks in advance

Summit’s picture

Hi, still I do not get this to get images as select options...for image-site this is very importent. I know this issue is closed, but may be someone has made a module/sandbox to get this easy done?
greetings, Martijn

Summit’s picture

sorry- double post..