Advertisement
axon

Untitled

Apr 17th, 2013
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.64 KB | None | 0 0
  1. <?php
  2. /**
  3. * XColumnWithDeleteLink
  4. *
  5. * Allows to delete column in CGridView column.
  6. *
  7. * @author Arthur Skobara <[email protected]>
  8. * @version 0.1
  9. */
  10. Yii::import('bootstrap.widgets.TbJEditableColumn');
  11.  
  12. class XColumnWithDeleteLink extends TbJEditableColumn
  13. {
  14.     public $editableCssClass = 'editable-header';
  15.     public $canBeDeleteCssClass = 'can-be-delete';
  16.  
  17.     public $deleteURL = '/phone/deletecolumn';
  18.  
  19.     protected $defaultOptions = array(
  20.         'method' => 'POST', // method to use to send edited content (POST or PUT)
  21.         'callback' => null, // Function to run after submitting edited content
  22.         'name' => 'value', // POST parameter name of edited content,
  23.         'id' => null, // POST parameter name of edited div id (if null will be filled with htmlOptions['id'] or $this->id)
  24.         'submitdata' => null, // Extra parameters to send when submitting edited content
  25.         'type' => 'text', // text, textarea or select (or any 3rd party input type)
  26.         'rows' => null, // number of rows if using textarea
  27.         'cols' => null, // number of cols if using textarea
  28.         'height' => '15px', // 'auto', 'none' or height in pixels,
  29.         'width' => '70%', // 'auto', 'none' or width in pixels
  30.         'loadurl' => null, // URL to fetch input content before editing
  31.         'loadtype' => 'GET', // Request type for load url. Should be GET or POST.
  32.         'loadtext' => null, //  Text to display while loading external content.
  33.         'loaddata' => null, // Extra parameters to pass when fetching content before editing.
  34.         'data' => null, // Or content given as paramameter. String or function.
  35.         'indicator' => null, // indicator html to show when saving (will default to assets/img/loading.gif if null)
  36.         'tooltip' => null, // optional tooltip text via title attribute
  37.         'event' => 'click', // jQuery event such as 'click' of 'dblclick'
  38.         'submit' => null, // submit button value, empty means no button
  39.         'cancel' => null, // cancel button value, empty means no button
  40.         'cssclass' => null, // CSS class to apply to input form. 'inherit' to copy from parent.
  41.         'style' => null, //  Style to apply to input form 'inherit' to copy from parent.
  42.         'select' => false, // true or false, when true text is highlighted
  43.         'placeholder' => null, // Placeholder text or html to insert when element is empty.
  44.         'onblur' => null, // 'cancel', 'submit', 'ignore' or function
  45.         'onsubmit' => null, // function(settings, original) { ... } called before submit
  46.         'onreset' => null, // function(settings, original) { ... } called before reset
  47.         'onerror' => null, // function(settings, original, xhr) { ... } called on error
  48.         'ajaxoptions' => null, // jQuery Ajax options. See docs.jquery.com.
  49.         'cancelAttrs' => array('class' => 'btn'), /* custom property */
  50.         'submitAttrs' => array('class' => 'btn'), /* custom property */
  51.         //'mask' => '99/99/9999', /* configuration setting for masked plugin */
  52.         //'dateformat' => 'yyyy/mm/dd', /* you can use this configuration when using the date plugin */
  53.         //'colorformat' => 'rgb' /*  rgb | hex | rgba you can use this parameter when using color picker plugin @see www.eyecon.ro/bootstrap-colorpicker/ */
  54.  
  55.     );
  56.  
  57.     public function init()
  58.     {
  59.         parent::init();
  60.  
  61.         if (!$this->saveURL)
  62.         {
  63.             $this->saveURL = Yii::app()->getRequest()->requestUri;
  64.         }
  65.         $this->registerCustomClientScript();
  66.     }
  67.  
  68.     public function renderHeaderCell()
  69.     {
  70.         $this->headerHtmlOptions['id']=$this->id;
  71.         $this->headerHtmlOptions['data-column-name']=$this->name;
  72.         $this->headerHtmlOptions['class']=$this->canBeDeleteCssClass;
  73.         echo CHtml::openTag('th',$this->headerHtmlOptions);
  74.         $this->renderHeaderCellContent();
  75.         echo "</th>";
  76.     }
  77.  
  78.     protected function renderHeaderCellContent()
  79.     {
  80.         $sort = $this->grid->dataProvider->getSort();
  81.         $label = isset($this->header) ? $this->header : $sort->resolveLabel($this->name);
  82.  
  83.         echo "<span class=\"{$this->editableCssClass}\">". $label .'</span><i class="right icon-trash"></i>';
  84.     }
  85.  
  86.     public function registerCustomClientScript()
  87.     {
  88.         $cs = Yii::app()->getClientScript();
  89.         $assetsUrl = Yii::app()->bootstrap->getAssetsUrl();
  90.         $cs->registerScriptFile($assetsUrl . '/js/jquery.jeditable.js', CClientScript::POS_END);
  91.  
  92.         $deleteURL = Yii::app()->createUrl($this->deleteURL);
  93.  
  94.         $options = CJavaScript::encode(array_filter($this->defaultOptions));
  95.         $cs->registerScript(__CLASS__ . '#' . $this->id, "
  96.             jQuery(document).on('{$this->event}','.{$this->editableCssClass}', function(){
  97.                 var self = jQuery(this);
  98.                 var name = self.parent().attr('data-column-name');
  99.                 var oldValue = self.html();
  100.                 var options = jQuery.extend(true, {$options}, {});
  101.                 self.editable(function(value, settings) {
  102.                     selectedId = $(this).attr('id');
  103.                     $.ajax({
  104.                         url:'{$this->saveURL}',
  105.                         data:{
  106.                             name:name,
  107.                             oldvalue:oldValue,
  108.                             editheader:'{$this->id}',
  109.                             value:value,
  110.                         },
  111.                         type:'post',
  112.                         success: function(data) {
  113.                             self.attr('data-column-name', data);
  114.                             var label = $('.inline-input-item label[name=\"'+oldValue+'\"]');
  115.                             label.attr('name', value);
  116.                             label.html(value);
  117.                         },
  118.                     });
  119.                     return value;
  120.                 }, options);
  121.             });
  122.         ");
  123.  
  124.         $cs->registerScript(__CLASS__ . '#column-delete', "
  125.             $(document).on('click', '.icon-trash', function() {
  126.                 var self = this;
  127.                 var name = $(this).parent().attr('data-column-name');
  128.                 $.ajax({
  129.                     url:'{$deleteURL}',
  130.                     data:{name:name},
  131.                     type:'post',
  132.                     success: function(data) {
  133.                         $.fn.yiiGridView.update('{$this->grid->id}');
  134.                         $('.inline-input-item').each(function(){
  135.                             var self = $(this);
  136.                             if (self.find('label').attr('name') == name) {
  137.                                 self.remove();
  138.                             }
  139.                         });
  140.                     }
  141.                 });
  142.             });
  143.         ");
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement