Updated 030 How to filter a list field based on association with another field (markdown)
@ -51,10 +51,16 @@ There is another way to do this in Javascript. There's this function, the 'admin
|
||||
|
||||
### PHP - Ajax Method - Model - Controller
|
||||
|
||||
Now let's go look at the PHP. Should know by now you do not need 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. If you compile the component [00:15:13](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h15m13s) you can unzip it and go to the model, and to the controller, either in the back end or in the front end. Where as for now it will be in the back end, it's under models. You look there's an Ajax model and all of the back end Ajax methods gets added to this one model. And 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 basically it is saying the values name is country, the task is getRegion, if no values passed it's Null. It must be an integer that is past, and the method name is getRegion. And it must be a login user. That's a simple as that. 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 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 gets 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 is 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.
|
||||
|
||||
Then here(php ajax method) [00:16:08](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m08s) we have the method called getRegion, it gets the id. It doesn't need to sanitize it again since the controller already did that. You can go look at the controller to see the implementation. So we simply getting the factory, the data base factories passes us the database, we set up a query, we say that we are looking for the ids in the region that are published. [00:16:38](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h16m38s) Now this is again where you need to make your changes. We want the names 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 than this country. We just add the interger there again. Set the query, execute it, check if you have a value. And if we do we 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. There we go done. Finished. And then we return false, if no value was found. That's all the PHP you need. And that returns the PHP to our JavaScript. And the JavaScript does the rest. [00:17:57](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h17m57s) I hope this will be very helpful, and help you to sort your dropdowns. I didn't spend much time on explaining that maybe I should. Let me give a quick run over of that.
|
||||
[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 data base factories passes us the database. A query need 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 interger 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.
|
||||
|
||||
### Filtering a Dropdown
|
||||
|
||||
The field that we are focusing on is this country in the region field and they are both custom. I've already done tutorials on how to set up a custom field. Just for a reminder, let's open region. In region we simply saying the button is true. [00:18:34](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h18m34s) We select the region, you can update the view name dynamically in the description. In here(XML field definition) we doing some PHP. You could put all of this PHP in type_PHP_1 without adding it to the 19th placement. I've actually used every area, [00:19:05](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m05s) I like making the code look as it was written and not computer-generated. Then I'm adding the options, and so this is just a simple basic custom field. There isn't even any JavaScript in here. I've done everything in back end admin view. Did the PHP as well as the Javascript. I think that is because I want to use this field maybe in other views with different [00:19:44](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m44s) association. 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. If you are targeting a table of another component which you're not having the control over [00:20:15](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m15s) the actual field names, it will work the same way as long as there is a field of association within the table that you'd like to filter you could write that PHP. Find the values and filter it.
|
||||
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 like making the code look as it was written and not computer-generated. Then I'm adding the options, and so this is just a simple basic custom field. There isn't even any JavaScript in here. I've done everything in back end admin view. Did the PHP as well as the Javascript. I think that is because I want to use this field maybe in other views with different [00:19:44](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h19m44s) association. 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. If you are targeting a table of another component which you're not having the control over [00:20:15](https://www.youtube.com/watch?v=Z8FLifQOjUk&list=PLQRGFI8XZ_wtGvPQZWBfDzzlERLQgpMRE&t=00h20m15s) the actual field names, it will work the same way as long as there is a field of association within the table that you'd like to filter you could write that PHP. Find the values and filter it.
|
||||
|
Reference in New Issue
Block a user