Filter a drop down with parameter obtained with Ajax

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
kerelov
Veteran Member
Posts: 42
Joined: 2020-04-17 21:20
Location: Bulgaria

Filter a drop down with parameter obtained with Ajax

Post by kerelov » 2020-07-27 08:30

Hi Guys,
I can't manage to make a correct jQuery select2 script that can filter a drop down based on a user choice from previous drop down and an additional filter parameter obtained by ajax request from another table. Actually I just only need to use the second filter parameter (ID), obtained with Ajax but first the user must make a choice in the first drop down. I saw how to update text fields with Ajax and select2 from UDEMY but for the drop downs it looks more complex and I don't know how to do it.

kerelov
Veteran Member
Posts: 42
Joined: 2020-04-17 21:20
Location: Bulgaria

Re: Filter a drop down with parameter obtained with Ajax

Post by kerelov » 2020-07-30 12:01

Guys,
I have finally figure it out.

Code: Select all

$j('#debit_account-container').on('change', function(e){
	var account_id = e.added.id;
	//console.log(account_id);
	if(account_id == '<Нищо>'){
		$j('#nomenclature_debit').val('');
	}else{
		$j.ajax({
			url: 'hooks/ajax-nomenclature-type.php',
			data: { account_id: account_id },
			success: function(data){
				nomanclature_debit_reload(data);
			}
		});
	}
});
ajax-nomenclature-type.php:

Code: Select all

<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");
	
	/* grant access to all users who have acess to the accountplan table */
	$od_from = get_sql_from('accountplan');
	if(!$od_from){
		header('HTTP/1.0 401 Unauthorized');
		exit;
	}
	
	$account_id = intval($_REQUEST['account_id']);
	
	$nomenclature_type_id = sqlValue("SELECT nomenclature_type FROM accountplan WHERE id='{$account_id}'");


	echo $nomenclature_type_id;
?>
This comes with a parasite field that I had to make because AppGini generates different versions of the function nomanclature_debit_reload when the field nomanclature_debit is not defined as filtered by another field. Without it you cant call nomanclature_debit_reload(data) because the function does not accept a parameter. So I just made this lookup nomanclature_type_debit that filters the nomanclature_debit and hide it from DV and TV. Not the best solution but it works and now I don't need to choose from 3 consecutive drop downs but only two. This is all good cause otherwise in my case in the second one there is always just one option.
Attachments
credit_mo.png
screenshot
credit_mo.png (158.61 KiB) Viewed 2301 times

User avatar
a.gneady
Site Admin
Posts: 1281
Joined: 2012-09-27 14:46
Contact:

Re: Filter a drop down with parameter obtained with Ajax

Post by a.gneady » 2020-08-04 16:51

Thanks for sharing :)
:idea: AppGini plugins to add more power to your apps:
  • DataTalk is an innovative AppGini plugin based on ChatGPT that allows you to interact with your AppGini database using natural language questions, without writing any SQL. Check the demo video
  • Mass Update plugin: Update multiple records at once and improve your workflow efficiency.
  • Check our other plugins and get a generous discount of up to 30% when buying 2 or more plugins.

Post Reply