Dynamic select(again)

A place for users and developers of the Xataface to discuss and receive support.

Dynamic select(again)

Postby Don » Sun Jan 30, 2011 9:52 am

I've been searching the forum and I've found quite a bit of information on this but so far the things I've tried aren't quite working the way I want.

Like many others I want 1 select list dependent on another select list. In other words when someone selects something in a select list the other select list is populated based on that selection.

I've seen several threads on this subject and I've tried to make it work but I get all values possible in the second select instead of just the ones that should be there based on the first selection.

I did see this post by Steve and I was wondering if it has been implemented yet since it was version 0.7 that this post was made.
http://xataface.com/forum/viewtopic.php?p=20317#p20317

I think what's stumping me is the example code has "slave" and "master" sprinkled through and I believe I need to substitute my actual names but I don't know where. I'm specifically looking at this example.
http://xataface.com/wiki/index.php?-act ... -mode=list

Can anyone help me understand what has to be replaced with my fields there?

Thanks
Don

Edit: Here's what my 3 files currently look like.
fields.ini
Code: Select all
[mwa_number]
   widget:label= "MWA Number"
   widget:type = select
   vocabulary = mwa_number
   
[part_number]
    widget:label="Part Number"
   widget:type = select
   filter=on
   vocabulary = part_number

valuelist.ini
Code: Select all
[part_number]
__sql__ = "SELECT part_number, part_number, mwa_number FROM mwa_parts"

[mwa_number]
__sql__ = "SELECT mwa_number, mwa_number FROM mwa"

transactions.php
Code: Select all
<?php

class tables_transactions {

function block__after_new_record_form(){
echo <<<END
<script language="javascript"><!--
var slave_field= document.getElementById('slave');
var master_field = document.getElementById('master');
END;
// Let's get all the slaves available.
$app =& Dataface_Application::getInstance();
$query =& $app->getQuery();
$table =& Dataface_Table::loadTable($query['-table']);
$slaves = $table->getValuelist('slaves_list');
$slave_masters = $table->getValuelist('slaves_list__meta');
// Note that the slaves_list__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 'slaves_list__meta'

// $slaves is an array with keys slave_id and values slave_name
// slave_masters is an array with keys slave_id and values master_id

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 ( slave_id in slaves_options){
if ( selected_master == slaves_master[slave_id] ){
slave_field.options[index++] = new Option(slaves_options[slave_id], slave_id);
}
}
};
//--></script>
END;
}

// Also place this javascript after an edit record form...
function block__after_edit_record_form(){
return $this->block__after_new_record_form();
}
}
?>
Don
 
Posts: 2
Joined: Mon Feb 08, 2010 8:25 am

Re: Dynamic select(again)

Postby shannah » Mon Jan 31, 2011 11:19 am

Code: Select all
var slave_field= document.getElementById('slave');
var master_field = document.getElementById('master');

In this snippet. "slave" is the name of the slave field, and "master" is the name of the master field. These would appear to correspond to your part_number and mwa_number fields respectively.

Code: Select all
$slaves = $table->getValuelist('slaves_list');
$slave_masters = $table->getValuelist('slaves_list__meta');


In this snippet "slaves_list" is the name of the valuelist that is used as a vocabulary for the slave field. In this case this would appear to correspond to your "part_number" valuelist. (And slaves_list__meta would correspond with "part_number__meta")

I think the rest of this code is generic.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Dynamic select(again)

Postby Don » Mon Jan 31, 2011 12:22 pm

Thank you Steve! That was what I needed.

I had tried everything except putting part_number in place of slave_list.

I love Xataface btw. Great tool to talk to a database with minimal work.
Don
 
Posts: 2
Joined: Mon Feb 08, 2010 8:25 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 10 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved