Dynamic dropdown / select

This is my data

Code…Max…Min
1 20 5
2 15 6
3 20 12

In code file, I set data type as ‘select’ and the dropdown shown 1…2…3 (Good)

Now for the Max…Min, I would like to show 20…19…18…7…6…5 for code 1 & 15…14…7…6 for code 2 respectively.

I think this can be done using code.onChange but how to do the max/min in dropdown dynamically?

Regards,
CK

Re: Dynamic dropdown / select

I read what you want, and I understand the number sequence, but I not sure exactly what you are asking. What do you want displayed in the popup? Is that what you are asking?

Regards.
Scott.

Re: Dynamic dropdown / select

Sorry, Actually there are 2 db’s,

1st is the master with 3 fields Code, Max & Min.
2nd is the trx with no. of fields, but only 2 fields are related to table 1. Code and xVar

In Table 2, when I select Code = 1 and onchange, I want xVar shown 20…19…18…7…6…5 as selection.
If I select Code = 2, then xVar should should show only 15…14…7…6

Re: Dynamic dropdown / select

I think this will perform what you want …

(Select 1) will have the 1,2,3

Choose AJAX processing for (Select 1), then choose the field (Select 2) that will be updated.

(Select 2) will have the selection set to auto and enter the SQL statement that will need to be executed based on the value of (Select 1).

This should auto populate (Select 2).

If not, let me know and I will setup a test app… please post the schema of the 2 tables.

Regards,
Scott.

Re: Dynamic dropdown / select

Edition Lookup for Code,
select Code, Max, Min from table1 order by Code

So this will show, 1…2…3 (This one no problem)

If I choose Code = 1, then xVar will have 20…19…6…5, So I can choose any value in this range and save it, for example, I choose 12 in xVar, So, the trx will have Code = 1 and xVar = 12

My question is, how to create the xVar to store the range 20…5 for selection.

Regards,
CK

Re: Dynamic dropdown / select

Several ways to populate (Select 2).

-Use JS event to to create a function that uses arrays to populate your control at runtime.

pseudo code:

var code1_array = [20, …];
var code2_array = [15, …];

var code_selected = document.F1.code[0];

// switch/elseif to select correct array based on code_selected

var select = document.F1.xVar[0];
select.options.length = 0; // clear out existing items
for(var i=0; i < code#_array.length; i++) {
var r = code1_array[i];
select.options.add(new Option(r.text, i));
}

  • Create an AJAX event to grab the values from a table. I would have to research how to update from SC, either HTML option of using a SC5 function.
  • Create 3 dropdowns and use manual values. Use sc_field_display() to determine which is displayed. (based on onChange of select 1)

Regards,
Scott.

Re: Dynamic dropdown / select

Scott,

Thanks for your reply.

Actually this Table will grown from time to time. Do you think and Code 1 Max / Min might change too.

Do you think HARD CODE is safe? What if my Code grown to 25 or maybe 50?

Regards,
CK

Re: Dynamic dropdown / select

I would most like create a table and then store the values in there for future growth. You could then pull the ranges from the table. I would use AJAX in this case to grab the ranges.

If you use straight JS, then you will have to create code to grab the data from the table.

Regards,
Scott.

Re: Dynamic dropdown / select

Scott,

Can I just store the Max (20) and Min (5) in db and let AJAX to create the pulldown base on the value read from the db?

How to write / pass the Max & Min to AJAX like Data Type = ‘Select’.

I just need an example to create & make this work.

Regards,
CK

Re: Dynamic dropdown / select

Give me a bit… I need to finish a few code projects and rebuild the carb on my mom’s mower then I will see what I can setup.

Scott.

Re: Dynamic dropdown / select

After some time on this. Here are the options that I see.

I have tested this option in a sample using different tables (sakila) as an example.
http://downloads.mysql.com/docs/sakila-db.zip (using actor/film_actor as my text tables)

back to your fields …

S1 = Code
S2 = Ranges

S1 = Select; auto: SELECT code from code_table; check AJAX Processing to update S2
S2 = Select; auto: SELECT code,xvar from ck_ranges WHERE code = ‘{s1_code_fieldname}’

new_table:
CREATE TABLE IF NOT EXISTS ck_ranges (
code int(11) NOT NULL,
xvar int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

– adding your ranges for each code
INSERT INTO ck_ranges (code, xvar) VALUES
(1, 20),
(1, 19),
(1, 18),

(2, 15),
(2, 14),
(2, 13),

(3, 12),
(3, 11),
(3, 10);

This will update S2 when S1 is changed.

The other option is to create an AJAX on change event on the code S1 and then write a function that grabs the min,max and then has to create a for/loop all the values and then use PHP in that event to populate S2.
http://www.tech-evangelist.com/2007/11/22/php-select-box/

That seems like a lot of extra work when you could just create a new table and be done with it.

The problem you have is that your min/max values will require you to create custom code to handle what you want. If you create the table in option 1, then you can use the built-in features of SC5 without having to create code to ‘explain’ the fields min/max to your form/controls.

Regards,
Scott.