Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-08-24 08:58:17

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,947
Website GitHub

Output javascript lookup tables on the fly

This is probably so simple, but the details are killing me. I’m iterating over a result set (from a shop database) and want to build a javascript lookup table from the information. Ideally I’d like to use jQuery’s .data() method, but it seems that’s only one-shot, i.e. each time round the loop if I use another .data() call it’ll overwrite the previous item if it has the same key. I want to append the values so that I end up taking this kind of input structure:

size=40, colour=red, stock=32
size=40, colour=green, stock=12
size=42, colour=red, stock=1
size=40, colour=blue, stock=0
size=44, colour=green, stock=5
...

and turning it into something javascripty like this as I iterate over each of the above rows:

size_matrix['40']['red'] = 32;
size_matrix['40']['green'] = 12;
size_matrix['42']['red'] = 1;
size_matrix['40']['blue'] = 0;
size_matrix['44']['green'] = 5

and

colour_matrix['red']['40'] = 32;
colour_matrix['green']['40'] = 12;
colour_matrix['red']['42'] = 1;
colour_matrix['blue']['40'] = 0;
colour_matrix['green']['44'] = 5

(or equivalent JSON / jQuery.data() structure)

Essentially I want to store it in a suitable format such that when someone chooses ‘40’ from a select list I can use jQuery to look up the stock quantities available in each colour from size_matrix, and vice versa: when they choose ‘red’ from the other select list I can look up the stock quantities available in each size from colour_matrix and display them.

At the moment, no matter what I try I keep getting javascript errors like size_matrix[40] is undefined or it just borks. I wonder if it’s because the first index is numeric and JS is assuming I should be numbering them starting from 0 instead of jumping in at slot 41 (or something like that).

I’m sure the solution is simple and I just need a nudge in the right direction on how to output the relevant data structures on the fly as I read each source row. Ultimately I don’t care what format the js info is stored in as long as I can ask the data structure for one size to return a list of colours+stock quantities, and look up one colour to get a list of sizes+stock quantities.

Many thanks in advance for any pointers.

Last edited by Bloke (2011-08-24 08:59:06)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Online

#2 2011-08-24 18:29:58

hakjoon
Member
From: Arlington, VA
Registered: 2004-07-29
Posts: 1,634
Website

Re: Output javascript lookup tables on the fly

So technically variables can’t start with numbers so it could be problematic. You could always prepend a $ or _ to the number and use that when looking up. So it would become size_matrix["$40"] or size_matrix["_40"]

That being said this does seem to work if you look it up using size_matrix["40"] (notice the quotes)


Shoving is the answer – pusher robot

Offline

Board footer

Powered by FluxBB