Changing the select list options for Add New Record
Changing the select list options for Add New Record
Hi,
I have a situation where I have a left outer join to return the Name of the EquipmentID field to populate the datatable column.
However I wish to populate the select type option values differently for Adding a new record. (I have No Edit option allowed)
Here is my php code to populate the Editor:
<?php
ini_set("display_errors",1);
/*
* Editor server script for DB table Configuration_Equipment
* Created by http://editor.datatables.net/generator
*/
// DataTables PHP library and database connection
include( "lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'Configuration_Equipment', 'CEID' )
->fields(
Field::inst( 'Configuration_Equipment.RoomID' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'Configuration_Equipment.ConfigurationID' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'Configuration_Equipment.EquipmentID' )
->options( 'Equipment','EquipmentID', 'Name')
->validator( 'Validate::dbValues' ),
Field::inst( 'Equipment.Name' ),
Field::inst( 'Equipment.Available'),
Field::inst( 'Equipment.Notes')
)
->leftJoin( 'Equipment', 'Equipment.EquipmentID', '=', 'Configuration_Equipment.EquipmentID' )
->where( function ( $q ) {
$q
->where('Configuration_Equipment.RoomID', $_REQUEST['RoomID'], '=')
->and_where('Configuration_Equipment.ConfigurationID', $_REQUEST['ConfigurationID'], '=');
})
->process( $_POST )
->json();
The editor code above gives me the Name of the equipment to display in the dataTable, but not sure how to get the select values to change on Adding a new record.
The new select options should be based on the following SQL code:
SELECT EquipmentID, Name FROM Equipment WHERE EquipmentID NOT IN
(SELECT EquipmentID FROM Configuration_Equipment WHERE RoomID <> $_REQUEST['RoomID'])
OR EquipmentID IN
(SELECT EquipmentID FROM Configuration_Equipment WHERE RoomID = $_REQUEST['RoomID'] AND
ConfigurationID <> $_REQUEST['ConfigurationID'])
This question has accepted answers - jump to:
Answers
I would suggest removing the use of the
Field->options()
method (which is how Editor is getting the list of options), and instead querying the database to get your list of options directly.Then use
Editor->data()
rather thanEditor->json()
to have it put the information into a variable, attach it to theoptions
parameter and send it back as JSON - e.g.:Where of course
$myOptions
is your list of options.Allan
Hi Allan,
Sorry about delay in getting back on this.
I have made changes from what I interpret above as follows:
Now the data gets returned as :
But my drop down in the Editor has nothing in it, when I click on the New button.
Any suggestions to what I have done incorrectly.
Thanks
Silly me, the myOptions should be as follows:
$myOptions[] = array('label' => $row['Name'] , 'value' => $row['EquipmentID']);
All good now.
Thanks for the update - good to hear that it's all working now.
Allan