Form Javascript driving me mad :(

OK. As long as we're on the same page, that I can't create the attributes. I can only set them from the existing options.

But it begs the question of how you want that mapping to be handled. I'm assuming you want it to be flexible, so you can add mappings of category to specific attributes, without having to edit the Javascript? Which would best be handled by having YAFT (Yet Another Table) which has something like:

category_pattern: field
attribute_id: join to attrbute table

... like ...

Beverage, 7 (ABV)
Hugh, 22 (Cheese)
Hugh, 23 (Grits)
Gifts, 3 (Gifts for her)
Gifts, 4 (Gifts for him)

... and the code could then simply be driven from that. Probably by having a little custom AJAX method to fetch the data, and load that up during page load.

And can you remind me why your original code is trying to set the attribute join element by label, rather than value? S0 "Gifts for her", rather than the PK of that row in the attributes table?

-- hugh
 
Last edited:
YAFT indeed ;) Sounds great.

Yes, you just saw a flaw in that code I posted. It should have been the raw id value for the attribute. The code was faulty enough that I didn't get to the point of noticing my mental hiccup.
 
OK - it'll take me a bit longer than the house quoted to add that.

Can you go ahead and create that list, Just needs to have those two fields, the pattern to match against the category, and the join element to the coresponding attribute. Then stock the data (cetagory, attributes, and the new cat/attr pairings) with some examples I can use.

-- hugh
 
I'm already up against it timewise to get this bit done, so it's fine without the extra table and just hardcoded like my abortive approach.

The ammo is already all there in the db in terms of categories and attributes. Just two hardcoded examples would be good, let's say:

- If category label contains 'Beverage' add "'Bottle Size', 'ABV' and 'Colour' attributes for the user to complete with values.
- And if category equals 'Food' then add 'Weight' attribute.

Contain and equals comparisons cover the bases and then I'm fine to build from there. Keeps it lean within within what we agreed too without countless hardcoded possibilities I'd have to come back about anyway.

Thanks again,

RW
 
OK, that's working.

But honestly, you need to drive this from a table. You really don't want to have to be tweaking the code every time you want to change those cat / attribute associations. Just need to add a third field, for 'condition', equals or contains (probably a dropdown with those strings as the value). Then you can add, change and remove those mappings without going anywhere neat the code.

And writing the code is the trivial and quick part. The time consuming part is figuring out what you need, and the structure of your app. Took me about 5 mins to write the code.

If you want to create a table and stock it, I can whip up the code in 10 mins.

-- hugh
 
This is the code so far ...

Code:
function doCategoryChange(el) {
   // don't actually use value
   var value = el.getValue();
   // get label
   var label = el.element.options[el.element.selectedIndex].text;
   var form = el.form;
   
   // use regular expression test(), so we can do /i case insensitive
   
   // exact match, so include ^ and $ anchors for start and end of string match
   if (label.test(/^food$/i)) {
      doSetAttribute('17', form);
   }
   
   // contains, so no anchors
   if (label.test(/beverage/i)) {
      doSetAttribute('1', form);
      doSetAttribute('16', form);
   }
}

function doSetAttribute(value, form) {
    // duplicate the group
      form.mockDuplicateGroup('37');
      // grab the repeat count for the element name, decrement 'cos number elements from 0
      repeatCount = form.repeatGroupMarkers.get('37') - 1;
      // update the attribute element with value
      form.formElements.get('Frontend_SKU_Attribute_Mapping___Attribute_' + repeatCount).update(value);
}
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top