Results 1 to 7 of 7

Thread: Adding JS code in Search view

  1. #1
    jlabuelo is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    426

    Default Adding JS code in Search view

    Hi all

    I have added two depenent fields to the Account module that are related (values in the child field are shown depending on the values picked in the parent field) using a JS code I have included in the custom\modules\Accounts\metadata\editviewdefs.php

    The fields and their relationship works fine and helps a lot as child field could have over 500 values.

    My next step would be moving this relationship to the SearchView, so when I have to search for an account, I would just need to select a value in the parent field, and only values related to it would be shown in the child field. (as I got in the edit view).

    I got the edit view to work following the suggestions on a post of this forum:

    http://www.sugarcrm.com/forums/showthread.php?t=31005&highlight=jlabuelo

    But now I dont know how to include this JS code I created and how to call it from the fields of the relationship.

    Any examples or ideas about how this could be got!?

    Thanks a lot

  2. #2
    blitz2190 is offline Senior Member
    Join Date
    Jul 2009
    Posts
    105

    Default Re: Adding JS code in Search view

    as far as adding the java script goes inside the templatemeta array add this in the custom searchdefs.php in the metadata folder

    PHP Code:
    'templateMeta' => 
      array (
        
    'maxColumns' => '3',
        
    'widths' => 
        array (
          
    'label' => '10',
          
    'field' => '30',
        ),
        
    'javascript' => '<script src="yourfile.js"></script>',
      ), 

  3. #3
    jlabuelo is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    426

    Default Re: Adding JS code in Search view

    Hi blitz2190

    Thanks a lot for your help with this. I have tried to add the line for the havascript as you showed to me in the searchdefs.php in custom\modules\Accounts\metadata. But when I try to reproduce the js code that works in the EditViewdefs.php I dont get any response, and I dont get either errors in the Firefox "Error Console"

    Could you please check if I have included it right?

    Thanks a lot

    PHP Code:
    <?php
    $searchdefs 
    ['Accounts'] = 
    array (
      
      
    'layout' => 
      array (
        
        
    'basic_search' => 
        array (
        
          
    'name' => 
          array (
            
    'name' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'billing_address_city' => 
          array (
            
    'name' => 'billing_address_city',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'phone_office' => 
          array (
            
    'type' => 'phone',
            
    'label' => 'LBL_PHONE_OFFICE',
            
    'width' => '10%',
            
    'default' => true,
            
    'name' => 'phone_office',
          ),
          
    'tipo_centro_c' => 
          array (
            
    'type' => 'enum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_TIPO_CENTRO',
            
    'width' => '10%',
            
    'name' => 'tipo_centro_c',
          ),
          
    'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ),
        ),
        
    'advanced_search' => 
        array (
          
    'name' => 
          array (
            
    'name' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_street' => 
          array (
            
    'name' => 'address_street',
            
    'label' => 'LBL_ANY_ADDRESS',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'phone' => 
          array (
            
    'name' => 'phone',
            
    'label' => 'LBL_ANY_PHONE',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'website' => 
          array (
            
    'name' => 'website',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_city' => 
          array (
            
    'name' => 'address_city',
            
    'label' => 'LBL_CITY',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'email' => 
          array (
            
    'name' => 'email',
            
    'label' => 'LBL_ANY_EMAIL',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'annual_revenue' => 
          array (
            
    'name' => 'annual_revenue',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_state' => 
          array (
            
    'name' => 'address_state',
            
    'label' => 'LBL_STATE',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'employees' => 
          array (
            
    'name' => 'employees',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_postalcode' => 
          array (
            
    'name' => 'address_postalcode',
            
    'label' => 'LBL_POSTAL_CODE',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'billing_address_country' => 
          array (
            
    'name' => 'billing_address_country',
            
    'label' => 'LBL_COUNTRY',
            
    'type' => 'name',
            
    'options' => 'countries_dom',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'tipo_centro_c' => 
          array (
            
    'type' => 'enum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_TIPO_CENTRO',
            
    'width' => '10%',
            
    'name' => 'tipo_centro_c',
          ),
          
    'rating' => 
          array (
            
    'name' => 'rating',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ),
        ),
      ),
      
    'templateMeta' => 
      array (
        
    'maxColumns' => '3',
        
    'widths' => 
        array (
          
    'label' => '10',
          
    'field' => '30',
        ),
        
    'javascript' => '<script src="modules/Accounts/Account2.js"></script>'
      
    ),
    );
    ?>

  4. #4
    agcopley is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Santiago, Chile
    Posts
    204

    Default Re: Adding JS code in Search view

    Quote Originally Posted by jlabuelo View Post
    Hi blitz2190

    Thanks a lot for your help with this. I have tried to add the line for the havascript as you showed to me in the searchdefs.php in custom\modules\Accounts\metadata. But when I try to reproduce the js code that works in the EditViewdefs.php I dont get any response, and I dont get either errors in the Firefox "Error Console"

    Could you please check if I have included it right?

    Thanks a lot

    PHP Code:
    <?php
    $searchdefs 
    ['Accounts'] = 
    array (
      
      
    'layout' => 
      array (
        
        
    'basic_search' => 
        array (
        
          
    'name' => 
          array (
            
    'name' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'billing_address_city' => 
          array (
            
    'name' => 'billing_address_city',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'phone_office' => 
          array (
            
    'type' => 'phone',
            
    'label' => 'LBL_PHONE_OFFICE',
            
    'width' => '10%',
            
    'default' => true,
            
    'name' => 'phone_office',
          ),
          
    'tipo_centro_c' => 
          array (
            
    'type' => 'enum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_TIPO_CENTRO',
            
    'width' => '10%',
            
    'name' => 'tipo_centro_c',
          ),
          
    'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ),
        ),
        
    'advanced_search' => 
        array (
          
    'name' => 
          array (
            
    'name' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_street' => 
          array (
            
    'name' => 'address_street',
            
    'label' => 'LBL_ANY_ADDRESS',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'phone' => 
          array (
            
    'name' => 'phone',
            
    'label' => 'LBL_ANY_PHONE',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'website' => 
          array (
            
    'name' => 'website',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_city' => 
          array (
            
    'name' => 'address_city',
            
    'label' => 'LBL_CITY',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'email' => 
          array (
            
    'name' => 'email',
            
    'label' => 'LBL_ANY_EMAIL',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'annual_revenue' => 
          array (
            
    'name' => 'annual_revenue',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_state' => 
          array (
            
    'name' => 'address_state',
            
    'label' => 'LBL_STATE',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'employees' => 
          array (
            
    'name' => 'employees',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'address_postalcode' => 
          array (
            
    'name' => 'address_postalcode',
            
    'label' => 'LBL_POSTAL_CODE',
            
    'type' => 'name',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'billing_address_country' => 
          array (
            
    'name' => 'billing_address_country',
            
    'label' => 'LBL_COUNTRY',
            
    'type' => 'name',
            
    'options' => 'countries_dom',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'tipo_centro_c' => 
          array (
            
    'type' => 'enum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_TIPO_CENTRO',
            
    'width' => '10%',
            
    'name' => 'tipo_centro_c',
          ),
          
    'rating' => 
          array (
            
    'name' => 'rating',
            
    'default' => true,
            
    'width' => '10%',
          ),
          
    'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ),
        ),
      ),
      
    'templateMeta' => 
      array (
        
    'maxColumns' => '3',
        
    'widths' => 
        array (
          
    'label' => '10',
          
    'field' => '30',
        ),
        
    'javascript' => '<script src="modules/Accounts/Account2.js"></script>'
      
    ),
    );
    ?>
    Hi,
    You have to remember that all the form and field names are different in search and therefore you cannot use the same js script.
    For example, the form is now 'searchform' not 'EditView' and the field names are suffixed with '_basic' or '_advanced' depending on which search tab they appear. Check the 'view source code' in Firefox to confirm.

    Rgds
    Andrew

  5. #5
    jlabuelo is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    426

    Default Re: Adding JS code in Search view

    Hi Andrew

    Thanks a lot for your suggestions.

    I have followed your instructions and now I have three JS codes in the moldules/Account folder

    Account.js which works for fields provincia_c[] and municipio_c[] and which is used in EditViewDef working fine.

    Now to use in the Basic Search Form and Advanced Search Form I have created :

    Account_Search.js which is the same code than Account.js but with the fields renamed to provincia_c_basic[] and municipio_c_basic[]

    Account_SearchAdvanced.js which is also a copy of Account.js but with fields renamed to provincia_c_advanced[] and municipio_c_advanced[].

    I have added the two js codes in the custom\modules\Accounts\metadata\searchdefs.php as you can see here

    PHP Code:
     'templateMeta' => 
      array (
        
    'maxColumns' => '3',
        
    'widths' => 
        array (
          
    'label' => '10',
          
    'field' => '30',
        ),
        
    'javascript' => '<script type="text/javascript" src="modules/Accounts/Account_Search.js"></script>',
        
    'javascript' => '<script type="text/javascript" src="modules/Accounts/Account_SearchAdvanced.js"></script>',
      ), 
    However when I lauch sugar, and I review the source code using firefox of the Accounts view, I dont find the JS code loaded. Also I have added some debug comments in the codes and dont get them in the Error Console which makes me think that JS code is not being loaded in the SearchForm. What do you think?

    Also just for me to confirm, in the searchdefs.php fields are still called provincia_c[] and municipio_c[] in both advanced search and basic search, and the way I call the js in these fields is

    Basic Search
    PHP Code:
    'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ), 
    Advanced Search
    PHP Code:
      'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData_Advanced();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ), 
    Last question is regarding what you said about we are now in the 'searchform' and not in 'EditView'. I dont make reference to this forms in the code, is there something else I should chage or this was just to point me that fields are called differently in both forms?

    Thanks a lot once more for your help!!.

  6. #6
    agcopley is offline Sugar Community Member
    Join Date
    Nov 2007
    Location
    Santiago, Chile
    Posts
    204

    Default Re: Adding JS code in Search view

    Quote Originally Posted by jlabuelo View Post
    Hi Andrew

    Thanks a lot for your suggestions.

    I have followed your instructions and now I have three JS codes in the moldules/Account folder

    Account.js which works for fields provincia_c[] and municipio_c[] and which is used in EditViewDef working fine.

    Now to use in the Basic Search Form and Advanced Search Form I have created :

    Account_Search.js which is the same code than Account.js but with the fields renamed to provincia_c_basic[] and municipio_c_basic[]

    Account_SearchAdvanced.js which is also a copy of Account.js but with fields renamed to provincia_c_advanced[] and municipio_c_advanced[].

    I have added the two js codes in the custom\modules\Accounts\metadata\searchdefs.php as you can see here

    PHP Code:
     'templateMeta' => 
      array (
        
    'maxColumns' => '3',
        
    'widths' => 
        array (
          
    'label' => '10',
          
    'field' => '30',
        ),
        
    'javascript' => '<script type="text/javascript" src="modules/Accounts/Account_Search.js"></script>',
        
    'javascript' => '<script type="text/javascript" src="modules/Accounts/Account_SearchAdvanced.js"></script>',
      ), 
    However when I lauch sugar, and I review the source code using firefox of the Accounts view, I dont find the JS code loaded. Also I have added some debug comments in the codes and dont get them in the Error Console which makes me think that JS code is not being loaded in the SearchForm. What do you think?

    Also just for me to confirm, in the searchdefs.php fields are still called provincia_c[] and municipio_c[] in both advanced search and basic search, and the way I call the js in these fields is

    Basic Search
    PHP Code:
    'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ), 
    Advanced Search
    PHP Code:
      'provincia_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_PROVINCIA',
            
    'width' => '10%',
            
    'name' => 'provincia_c',
            
    'displayParams' => array ('javascript' => 'onchange="initData_Advanced();"'),
          ),
          
    'municipio_c' => 
          array (
            
    'type' => 'multienum',
            
    'default' => true,
            
    'studio' => 'visible',
            
    'label' => 'LBL_MUNICIPIO',
            
    'width' => '10%',
            
    'name' => 'municipio_c',
          ), 
    Last question is regarding what you said about we are now in the 'searchform' and not in 'EditView'. I dont make reference to this forms in the code, is there something else I should chage or this was just to point me that fields are called differently in both forms?

    Thanks a lot once more for your help!!.
    1. You cant add the javascript here..you need to include it in the listview..thats where the searchforms appear. Use a view.list.php in your custom directory to include the javascript.

    2. The javascript itself references the DOM and the forms. You obviously need to ensure that the forms you reference are actually in the HTML. Check your HTML source with your js.

    Rgds
    Andrew

  7. #7
    jlabuelo is offline Sugar Community Member
    Join Date
    Apr 2008
    Posts
    426

    Default Re: Adding JS code in Search view

    Thanks a lot for the answer, just a quick question. I dont find the view.list.php for the Accounts module neither in the Accounts module nor in the Custom Accounts folder (neither in all Custom folder and subfolder).

    I have run a search to find the view.list.php file for the Accounts module but seems does not exist. I have found it for other modules like Campaigns, Calendar, Activities... but not for accounts.

    Could you point me where the file is? and if I have to create a new one, how should I do it?

    Thanks a lot again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding field of type link through code
    By kavita in forum Developer Help
    Replies: 1
    Last Post: 2009-08-05, 04:03 PM
  2. adding subpanel through code
    By kavita in forum Developer Help
    Replies: 0
    Last Post: 2009-04-08, 11:41 AM
  3. adding subpanel through code
    By kavita in forum Help
    Replies: 0
    Last Post: 2009-04-08, 11:16 AM
  4. Replies: 2
    Last Post: 2007-08-14, 08:41 PM
  5. Replies: 0
    Last Post: 2005-05-10, 12:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •