Assign a value to different field after pulldown Onchange event
Assign a value to different field after pulldown Onchange event

In a lightbox editor form, I am trying to update a field after a dropdown list selection is made. At this point I am just manually assigning a value of 100 to test if the Onchange event is suitable to task.Unfortunately the transaction.amount field never updates. When I run the code under the Chrome debugger I get an Undefined, Unknown field error. The alert in the code block below fires off just fine.
editor.field('transaction.Item').input().on('change', function (e, d) {
if ( d && d.editorSet )return;{
var selectedval = $(this).val();
alert("You selected " + selectedval);
editor.field( 'transaction.Amount' ).val( 100 );
}
});
I pulled this almost identical block of code from the forum. What am I missing here ?
$( editor.field( 'firstField' ).input() ).on( 'keyup', function (e, d) {
if ( ! d || ! d.editor ) {
editor.field( 'secondField' ).val( "Closed");
editor.field( 'updated_by' ).val( );
}
} );
This question has accepted answers - jump to:
Answers
Hi @AstecConsulting ,
It would be best to use
dependent()
for that - see this example. If you change "office" tobe "XXX" it will update the salary.Hope that helps,
Cheers,
Colin
Colin,
Thanks for your quick reply. I have spent way more time on this than I will ever admit. Per your suggestion, I went ahead and tried the dependent() feature which I agree is a better approach. I get a similar result in that the Edit form hangs a bit and opening up the Chrome debugger shows
2dataTables.editor.min.js:54 Uncaught Unknown field name - transaction.Amount
Either I am missing the correct javascript library reference or that field name , which is a result of a left-outer join, is not compatible . I looked at the libraries in the example you provided and only the autofill.js was missing. Also added that and no change.
Hi @AstecConsulting ,
Would you be able to link to your page so we could take a look? If not, could you post it here. The error wasn't missing source files, it's reporting that a field name couldn't be found,
Cheers,
Colin
Here is the server side
var editor;
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "ajax_dt_transactions.php",
table: "#dtTransactions",
editor.on( 'preSubmit', function ( e, o, action ) {
var amount = this.field( 'transaction.Amount' );
if ( ! amount.val() ) {
amount.error( 'Amount must be provided' );
}
if ( this.inError() ) {
return false;
}
} );
editor.dependent( 'transaction.TransactionID', function ( val ) {
return val === '' ?
{ hide: ['transaction.TransactionID'] } :
{ show: ['transaction.TransactionID'] } ;
} );
editor.dependent('transaction.CheckNo', function(val, data, callback) {
if (val === '222') {
editor.field('transaction.Amount').set(12345);
}
callback(true);
});
$('#dtTransactions').DataTable( {
} );
```
Do you have multiple Editor instances on your page? You have a global
var editor;
at the top of the JS there. We do that in our demos since we need a reference to the instance from our demo script to display extra data about the example, but its unlikely you want to do that locally. I'd suggest using:Allan
Yes that fixed it . Thanks so much for catching this. It was becoming an obsession. I am pretty new to datatables and a bit overwhelmed with all of the functionality. Coming from a dotnet background so its copy, paste, customize and debug which explains the multiple Editor instances.
As a follow, can you recommend a simple method to lookup a value in a database that corresponds to the selected value in a dropdown list ? Since this is happening at the client side, I need to pull the value down before the edit form is closed. In the dotnet world I will hide the value I am looking to assign as a field in the pulldown, then just reference it after the selection. Its a small table so I could also assign it to an array as the table data only changes annually.
Thanks again for your help.
Good to hear that helped
.
dependent()
is probably the best way of doing what you are looking for now. I wrote another reply earlier discussing it which I think is applicable here.Let me know how you get on with it.
Allan
Thanks Allan. Sorry it took me awhile to respond but my appendix decided it wanted to come out so I've been in a bit of a fog. On my feet again so I will be diving back into the forum and following the thread you suggested. Please mark these threads as closed.