How to populate select field with data from another table?
How to populate select field with data from another table?
I have been reading https://datatables.net/forums/discussion/comment/137978/#Comment_137978 and https://datatables.net/forums/discussion/53705 but I'm just not getting it. Does anyone have a more involved example? Is there another way to achieve this such as using ajax?
I have a dept_code (INT) and a department name (VARCHAR) in a table. I would like to populate a select field in editor to populate only certain departments based on dept_code. The Datatable data itself is in a different table.
My editor code looks like this:
$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
ajax: 'ajax/csirs-departments_test.php',
...
{
label: 'Department:',
name: 'csirs.department',
type: 'select'
}
...
}
]
} );
$.getJSON( 'ajax/csirs_departments_test.php', function( json ) {
editor.field( 'csirs.department' ).update( json.options );
} );
$('#csirs').DataTable( {
...
I can see in the developer tools that I am getting just the departments I chose by dept_code but the select shows all departments and not just the one I chose. I can't figure out why.
This question has an accepted answers - jump to answer
Answers
Some more information, I have a lk_departments table that has local, county, state and federal departments defined and each one has a unique dept_code. For a particular Editor form I only want to show the state departments which is a list of about 230 departments. The whole list totals to around 1,020 and I want to cut that down to only the departments for the state.
I'm pretty sure I can achieve what I need by using the options in my php script.
However the departments are returning null. I've checked the fields in the lk_departments table and dept_code exists and is an INT. So once again I'm stuck, Arggggg!
The sql query looks like this in the developer tools;
Why is dept_code being set to NULL? I have php error reporting turned on and ->debug(true) and I see no errors.
Can you give me a link to the page so I can investigate please?
Also, if you are Ajax loading data for your DataTable using (
ajax
) then your$.getJSON
in the first block shown above won't be needed (but the code isn't complete so I can't say for sure).Allan
Yes the first block is using getJSON. While I did some reading trying to get more information on getJSON I found some code that uses the ->where condition and that looks like that would work for what I need to do.
I am replacing the first block of code that uses getJSON with the ->options in my PHP script as in the second block. I only need the states and this looks to be a much simpler way to achieve that for both Editor and the Datatable.
->where
will work just fine from the table's ownajax
option.Assuming that the DataTable also calls
ajax/csirs-departments_test.php
using itsajax
option, what does it return please?Allan
It calls on csirs-con.db and has this code and the Field is csirs.department...
The returned data that I see in the developer tools is;
Are you able to log in?
Hi,
Many thanks for the PM with the login details - sorry for the delay getting back to you - I completely missed the PM!
So taking the
csirs.department
field, you have->where( 'dept_code', 16, '=' )
in the PHP. And the return from the server for that field iscsirs.department: []
- which suggests to me that there are no items in the database which havedept_code = 16
on that table.Is that not what you are expecting here?
Allan
No problem Allen. I was out of town a few days anyway. Sneaking in some work when the wife wasn't looking or was asleep
Yes I am not expecting null. There are 230 items/departments with a dept_code of 16 in the table lk_departments when I query the database from the command line.
Perhaps this would help along with the PHP script I'm using above declaring the tables and fields and joins...
Do I need a dept_code field in the csirs table? If so they are all going to be 16.
In the Options::inst(), is this calling on the lk_departments table for dept_code or is it expecting dept_code to be in the csirs table?
Looking at the SQL debug (since you have the
debug()
option enabled in the PHP it is showing that the following is being executed:That explains the 0 results!
I had to refer to the docs myself to understand why that wasn't working.
Options->where()
expects a function, not a condition (which theEditor->where()
does allow).The second example just below that link point shows how it can be done. In this case:
Allan
That did the trick Allen. Many thanks again. I was reading the docs for the Where condition. I would have never thought about the Join docs.