How to create an Ajax call to filter data for Joomla Admin Component Topic is solved

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 31
Joined: Wed Mar 08, 2006 7:20 am

How to create an Ajax call to filter data for Joomla Admin Component

Post by nkamp » Mon Apr 27, 2020 2:07 pm

In my custom field/edit.php I listed the employees and have add some filter fields:
- for frontname,
- surname
- and category.
When I start typing in one of the filter fields, the Ajax call is executed.

Step 1: Custom field with the jQuery.ajax code:

Code: Select all

var token = jQuery("#token").attr("name");
var getUrl = "index.php?option=com_component&task=employeesearch&format=json";
jQuery.ajax({
	type: "POST",
	url: getUrl,
	data: {[token]: "1", task: "employeesearch", format: "json", arfilter: arfilter},
    success: function(result, status, xhr) { displaySearchResults(result); },
    error: function() { console.log(\'ajax call failed\'); },
});
(The array arFilter does have the value's of the filter fields.)

Step 2: Main controller:
I have in the main controller the function employeesearch added. The 'default' function display does also exist in this main controller. The task is 'employeesearch'.

Code: Select all

public function employeesearch()  {
    echo '<script>alert("DEBUG: main controller.php employeesearch function")</script>';
    if (!JSession::checkToken('get')) {
        echo new JResponseJson(null, JText::_('JINVALID_TOKEN'), true);
    } else  {
        parent::display();
    }
}
After the Ajax call is executed, not one ( 'default' display, employeesearch) of the functions is exected in the main controller.

Step 3: Admin/com_comoponent/views/employee/view.json.php
I do not have code in here, first I want to find out that this is executed. I think, if read the documentation on the right way, should be executed because the format is json.

Code: Select all

class ComponentViewEmployee extends JViewLegacy {
    function display($tpl = null) {
        //$input = JFactory::getApplication()->input;
        
        echo "view.json.php - display function";
    }
Step4 : Admin/com_comoponent/models/employee.php

For now I have add here the function getEmployeeSearchResults

Code: Select all

public function getEmployeeSearchResults($arEmployee) {
    try {
         echo "model getemployeesearchResults($arEmployee) {
         result = "Query executed";
    }
        catch (Exception $e) {
            $msg = $e->getMessage();
            JFactory::getApplication()->enqueueMessage($msg, 'error'); 
            //$results = null;
            $result = null;
    }
    return $result;
}
In the console log I get everytime "Ajax call failed" which is the error message of the Ajax call.

I tried to analyze it with the "browser developer tools", there I see "http://jcbworkshop2/administrator/index ... ormat=json". In the XHR the message 404.

I'm following the https://docs.joomla.org/J3.x:Developing ... dding_AJAX. This is about the frontend but I should think it is more or less the same, but maybe NOT?

The examples I found with googling are all based on the front-end view.

On this moment I don't know how I can make the following step????? How can I find out where it does go wrong?

I hope that someone can help me further and point me in direction, so that I can get further.

Thanks in advance

Nico
Last edited by toivo on Mon Apr 27, 2020 3:12 pm, edited 1 time in total.
Reason: mod note: moved from 3.x Extensions

 
nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 31
Joined: Wed Mar 08, 2006 7:20 am

Re: How to create an Ajax call to filter data for Joomla Admin Component

Post by nkamp » Wed May 20, 2020 8:39 pm

I have solved it and I was further than I had realize.

The main thing was I had add echo statements in the functions of the main controller, view.json.php and in de model function. Every time I did an Ajax call, there was one line in the console log with an url and one line with the message "Ajax call failed". Further nothing. My echo statements were not printed.

After a while later, I'm only working in the evening on it, I thought let's put the url in my browser. There I saw my echo statements printed! I take my echo statements out of the code and made maybe a few other little changes - but than it was working!

I don't know why the echo statements are not shown? And why these echo statements give some trouble for the Ajax call, so that it fails. But anyway I have it working now.

 

Post Reply

Return to “Joomla! 3.x Coding”