[ 9 posts ]

Matthew Strube

  • Username: ScorNinja
  • Joined: Fri Jul 08, 2011 8:52 am
  • Posts: 6
  • Offline
  • Profile

custom dropDownOptions and updates

Post Posted: Fri Jul 08, 2011 9:23 am
+0-
Heya. I am new at YUI(only two days since I downloaded everything), and I am trying to create a price list in a table.

Each table row represents a picture frame, and in one of the columns, I have a dropdown menu with the sizes. Certain picture frames are available only in certain sizes, so each one needs a special dropdown menu. The information is set up where the 'label' is the size(8" x 10"), and the 'value' is the price($5.00). When a size is changed, I would like the next cell over to show the price.

Code:
YAHOO.example.Data = {
    retail: [
        {
            Frame: "BW1",
            Size:[  {label: "Choose Size",      value: "$0.00"},
                    {label: "3 1/2\" x 5\"",    value: "$13.00"}]
        },
        {
            Frame: "BW2",
            Size:[  {label: "Choose Size",      value: "$0.00"},
                    {label: "24\" x 36\"",      value: "$40.00"}]
        }]
}


Unfortunately, the dropdown box just shows: "[object Object], [object Object]..."

Code:
var myColumnDefs = [
            {key:"Frame"},
            {key:"Size", formatter:"dropdown", dropdownOptions:YAHOO.example.Data.retail.Size},
            {key:"Price"}
        ];


...and I don't know how to get the updates to work.

Matthew Strube

  • Username: ScorNinja
  • Joined: Fri Jul 08, 2011 8:52 am
  • Posts: 6
  • Offline
  • Profile

custom dropDownOptions and updates

Post Posted: Thu Jul 14, 2011 10:43 am
+0-
An idea I have to simplify this is as follows:

I create the array of all the sizes available. Then for each frame, I have an array of the prices for each of the frames. For each frame not available, I could put "N/A".

This fixes my problem with the dropDownOptions. But how do I make it where when the value of the dropDown is changed, the 'Price' cell of the same row is updated with the correct price of the frame?

Example code: http://pastie.org/2213721
(Check line 20)

Please help me.

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Fri Jul 15, 2011 6:18 am
+0-
Hi Matthew,

After reviewing your pastie code I saw a few minor items that you may want to address to help speed up your debugging. These things often crop up when cut/pasting code,
1. Your variables Size and Data aren't defined properly, you use ":" assignment and should use "=".
2. The Size variable is assigned inside of an anonymous function, "function(){ ..... }", you should remove that.
3. The price data stored in your "Data" array is defined as "Prices" but you refer to "Price" in myDS and myColumnDefs. These should be changed to "Prices" to match the data.

Those are the easy items.

The "Data" array you have created is considered to be complex data and not easily implemented in DataSource/DataTable. When using TYPE_JSARRAY the DataSource expects at some internal data level to find a simple JS array. This array should be an array of objects, where each object member (or property) name is defined as "keys" in DS and the column defs. Your "Data" array contains two object members (Frame and Prices) but "Prices" is another array of objects ... that is a no-no.

My second comment is I think you may be confusing "displaying" data in the DataTable with "editing" data in the DataTable. A lot of people get confused by YUI DataTable's "dropdown" formatter usage versus using inline cellediting "dropdown".

It really depends on what you are trying to do with you DataTable. Is your example being used for "order entry" where a user adds an order for a certain frame (i.e. BW2) and then selects a size (i.e. 4x6) and you then present a price to them? Or do you want to show what sizes / prices apply for a specific frame style?

You may want to consider re-thinking the data structures to reduce the Data array to a simple array (so DS can use it) and/or what you are doing with the dropdowns. Once you have that figured out many on this forum can help you with "formatters" versus "editors".

Todd

Matthew Strube

  • Username: ScorNinja
  • Joined: Fri Jul 08, 2011 8:52 am
  • Posts: 6
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Mon Jul 18, 2011 12:58 pm
+0-
Thank you Todd for the reply.

About the first 3 problems you mentioned, they are, I believe, mainly because I rewrote my code for my Pastie while cramming both my 'data' and 'function' into one.

As for the "Data" array, I guess I can make the 'Prices' be ["$1.00","$2.00"...]?

As for the table itself, all I want is to display the prices.

My main problem is that I can't get the update to work whatsoever. I am trying to make the 'Size' value of the data to change to the 'value' of the selected dropDown option. Then update the 'Price' cell with the new price.

Here is a new Pastie with my actual code(albeit shortened slightly for simplicity): http://pastie.org/2233482

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Thu Jul 21, 2011 8:28 am
+0-
Hi Matthew,

Don't know if you have resolved this yet. I looked over your pastie and it seems like the JS array that your DataTable based upon is still in the wrong form. I mentioned earlier that the DataTable has to be supplied a "simple" array, meaning that one entry point leads to all singular object members. The JS array you provide to DataSource ( named "retail" ) contains two records, but the "Price" member MUST be singular, and not an array.

If your intent is to display a table of prices, then your array could be something like;
Code:
var retail = [
   { Frame: "BW1", Size: 0, Price:13.00 },
   { Frame: "BW1", Size: 0, Price:14.00 },
   { Frame: "BW1", Size: 0, Price:22.00 },
   { Frame: "BW2", Size: 0, Price:14.00 },
   { Frame: "BW2", Size: 0, Price:15.00 },
   { Frame: "BW2", Size: 0, Price:16.00 },
   { Frame: "BW2", Size: 0, Price:24.00 }
];

This would process fine into DataSource and will display a DataTable perfectly fine.

The dropdownOptions array of prices is usually a completely different JavaScript Array from the DataTable array. We usually setup the dropdownOptions array as a separate array (i.e. hash) within JS. It cannot be embedded within DataTable's data array.

If you checkout the YUI DataTable examples and review all of the different options for DataSource, you will see that none of them contain "complex" arrays.

Hopefully this helps a little.

Todd

Matthew Strube

  • Username: ScorNinja
  • Joined: Fri Jul 08, 2011 8:52 am
  • Posts: 6
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Thu Jul 21, 2011 10:31 am
+0-
Once again, Todd, I thank you for the help you are giving me.

I have gotten no headway really since my last pastie... I now understand about the 'Prices' having to be a simple data types... But in this example(http://developer.yahoo.com/yui/examples/datatable/dt_cellediting.html), the 'colors' is an array... And it appears to work fine...

Here is actually what I am trying to accomplish:
Image

In the image above, I have the 'price_formatter' showing the price of the second size(on some that is N/A), but when I select a different size, nothing happens.

So I know that the dataTable can read from the 'Prices' array, but I can't get the update to work correctly.

My code is still the same as the previous pastie for now, I don't want to change the format of it to what you requested unless it is absolutely necessary, because as you can tell, there are a lot of frames and prices.

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Thu Jul 21, 2011 2:07 pm
+0-
Matthew,

I'm sure I am making this WAY too complicated (one of the many services I provide). The image helped convey what you are trying to do. My understanding is that your DataTable is really just meant to display prices. I assume the dropdown is intended so the user can see different prices only, for each specific frame and size. This isn't necessarily meant to serve as a "basket" or for "order processing". Is that correct ? Because there are slight differences. As for the "dt_cellediting" example, that one is a little confusing.

If every frame had exactly the same size / price combination this would be a piece of cake. You would just define a simple array (call it mySizes) that would contain the options and define a column as formatter:"dropdown" with dropdownOptions:mySizes. But if I understand correctly, each frame can have different sizes and potentially different prices per size. That is where it gets complicated.

I worked up an example from your pastie that is self-contained and handles different size/price combos for different frame sizes. It re-uses your "retail" array with some embellishments. Then use the "retail" array as a basis for creating the actual DataTable array, which is slightly different. I attach a listener to changes on the dropdown to update the "price" column on a change. The link to the source code on gist is https://gist.github.com/1098292. You should be able to drop this in and fire it off and it seems to work.

Should you want to do an order processing / shopping basket type thing that is a little more complicated, but doable.

Hope this is closer to what you were trying ...

Todd

Matthew Strube

  • Username: ScorNinja
  • Joined: Fri Jul 08, 2011 8:52 am
  • Posts: 6
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Fri Jul 22, 2011 7:04 am
+0-
Wow! Thanks for the help! I will work on this today and see what I can get done.

Matthew Strube

  • Username: ScorNinja
  • Joined: Fri Jul 08, 2011 8:52 am
  • Posts: 6
  • Offline
  • Profile

Re: custom dropDownOptions and updates

Post Posted: Fri Jul 29, 2011 8:53 am
+0-
Works great in Chrome and Firefox, but not so well in Internet Explorer. The 'Price' field never updates for some reason in IE 8... Haven't tested on IE7 nor IE9 yet...
  [ 9 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum