Advertisement
AmourSpirit

Joomla 3.4x select2 example with ajax

Aug 6th, 2015
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.48 KB | None | 0 0
  1. <?php
  2. // See: http://pastebin.com/fbqLXDPZ for formbehaviourselect2
  3. /**
  4.  * @version     $Id$
  5.  * @package     com_cn_reports
  6.  * @copyright   Copyright (C) 2015. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  * @author    
  9.  */
  10. // no direct access
  11. defined('_JEXEC') or die;
  12.  
  13. JHtml::_('behavior.keepalive');
  14. JHtml::_('behavior.tooltip');
  15. //JHtml::_('behavior.formvalidation'); pre joomla 3.4
  16. JHTML::_('behavior.formvalidator'); // jooomla 3.4
  17. //JHtml::_('formbehavior.chosen', 'select'); // defaault joomla form behaviour for select
  18.  
  19. // form behaviour select2 is a custom behaviour class that implements
  20. // select2 version 4. All necessary scripts are loaded from there.
  21. JHtml::_('formbehaviourselect2.select2', 'select'); // /libraries/cms/html/formbehaviorselect2.php
  22. JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
  23. //Load admin language file
  24. $lang = JFactory::getLanguage();
  25. $lang->load('com_cn_reports', JPATH_ADMINISTRATOR);
  26. $doc = JFactory::getDocument();
  27.  
  28. $doc->addScript(JUri::base() . '/components/com_cn_reports/assets/js/form.js');
  29.  
  30. $js_example_basic_single = 'jQuery(document).ready(function() {' . PHP_EOL;
  31. $js_example_basic_single .= 'jQuery(".js-example-basic-single").select2();' . PHP_EOL;
  32. $js_example_basic_single .= '});' . PHP_EOL;
  33.  
  34. // getFormatToken is needed as the server will use this as a security check to make sure data is being asked for
  35. // from a valid form call.
  36. $ajaxUrlUser = 'index.php?option=com_cn_reports&task=ajax.usersjson&format=json&tmpl=component&' . JSession::getFormToken() . '=1';
  37. $placeholder = JText::_('COM_CN_REPORTS_LBL_SELECT_OPTON');
  38.  
  39. // the style below is to format the dropdown of the customer after the ajax call picks up data.
  40. ?>
  41. <style>
  42. .boxer
  43. {
  44.    display: table;
  45.    border-collapse: collapse;
  46.    border-spacing: 0;
  47.    table-layout: fixed;
  48. }
  49.  
  50. .boxer .box-row {
  51.    display: table-row;
  52. }
  53.  
  54. .boxer .box {
  55.    display: table-cell;
  56.    text-align: left;
  57.    vertical-align: top;
  58. /*    border: 1px solid black; */
  59.    white-space: nowrap;
  60.    background: transparent;
  61. }
  62. <?php
  63. // set the background color to blue for rows currently being
  64. /// hovered over by the mouse in the dropdown list of ajax data.
  65. ?>
  66. .select2-results__option--highlighted > .boxer,
  67. .select2-results__option--highlighted > .box
  68. {
  69.     background: #5897fb;
  70. }
  71. .box1 {
  72.   width:25%;
  73. }
  74. .box2 {
  75.   width:25%;
  76. }
  77. .box3 {
  78.   width:50%
  79. }
  80. <?php
  81. // the following css is specific to customer drop down list.
  82. // it sets the padding to 0 giving a much nicer display in the ajax return data.
  83. ?>
  84. #select2-jform_customer_id-results > li,
  85. {
  86.     padding:0px;
  87. }
  88. </style>
  89. <script type="text/javascript">
  90. jQuery(document).ready(function(){
  91.     jQuery("#jform_customer_id").select2({
  92.         ajax: {
  93.             url: "<?php echo $ajaxUrlUser; ?>",
  94.             dataType: 'json',
  95.             delay: 250,
  96.             data: function (params) {
  97.                 return {
  98.                 q: params.term, // search term
  99.                 page: params.page
  100.             };
  101.         },
  102.         processResults: function (data, page) {
  103. <?php
  104.         // parse the results into the format expected by Select2.
  105.         // since we are using custom formatting functions we do not need to
  106.         // alter the remote JSON data
  107. ?>
  108.             return {
  109.             results: data
  110.             };
  111.         },
  112.         cache: true
  113.         },
  114.         placeholder: "<?php echo $placeholder; ?>",
  115.         escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
  116.         minimumInputLength: 1,
  117.         templateResult: formatUser,
  118.         templateSelection: formatUserSelection,
  119.         allowClear: true
  120.     });
  121.  
  122. <?php
  123.     // sets up the clear method when the select2 is cleared.
  124.     // The reason for this is that the default select2 will not empty the selects
  125.     // for the ajax call when clear is called. This resulted in the select to remain
  126.     // when existing record was being summited back to the server.
  127.     // For instance if the customer field had a value before the form was loaded and
  128.     // then you loaded the form, cleard the customer and then submitted the form then
  129.     // the result was joomla did not pick up any changes and even though you cleared
  130.     // the customer it still got submitted and pickup on the serer side.
  131. ?>
  132.     jQuery("#jform_customer_id").on("select2:unselect", function (e) {
  133.         var $select = jQuery('#jform_customer_id');
  134.         $select.empty();
  135.     });
  136. });
  137.  
  138. jQuery(document).ready(function(){
  139.     <?php
  140.     // add required to fields and dropdowns that are required
  141.     // these are dynamicly genereated and therefore we will add the class the them
  142.     // client side.
  143.     ?>
  144.     if (!jQuery( "#jform_override_type_id" ).is( ".required" )) {
  145.         jQuery('#jform_override_type_id').addClass('required');
  146.     }
  147.  
  148.     if (!jQuery( "#jform_retail_partner_id" ).is( ".required" )) {
  149.         jQuery('#jform_retail_partner_id').addClass('required');
  150.     }
  151.     <?php
  152.     // set the placeholder text for the exsiting drop downs
  153.     // Joomla does generate an empty option as the first element
  154.     // for new records. Select2 will use these to display select an option.
  155.     ?>
  156.     jQuery("#jform_override_type_id").select2({
  157.         placeholder: "<?php echo $placeholder; ?>",
  158.         allowClear: false
  159.     });
  160.     jQuery("#jform_retail_partner_id").select2({
  161.         placeholder: "<?php echo $placeholder; ?>",
  162.         allowClear: false
  163.     });
  164. });
  165. <?php
  166.     // the format user is used by the ajax call for the customer
  167.     // it uses css to format the display. Each found entry in the ajax call will
  168.     // call the formatUer function.
  169.     //
  170.     // The formatUserSelections function simply converts the user name field into user.text field
  171.     // this is because select2 4.0 is strict about having the format as id and name for the select element
  172. ?>
  173.     function formatUser (user) {
  174.         //console.log(user);
  175.         if (user.loading) return user.text;
  176.         var markup = '' +
  177.         '<div class="boxer">'                                       +
  178.             '<div class="box-row">'                                 +
  179.                 '<div class="box box1">' + user.name + '</div>'     +
  180.                 '<div class="box box2">' + user.username + '</div>' +
  181.                 '<div class="box box3">' + user.email + '</div>'    +
  182.             '</div>'                                                +
  183.         '</div>';
  184.  
  185.         return markup;
  186.     }
  187.  
  188.     function formatUserSelection (user) {
  189.         return user.name || user.text;
  190.     }
  191.  
  192.  
  193.  </script>
  194.  
  195.  
  196. <script type="text/javascript">
  197.     if (jQuery === 'undefined') {
  198.         document.addEventListener("DOMContentLoaded", function(event) {
  199.             jQuery('#form-rtreport').submit(function(event) {
  200.  
  201.             });
  202.  
  203.  
  204.             jQuery('input:hidden.user_id').each(function(){
  205.                 var name = jQuery(this).attr('name');
  206.                 if(name.indexOf('user_idhidden')){
  207.                     jQuery('#jform_user_id option[value="' + jQuery(this).val() + '"]').attr('selected',true);
  208.                 }
  209.             });
  210.                     jQuery("#jform_user_id").trigger("liszt:updated");
  211.             jQuery('input:hidden.override_type_id').each(function(){
  212.                 var name = jQuery(this).attr('name');
  213.                 if(name.indexOf('override_type_idhidden')){
  214.                     jQuery('#jform_override_type_id option[value="' + jQuery(this).val() + '"]').attr('selected',true);
  215.                 }
  216.             });
  217.                     jQuery("#jform_override_type_id").trigger("liszt:updated");
  218.         });
  219.     } else {
  220.         jQuery(document).ready(function() {
  221.             jQuery('#form-rtreport').submit(function(event) {
  222.  
  223.             });
  224.  
  225.  
  226.             jQuery('input:hidden.user_id').each(function(){
  227.                 var name = jQuery(this).attr('name');
  228.                 if(name.indexOf('user_idhidden')){
  229.                     jQuery('#jform_user_id option[value="' + jQuery(this).val() + '"]').attr('selected',true);
  230.                 }
  231.             });
  232.                     jQuery("#jform_user_id").trigger("liszt:updated");
  233.             jQuery('input:hidden.override_type_id').each(function(){
  234.                 var name = jQuery(this).attr('name');
  235.                 if(name.indexOf('override_type_idhidden')){
  236.                     jQuery('#jform_override_type_id option[value="' + jQuery(this).val() + '"]').attr('selected',true);
  237.                 }
  238.             });
  239.                     jQuery("#jform_override_type_id").trigger("liszt:updated");
  240.         });
  241.     }
  242. </script>
  243.  
  244. <div class="rtreport-edit front-end-edit">
  245.     <?php if (!empty($this->item->id)): ?>
  246.         <h1><?php echo JText::_('COM_CN_REPORTS_EDIT_ITEM') . '&nbsp;' . $this->item->override_short_desc; ?></h1>
  247.     <?php else: ?>
  248.         <h1><?php JText::_('COM_CN_REPORTS_ADD_ITEM'); ?></h1>
  249.     <?php endif; ?>
  250.  
  251.     <form id="form-rtreport" action="<?php echo JRoute::_('index.php?option=com_cn_reports&task=rtreport.save'); ?>" method="post" class="form-validate form-horizontal" enctype="multipart/form-data">
  252.  
  253.     <input type="hidden" name="jform[id]" value="<?php echo $this->item->id; ?>" />
  254.     <input type="hidden" name="jform[ordering]" value="<?php echo $this->item->ordering; ?>" />
  255.     <input type="hidden" name="jform[checked_out]" value="<?php echo $this->item->checked_out; ?>" />
  256.     <input type="hidden" name="jform[state]" value="<?php echo $this->item->state; ?>" />
  257.     <input type="hidden" name="jform[order_id]" value="<?php echo $this->item->order_id; ?>" />
  258.  
  259.     <div class="control-group">
  260.         <div class="control-label"><?php echo $this->form->getLabel('override_short_desc'); ?></div>
  261.         <div class="controls"><?php echo $this->form->getInput('override_short_desc'); ?></div>
  262.     </div>
  263.  
  264.     <div class="control-group">
  265.         <div class="control-label"><?php echo $this->form->getLabel('override_type_id'); ?></div>
  266.         <div class="controls"><?php echo $this->form->getInput('override_type_id'); ?></div>
  267.     </div>
  268.     <?php foreach((array)$this->item->override_type_id as $value): ?>
  269.         <?php if(!is_array($value)): ?>
  270.             <input type="hidden" class="override_type_id" name="jform[override_type_idhidden][<?php echo $value; ?>]" value="<?php echo $value; ?>" />
  271.         <?php endif; ?>
  272.     <?php endforeach; ?>
  273.  
  274.     <div class="control-group">
  275.         <div class="control-label"><?php echo $this->form->getLabel('retail_partner_id'); ?></div>
  276.         <div class="controls"><?php echo $this->form->getInput('retail_partner_id'); ?></div>
  277.     </div>
  278.     <?php foreach((array)$this->item->retail_partner_id as $value): ?>
  279.         <?php if(!is_array($value)): ?>
  280.             <input type="hidden" class="retail_partner_id" name="jform[user_idhidden][<?php echo $value; ?>]" value="<?php echo $value; ?>" />
  281.         <?php endif; ?>
  282.     <?php endforeach; ?>
  283.  
  284.     <div class="control-group">
  285.         <div class="control-label"><?php echo $this->form->getLabel('customer_id'); ?></div>
  286.             <div class="controls">
  287.                 <select id="jform_customer_id" name="jform[customer_id]" class="customer_id">
  288.                 <?php if ((!empty($this->item->id) && (!empty($this->item->customer_id) ))) {
  289.                     // call html helper method to generate the current selected option
  290.                     // example <option value="1223" selected="selected">John D</option>
  291.                     echo JHtml::_('contentuser.userOption', (int)$this->item->customer_id);
  292.                 } ?>
  293.                 </select>
  294.             </div>
  295.         </div>
  296.         <div class="control-group">
  297.             <div class="control-label"><?php echo $this->form->getLabel('order_id'); ?></div>
  298.             <div class="controls"><?php echo $this->form->getInput('order_id'); ?></div>
  299.         </div>
  300.     <div class="control-group">
  301.         <div class="control-label"><?php echo $this->form->getLabel('amount'); ?></div>
  302.         <div class="controls"><?php echo $this->form->getInput('amount'); ?></div>
  303.     </div>
  304.     <div class="control-group">
  305.         <div class="control-label"><?php echo $this->form->getLabel('override_desc'); ?></div>
  306.         <div class="controls"><?php echo $this->form->getInput('override_desc'); ?></div>
  307.     </div>
  308.     <div class="control-group">
  309.         <div class="control-label"><?php echo $this->form->getLabel('admin_notes'); ?></div>
  310.         <div class="controls"><?php echo $this->form->getInput('admin_notes'); ?></div>
  311.     </div>
  312.     <?php if(empty($this->item->created_by)): ?>
  313.         <input type="hidden" name="jform[created_by]" value="<?php echo JFactory::getUser()->id; ?>" />
  314.     <?php else: ?>
  315.         <input type="hidden" name="jform[created_by]" value="<?php echo $this->item->created_by; ?>" />
  316.     <?php endif; ?>
  317.     <input type="hidden" name="jform[checked_out_time]" value="<?php echo $this->item->checked_out_time; ?>" />
  318.  
  319.     <input type="hidden" name="jform[modified_time]" value="<?php echo $this->item->modified_time; ?>" />
  320.  
  321.     <input type="hidden" name="jform[created_time]" value="<?php echo $this->item->created_time; ?>" />
  322.  
  323.         <div class="control-group">
  324.             <div class="controls">
  325.                 <button type="submit" class="validate btn btn-primary"><?php echo JText::_('JSUBMIT'); ?></button>
  326.                 <a class="btn" href="<?php echo JRoute::_('index.php?option=com_cn_reports&task=rtreportform.cancel'); ?>" title="<?php echo JText::_('JCANCEL'); ?>"><?php echo JText::_('JCANCEL'); ?></a>
  327.             </div>
  328.         </div>
  329.  
  330.         <input type="hidden" name="option" value="com_cn_reports" />
  331.         <input type="hidden" name="task" value="rtreportform.save" />
  332.         <?php echo JHtml::_('form.token'); ?>
  333.     </form>
  334. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement