3

I'm making a module where the user can select some manufacturers names from the backend and the names will be posted in the index with some nice css.

I want to ask if I can add some jquery in the backend page, to make it easier for user. I want to make a text box where will count how many manufacturers the user selected ( max 30 ) and a list with every manufacturer currently selected. I will post an image to make it even more clear:

enter image description here

1 Answer 1

4

You will need to create a custom form field for this.

So firstly, change the type of the field in your XML like so:

<field name="brands" type="brands" label="Foreign Brands" />

Then in your XML, replace your <fieldset> with:

<fieldset addfieldpath="/modules/mod_mymodule/fields">

Then, create a new PHP file called brands.php in:

modules/mod_mymodule/fields

defined('JPATH_BASE') or die;

jimport('joomla.form.formfield');

class JFormFieldBrands extends JFormField
{
    protected $type = 'brands';

    public function getInput()
    {
       $doc = JDocument::getDocument();
       $js = '
              jQuery(document).ready(function($){
                  // Your JS code
              });
       '; 
       $doc->addScriptDeclaration($js);

       $options = array(
           'A.Dominguez' => 'A.Dominguez',
           'A.Feretti'   => 'A.Feretti',
           // Add more here
       );

       $select = JHtml::_('select.genericlist', $options, $this->name, null, 'value', 'text', $this->value, $this->id);

       return $select;
    }
}
14
  • in the above custom form field you entered the code to post the dropdown list also ?? or it is only for the 2 jquery elements ?? also really nice answer gonna test it in 5 mins!
    – IseNgaRt
    Dec 3, 2014 at 8:34
  • Yes, this code will also render the dropdown list. You simply need to add all your values to the $options array. I have added a comment // Add more here where you need to do this and also a comment where you can add your jQuery code ;)
    – Lodder
    Dec 3, 2014 at 8:36
  • I allready created the custom form field code for the dropdown list, so the jquery code HAVE to be in the same file right ? or i can create a new one with only jquery code? could it work then ?
    – IseNgaRt
    Dec 3, 2014 at 8:39
  • Yes you can do that too. Make sure you import the js file properly using Joomla coding standards. I on my way to work now but can show you when I get there
    – Lodder
    Dec 3, 2014 at 8:42
  • 1
    JHtml::_('script', JUri::root() . 'modules/mod_mymodule/path/file.js');
    – Lodder
    Dec 3, 2014 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.