Can the dependent api be used to update inputs other than selects?
Can the dependent api be used to update inputs other than selects?
I have this scenario:
permit table (pa_perm):
id, contact, title, phone, email
contact table (pa_contacts):
id, contact, title, phone, email
I want to open the editor to show the permit info. Some of the fields present on the form will be a select input to choose the contact as well as text inputs for title, phone and email. When a contact is chosen in the select input, I want the title, phone and email inputs to get updated with the chosen contacts info.
Based on what Ive seen, I'm thinking the dependent api is the right tool here, but all of the examples I am seeing involve a select input updating another select input.
editor.dependent( 'continent', '/api/countries' );
include_once( $_SERVER['DOCUMENT_ROOT']."/php/DataTables.php" );
$countries = $db
->select( 'country', ['id as value', 'name as label'], ['continent' => $_REQUEST['values']['continent']] )
->fetchAll();
echo json_encode( [
'options' => [
'country' => $countries
]
] );
I'm currently trying the following:
editor.dependent('pa_perm.Contact', './api/paContactInfo.php');
require('../../../includes/inc_session.php');
// DataTables Editor PHP library
include( '../DataTables_New/Editor-2.0.5/lib/DataTables.php' );
$ContactInfo = $db
->select( 'pa_contacts', ['Title', 'Phone', 'Email'], ['Contact' => $_REQUEST['values']['pa_perm.Contact']])
->fetch();
echo json_encode( [
'data' => [
0 => [
'contacts' => $ContactInfo
]
]
] );
When using those, the api is firing and returning the following:
{
"data": [
{
"contacts": {
"Title": "Planner",
"Phone": "1234567899",
"Email": "blah.blah@blah.com"
}
}
]
}
...but the title, phone and email inputs are not being updated. Is it possible to do this? Can I get this json mapped to the right place in the editor after being returned from the api?
Thanks in advance.
This question has accepted answers - jump to:
Answers
Yes indeed it is. Use
values
rather thandata
as part of the response (see the Return options / JSON section in thedependent()
documentation).This assumes that your Editor fields are called
Title
,Phone
andEmail
(capitalisation is important). Basically the entry in thevalues
object needs to match thefields.name
for the field's value to be updated.Allan
OK. I had tried values before, I see the problem now.
This was part of my server script
My field names did not match because of the contacts join in the server script. I had to remove the join, thereby getting the field names to be right, from my script to get it working.
Can I assume now that with the contacts join now gone, the simple act of opening the editor and the select field populating and firing this dependent function will 'act as my join' in the sense that the existing title, phone and email data will be populated on editor open? That is what seems to be happening, I just want to make sure that is expected operation so that I can call that portion of my code good to go.
Thanks.
Yes, I would expect that to work.
That said, it is possible to get linked data to work with the
dependent()
as well. You would just need to have your JSON return look like this:Allan