Page 1 of 1

Another Dynamic Menu question [NOT REQUIRED]

PostPosted: Thu Sep 02, 2010 4:56 pm
by cantlep
Hiya,

I know this has been discussed many times before and I've seen a few "solutions" but nothing I can really follow (mainly due to my lack of JS knowledge and also differences in solutions over the years)..I'm hoping someone can help..

I have a table called "BodyParts". Within it contains BodyPartID and BodyPart.
I have a second table called "Exercises". Within it contains ExerciseID, Exercise and BodyPart

What I'd like is when someone selects the BodyPart from the dropdown (let's say "Chest"), all subsequent dropdowns for that record (of which there could be up to 6) are updated with only "Chest" related Exercises.

I know it'll require some JS (which is where I'll struggle) and possibly the use of the widget:atts:onchange function in fields.ini (this isn't mentioned in the wiki article here - http://xataface.com/wiki/index.php?-tab ... e_id=%3D90) but I'd just like some guidance if anyone can help.

If that's not difficult enough, there might be 1 dropdown menu that would need to be excluded from this functionality (but I know I might be asking too much there). If anyone can help with the above, that'd be fantastic!

Thanks in advance.

Paul

Re: Another Dynamic Menu question

PostPosted: Fri Sep 03, 2010 7:58 am
by cantlep
Hi Steve, In a few of your posts you've mentioned this:
One key that makes this possible is that you can add a third column to the SQL query of a valuelist that is treated as a category.


My valuelists.ini looks like this:

Code: Select all
[Exercises]
__sql__= "SELECT ExerciseID, Exercise, BodyPartID FROM Exercises"

[BodyParts]
__sql__= "SELECT BodyPartID, BodyPart FROM BodyParts"

My fields.ini looks lik this

Code: Select all
[BodyPart]
widget:label = "Body Part"
widget:type = select
vocabulary = BodyParts

[Exercise1]
widget:label = "Exercise 1"
widget:type = select
tab = Exercise_1
group = Exercise_1
vocabulary = Exercises

However, because BodyPartID doesn't actually exist in the Exercises table, I get this error.

Code: Select all
:  Valuelist query 'SELECT ExerciseID, Exercise, BodyPartID FROM Exercises' failed. On line 1206 of file /var/www/html/public/workout/dataface/Dataface/Table.php in function printStackTrace()\n<br>On line 3140 of file /var/www/html/public/workout/dataface/Dataface/Table.php in function _loadValuelistsIniFile()\n<br>On line 15 of file /var/www/html/public/workout/dataface/Dataface/FormTool/select.php in function getValuelist(Exercises)\n<br>On line 393 of file /var/www/html/public/workout/dataface/Dataface/FormTool.php in function buildWidget(Dataface_Record Object,array(Exercise1,varchar(60),NO,,,,WorkoutLog,array(Exercise 1,,WorkoutLog.Exercise1.label,WorkoutLog,Dataface_QuickForm Object,Exercise1,1)\n<br>On line 345 of file /var/www/html/public/workout/dataface/Dataface/QuickForm.php in function buildWidget(Dataface_Record Object,array(Exercise1,varchar(60),NO,,,,WorkoutLog,array(Exercise 1,,WorkoutLog.Exercise1.label,WorkoutLog,Dataface_QuickForm Object,Exercise1,1,array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, in /var/www/html/public/workout/dataface/Dataface/Table.php on line 1206, referer: http://www.blah.co.uk/index.php?-action=new&-table=WorkoutLog

And so it prevents the code from working
This is in my table delegate class:

Code: Select all
//Generate dynamic menu for Exercises

function block__after_new_record_form(){
echo <<<END
<script language="javascript"><!--
var slave_field= document.getElementById('Exercise1');
var master_field = document.getElementById('BodyPart');
END;
// Let's get all the slaves available.
$app =& Dataface_Application::getInstance();
$query =& $app->getQuery();
$table =& Dataface_Table::loadTable($query['-table']);
$slaves = $table->getValuelist('Exercises');
$slave_masters = $table->getValuelist('Exercises__meta');
// Note that the Exercises__meta valuelist is automatically created
// because we had three columns in the slaves valuelist.
// The first and third columns effectively create a 2nd valuelist
// named 'Exercises__meta'

// $slaves is an array with keys ExerciseID and values Exercise
// slave_masters is an array with keys ExerciseID and values BodyPartID

import('Services/JSON.php');
$json =& new Services_JSON(); // A JSON encoder to allow us to easily
// convert PHP arrays to javascript arrays.
echo '
var slaves_options = '.$json->encode($slaves).';
var slaves_master = '.$json->encode($slave_masters).';
';

echo <<<END
master_field.onchange = function(){
var selected_master = master_field.options[master_field.selectedIndex].value;
slave_field.options.length = 0;
var index = 0;
for ( ExerciseID in slaves_options){
if ( selected_master == slaves_master[ExerciseID] ){
slave_field.options[index++] = new Option(slaves_options[ExerciseID], ExerciseID);
}
}
};
//--></script>
END;
}

// Also place this javascript after an edit record form...
function block__after_edit_record_form(){
return $this->block__after_new_record_form();
}

Can you help?
Cheers

Re: Another Dynamic Menu question

PostPosted: Fri Sep 03, 2010 8:46 am
by shannah
If BodyPartID doesn't exist in the Exercise table, why are you including it in the SQL query. The SQL query needs to be valid.

Re: Another Dynamic Menu question

PostPosted: Fri Sep 03, 2010 9:05 am
by cantlep
Well, indeed. Since then, I've added that field. I've tied up the IDs with those from the BodyPart table...still no joy, the "slave" menu still outputs all exercises, not just those tied with the "Master" BodyPart that is selected.

I've done that manually but should a relationship be used? It's not mentioned in the wiki....

Cheers

Re: Another Dynamic Menu question

PostPosted: Sun Sep 05, 2010 3:45 pm
by cantlep
It would appear that the reason it's not working is due to the fact that, One I make the select in the Master menu, I then have to click "next" to jump to the next tab, this seems then to make the next page unaware of the original selection :-(

Any ideas how I can make the submenus change even after I navitgate to the next page?

Thanks