Updated 030 How to filter a list field based on association with another field (markdown)

Amigo 2019-08-22 16:20:55 +02:00
parent e6dcf1f7af
commit e8744753a2

@ -6,68 +6,66 @@
[00:00:00](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m00s)
(_Click on these time links to see Youtube video_)
Perhaps you encountered the necessity to limit the dropdowns of a list field based on its association with another table. A very common place where this occurs is for example when the regions of only one country need to be loaded. Although there are many ways to accomplish this, this is how I have done it.
Perhaps you encountered the necessity to limit the dropdowns of a list field based on its association with another table. A common place where this occurs is, for example, when the regions of only one country have to be loaded. Although there are many ways to accomplish this, this is how I have done it.
* ### Example Component With Filters
[00:00:43](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h00m43s)
For example; Here is a component called 'Job Tracking'. In this component is a client area. If you go to Admin Views, it may be seen that there is 'Country' and 'Regions'. The field concepts can be changed and replaced with other tables. (See video) [00:01:16](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m16s) Here follows an explanation of how it is implemented in the client's view because that's where most of the work is done. JavaScript, as well as the PHP area, are two areas that should be looked at. [00:01:51](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m51s)In various previous tutorials some of the Ajax functionality in Component Builder had been explained.
For example; Here is a component called 'Job Tracking'. In this component is a client area. If you go to Admin Views, there is 'Country' and 'Regions'. The field concepts can be changed and replaced with other tables. (See video.) [00:01:16](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m16s) Here follows an explanation of how it is implemented in the client's view because that's where most of the work is done. JavaScript, as well as the PHP area, are two areas that should be looked at. [00:01:51](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h01m51s) In previous tutorials some of the Ajax functionality in Component Builder had been explained.
For the sake of those who have not watched all the tutorials, here is a brief explanation. But first, it should be determined what is our objective. [00:02:19](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m19s) When starting with a client a country needs to be selected. Let it load only that country's regions. [00:02:41](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m41s) Furthermore you want to make sure that when you unselect the Country, it also unselects the Region.
For the sake of those who have not watched all the tutorials, here is a brief explanation. First, it should be determined what our objective is. [00:02:19](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m19s) When starting with a client a country must be selected. Load only that country's regions. [00:02:41](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h02m41s) You furthermore want to make sure that when you unselect the Country, it also unselects the Region.
There are various things that need to be accomplished here. This will be illustrated in JavaScript. [00:03:10](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m10s) If the country "Australia" is used, for example, it will be seen that it does not load any regions, in fact, it shows: 'Create a region'. If Namibia is being selected, it indicates 'Select a region', and so there are two regions already created. There is this button(see video) in some of the dropdowns which are being added by selecting 'true' to the button. It's very helpful for either creating or editing existing regions. So if a country is selected where there are not any region set yet, it tells you to create one. [00:03:44](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m44s) Then a region can be created for this country, If 'Create' is selected, then your region may be created. Save and close. Call it 'Region' for example. Save and close. Go back to Creating a client. [00:04:17](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m17s) If Australia is selected it shows that it got 'Regions'.
There are various things that must be accomplished here. This will be illustrated in JavaScript. [00:03:10](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m10s) If, for example, the country "Australia" is used, notice that it does not load any regions. It shows 'Create a region'. If Namibia is selected, it indicates 'Select a region'. So two regions are already created. There is this button in some of the dropdowns which are being added by selecting 'true' to the button. It's helpful to either create or editing existing regions. If a country is selected where there are not any region set yet, it tells you to create one. [00:03:44](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h03m44s) If 'Create' is selected, your region may be created. Save and close. Call it 'Region', for example. Save and close. Go back to Creating a client. [00:04:17](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m17s) If Australia is selected it shows that it has 'Regions'.
To demonstrate how that was done:
To demonstrate how that was done:
### Two Variables - Var Regions(object) Var Region(store)
[00:04:30](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h04m30s)
Let's get started on a JavaScript side of things. There are two variables. Both may be called 'var'. One is an object and one is where a value is going to be stored, only if that value is already set. When the fields are Created, all the regions are brought to the page, no region should be left unloaded. [00:05:11](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m11s) Rather parse those regions into this variable, only then put those regions that are part of this country back into the list of drop-down options.[00:05:43](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m43s) It is been done for 'each(funtion()' on the regions options. (See video) and then I'm getting the value, then it is put in 'var'key, then get the 'text', and place it in 'var' text, and add it to regions'Key=text'. Simply as it is loaded in an array. [00:06:08](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m08s) Then take the value of the set region(see video) and add that to 'Region', and the function 'getregion' is set.
Let's get started on the JavaScript side of things. There are two variables. Both may be called 'var'. One is an object and one is where a value will be stored (only if that value is already set.) When the fields are Created, all the regions are brought to the page. No region should be left unloaded. [00:05:11](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m11s) Rather parse those regions into this variable, then place those regions that are part of this country back into the list of drop-down options. [00:05:43](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h05m43s) It is been done for 'each(function()' on the regions options. (See video.) Get the value; it is placed in 'var'key. Get the 'text', place it in 'var' text, then add it to regions'Key=text'. It is loaded in an array. [00:06:08](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m08s) Then take the value of the set region, add that to 'Region', and the function 'getregion' is set.
### Loading Show - Class ID
[00:06:23](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m23s)
First load the 'jQuery("#loading"),show);' Now this 'show' or rather the class ID, is loaded on the page by default when Fade in Affect is set on 'Add'. Then by default loads that class with a spinner. If the loading process takes long the user is incapable of doing anything, [00:06:58](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m58s) It is not necessary to add that. Then look for the options 'find('option')' for the region, and remove all of them. Then an update is triggered. This update that gets triggered here 'lizt', is a Joomla function. [00:07:19](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m19s) It is possibly going to change in the future. Keep in mind that if things break, it is possible happening right here, because this list field is dynamically generated by Joomla. Only after everything has been removed, it may be told to draw that list again.
First load the 'jQuery("#loading"),show);'. This 'show', or the class ID, is loaded on the page by default when Fade in Affect is set on 'Add'. By default, it loads that class with a spinner. If the loading process takes a long time the user is incapable of doing anything. [00:06:58](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h06m58s) It is not necessary to add that. Look for the options 'find('option')' for the region, and remove all of them. An update is then triggered. This update that is triggered here 'lizt', is a Joomla function. [00:07:19](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m19s) It might change in the future. Keep in mind that if things break, it might happen here, because this list field is dynamically generated by Joomla. Only after everything has been removed, it may be told to draw that list again.
### GetRegion
[00:07:51](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m51s) So up till here only the region had been touched. Now get the country value that has been selected and adds it to 'country'. Then call the server at this point. The only thing which is given to the server is the 'country' ID. Up here is a 'country' ID. We have our getUrl. In this case, 'getRegion' is the task. [00:08:16](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m16s) Then check if the token is on the page and make sure that a country ID is not zero. A request is loaded and an Ajax is sent.
[00:07:51](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h07m51s) Up till here only the region had been touched. Now get the country value that has been selected and add it to 'country'. Now call the server. The only thing given to the server is the 'country' ID. Up here is a 'country' ID. We have our getUrl. In this case, 'getRegion' is the task. [00:08:16](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m16s) Check if the token is on the page and make sure that a country ID is not zero. A request is loaded and an Ajax is sent.
It is done this way because some things need to be performed after we have got the result which is this 'done'. [00:08:40](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m40s) Then 'setRegion'. The result is placed into 'setRegion', hide the 'load', and then do some house cleaning on the 'button'. In this instance, the button got tampered with. There is a correction region button below. (See video) [00:09:05](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m05s) This code may be copied and tested. Update 'region' to whatever field you are trying to limit but only do that after 'setRegion'.
It is done this way because some things must be performed after we have the result which is this 'done'. [00:08:40](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h08m40s) Then 'setRegion'. The result is placed into 'setRegion'. Hide the 'load' and do some house cleaning on the 'button'. In this instance, the button had been tampered with. There is a correction region button below. (See video.) [00:09:05](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m05s) This code may be copied and tested. Update 'region' to whatever field you are trying to limit but only after 'setRegion'.
### SetRegion
SetRegion is quite simple, mainly because most of the work had already been done, with the result being returned. (It gets returned by PHP). [00:09:38](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m38s) Get an array in 'setRegion', and check if the array is there, and then 'append'. Simply as with any list selection, 'append' the value.
SetRegion is simple, mainly because most of the work had already been done, with the result being returned. (It gets returned by PHP). [00:09:38](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m38s) Get an array in 'setRegion', check if the array is there, then 'append'. As with any list selection, 'append' the value.
* ### Strings - Select_A_Region, Create_A_Region
[00:09:55](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m55s)The 'select_a_region' is a JavaScript variable that gets set through PHP, which will be discussed at a later stage. Basically, it's just a string that says that the region should be selected. [00:10:21](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m21s) If there is no array returned, another variable should be used, the 'create_a_region' which is also just a string.
[00:09:55](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h09m55s) The 'select_a_region' is a JavaScript variable that is set through PHP, which will be discussed at a later stage. It's just a string that says that the region should be selected. [00:10:21](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m21s) If there is no array returned, another variable should be used, the 'create_a_region' which is also just a string.
Go to 'jQuery.each(array,function(i,id){' , select 'array' and use the 'id', and check if the 'id' is in 'regions'. If it is in 'regions', append the 'id' as a value. As you remember in 'regions' the text, as well as the 'id', had been saved. [00:10:51](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m51s) So that is how to get the text, simply adding it in there(see video). It could have been done in PHP, but I felt let the browser help us with some of the lifting. There are regions, if it's a huge table it should be approached differently. Eventually, it is checked whether the 'id' and the 'region' equals(id==region). Since if it does it is necessary to ensure to set the 'region' value.
Go to 'jQuery.each(array,function(i,id){', select 'array', and use the 'id'. Check if the 'id' is in 'regions'. If it is in 'regions', append the 'id' as a value. In 'regions' the text, as well as the 'id', had been saved. [00:10:51](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h10m51s) That is how to get the text, adding it in there. (See video.) It could have been done in PHP, but let the browser help with some of the lifting. There are regions. If it's a huge table it should be approached differently. Eventually, it is checked whether the 'id' and the 'region' equals(id==region). If it does, it is necessary to ensure setting the 'region' value.
[00:11:25](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m25s) As you remember we found the region value and stored it into 'region'. Just in case the value was set before and saved and now the person is looking through different countries. But when he again select the country that previously was saved, we want to make sure that this previous region is also shown as the selected value. That's simply what we are doing here(jQuery(#jform_region)val(id)). Then after everything is done we again trigger this liszt:Updated. [00:11:58](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m58s) Which is the same as what was triggered there(see video). That sets the Regions List again afresh. Then do we run this(see video) bit of housekeeping which is done by the region button.
[00:11:25](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m25s) As you remember we found the region value and stored it into 'region'. Just in case the value was set before and saved, now the person looks through different countries. But when he selects the country that was previously saved, we want to make sure that this previous region is also shown as the selected value. That's what we are doing here. (jQuery(#jform_region)val(id)). After everything is done we trigger this liszt:Updated again. [00:11:58](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h11m58s) Which is the same as what was triggered there. (See video.) This sets the Regions List afresh. We run this bit of housekeeping which is done by the region button.
### RegionButton Correction
[00:12:22](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m22s)The region button is actually JavaScript that is added because of the List field. If a specific list field was set to have no button, then simply leave this out. If it was set to have a button, the name of the button would be this area before the 'jform'_. This part of the name('region') would be the first part of the name of the button function, and then the last part would be 'button'. [00:12:54](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m54s) So that's how it can execute and search for this 'regionButton' function on the page if there is any uncertainty. That's how to run the Button Correction. That is how the JavaScript looks that performs this. This area of JavaScript can actually include PHP.[00:13:25](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m25s) That area can not because that's just a pure JavaScript file where this is JavaScript added to a PHP file. Here may be added some PHP and that's where to get the 'select_region' and 'create_region'.
[00:12:22](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m22s) The region button is JavaScript that is added because of the List field. If a specific list field was set to have no button, then leave this out. If it was set to have a button, the name of the button would be this area before the 'jform'_. This part of the name ('region') would be the first part of the name of the button function, and the last part would be 'button'. [00:12:54](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h12m54s) That's how it can execute and search for this 'regionButton' function on the page if there is uncertainty. That's how to run the Button Correction. That is how the JavaScript performing this looks like. This area of JavaScript can include PHP.[00:13:25](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h13m25s) This area can not because it's a pure JavaScript file where this is JavaScript added to a PHP file. There may be added some PHP and that's where to get the 'select_region' and 'create_region'.
### JavaScript - AdminForm.on(change)
There is another way to do this in Javascript. There's this function, the 'adminForm.on(change)' of the country field and when that event occurs, we want to execute 'getRegion'. [00:14:06](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m06s) That is done every time that field is changed. That is the extra Script that needs to be added to these two if you are going to copy this. That will execute this function which is the same function that is executed on the page load. [00:14:33](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m33s) It is not necessary to execute this because that is already been done on the page load. That is all the JavaScript that needs to be added.
There is another way to do this in Javascript. This function, the 'adminForm.on(change)' of the country field and when that event occurs. We want to execute 'getRegion'. [00:14:06](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m06s) That is done every time the field is changed. That is the extra Script that must be added to these two if you are going to copy this. It will execute this function which is the same function executed on the page load. [00:14:33](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h14m33s) It is not necessary to execute this because it is already been done on the page load. It is all the JavaScript that should be added.
### PHP - Ajax Method - Model - Controller
It is not necessary to add the PHP to the same Admin view, since all the Ajax methods are all added to one file in the component called, Ajax. The model, as well as the controller, is the Ajax model and controller. [00:15:13](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m13s) If the component is compiled it can be unzipped. Then go to the model, and to the controller, either in the back end or in the front end. Whereas for now, it will be in the back end, it is under models. It may be seen that there is an Ajax model and all of the back end Ajax methods get added to this one model. So this 'php ajax method' area is what takes care of the model. [00:15:37](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m37s) And this 'Ajax input - add' area here takes care of the controller. So the value's name is 'country', the task is 'getRegion', if no values are passed it is Null. It must be an integer that is past, and the method name is 'getRegion' and it must be a login user. That takes care of the whole controller setup.
It is not necessary to add the PHP to the same Admin view since all the Ajax methods are added to one file in the component Ajax. The model, as well as the controller, is the Ajax model and controller. [00:15:13](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m13s) If the component is compiled it can be unzipped. Go to the model and the controller, in the back end or in the front end. For now, it will be in the back end under models. There is an Ajax model and all of the back end Ajax methods are added to this one model. This 'php ajax method' area is what takes care of the model. [00:15:37](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m37s) This 'Ajax input - add' area takes care of the controller. The value's name is 'country', the task is 'getRegion', if no values are passed it is Null. It must be an integer that is passed. The method name is 'getRegion' and it must be a login user. That takes care of the whole controller setup.
[00:16:08](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m08s) Then here is the method('php ajax method') called 'getRegion', it gets the ID. It doesn't need to sanitize it again since the controller already did that. At the controller may the implementation be seen. So we simply getting the factory, the database factories pass us the database. A query needs to be set up, that requests the ids in the region that are published. [00:16:38](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m38s) This is again where the changes need to be done. We want the IDs of the regions on their association with the country. Then in the region table, we have a field, a column, that is called 'country'. It has an 'id' in it. [00:17:06](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m06s)
So that each region is linked to a country. Whatever table you're targeting, this association would be this country. Add the integer again, set the query, execute it and check if you have a value. If there is a value simply return the 'loadcolumn' function, [00:17:33](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m33s) which returns the 'id' in an array. And then return 'false', if no value was found. That is all the PHP necessary. That returns the PHP to the JavaScript and the JavaScript does the rest. [00:17:57](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m57s) This will help to sort your dropdowns.
[00:16:08](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m08s) Here is the method('php ajax methogotd') called 'getRegion'. It gets the ID. It doesn't need to sanitize it again since the controller already did that. At the controller, the implementation may be seen. We get the factory and the database factories pass us the database. A query must be set up that requests the ids in the region that are published. [00:16:38](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m38s) This is where the changes must be done. We want the IDs of the regions on their association with the country. In the region table, we have a field and a column, called 'country'. It has an 'id' in it. [00:17:06](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m06s) So each region is linked to a country. Whatever table you're targeting, this association would be this country. Add the integer again, set the query, execute it, and check if you have a value. If there is a value return the 'loadcolumn' function, which returns the 'id' in an array. [00:17:33](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m33s) Then return 'false' if no value was found. That is all the PHP necessary. It returns the PHP to the JavaScript and the JavaScript does the rest. [00:17:57](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m57s) This will help to sort your dropdowns.
### Filtering a Dropdown
The fields that are being focused on is this Country and this Region Field which are both custom. Tutorials had been done on how to set up a Custom Field. Just for a reminder: Open Region. In Region the button is set on true ('button="true"'). [00:18:34](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m34s) The region is selected, the view name can be dynamically updated in the description. In here(XML field definition) some PHP is done. All of this PHP may be placed in 'type_PHP_1' without adding it to the 19th placement. Every area had been used. [00:19:05](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m05s) I prefer to let the code appear as it had been written and not computer-generated. Options are added, and so this is just a basic Custom field without any JavaScript in it. Everything had been done in back end Admin View, the PHP as well as the Javascript. [00:19:44](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m44s) I preferred to use this field may be in other views with different associations. So I don't tend to use the script area maybe as one might expect. So that's quickly looking at filtering a dropdown Custom list selection via JavaScript and Ajax.[00:20:15](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m15s) If you are targeting a table of another component which you do not have control over the actual field names, it will work the same way as long as there is a field of association within the table that you like to filter, you could write that PHP, find the values and filter it.
The fields being focused on this is Country and Region Field both of which are custom. Tutorials had been done on how to set up a Custom Field. As a reminder: Open Region. In Region the button is set on true ('button="true"'). [00:18:34](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m34s) The region is selected. The view name can be dynamically updated in the description. Here(XML field definition) some PHP is done. All of the PHP can be placed in 'type_PHP_1' without adding it to the 19th placement. Every area had been used. [00:19:05](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQHereHereZWBfDzzlERLQgpMRE&t=00h19m05s) I prefer to let the code appear as it had been written and not computer-generated. Options are added, and so this is a basic Custom field without any JavaScript in it. Everything had been done in the back end Admin View, the PHP as well as the Javascript. [00:19:44](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m44s) I prefer to use this field may be in other views with different associations. So I don't usually use the script area as one might expect.
So that's looking at filtering a dropdown Custom list selection via JavaScript and Ajax. [00:20:15](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m15s) If you are targeting a table of another component where you do not have control over the actual field names, it will work the same way as long as there is a field of association within the table that you like to filter, you could write that PHP, find the values and filter it.