Custom CButtonColumn solution in CGridView under Yii Framework

Source: Internet
Author: User
Custom CButtonColumn solution in CGridView under Yii Framework

As required by the project, you need to add the shelving and dismounting functions to the product. the most convenient solution is to add the shelving and dismounting links to the CButtonColumn component under admin. php on the product management interface.

Here we need to override the related methods in CButtonColumn to implement this function:

First, we need to define a class PButtonColumn inherited from CButtonColumn, and then add the shelving and dismounting attributes in this class. after studying the source code, the initDefaultButtons () method is used to add buttons and corresponding operations in CButtonColumn.

 Controller-> createUrl ("publish", array ("id" => $ data-> primaryKey ))'; public $ publishButtonOptions = array ('class' => 'Publish '); public $ afterPublish; public $ offlineButtonLabel; public $ offlineButtonImageUrl; public $ offlineButtonUrl = 'yii: app () -> controller-> createUrl ("offline", array ("id" => $ data-> primaryKey ))'; public $ offlineButtonOptions = array ('class' => 'offline'); public $ offlineConfirmation; public $ afterOffl Ine; protected function initDefaultButtons () {if ($ this-> viewButtonLabel = null) $ this-> viewButtonLabel = 'View '; if ($ this-> updateButtonLabel = null) $ this-> updateButtonLabel = 'modify'; if ($ this-> publishButtonLabel = null) $ this-> publishButtonLabel = 'Release'; if ($ this-> offlineButtonLabel = null) $ this-> offlineButtonLabel = 'dismounting '; if ($ this-> deleteButtonLabel = null) $ this-> deleteButtonLabel = 'delete'; if ($ this-> viewButtonImageUrl = Null) $ this-> viewButtonImageUrl = $ this-> grid-> baseScriptUrl. '/view.png'; if ($ this-> updateButtonImageUrl === null) $ this-> updateButtonImageUrl = $ this-> grid-> baseScriptUrl. '/update.png'; if ($ this-> publishButtonImageUrl === null) $ this-> publishButtonImageUrl = $ this-> grid-> baseScriptUrl. '/publish.png'; if ($ this-> offlineButtonImageUrl === null) $ this-> offlineButtonImageUrl = $ this-> grid-> baseScriptUrl. '/offline.png'; If ($ this-> deleteButtonImageUrl === null) $ this-> deleteButtonImageUrl = $ this-> grid-> baseScriptUrl. '/delete.png'; if ($ this-> offlineConfirmation = null) $ this-> offlineConfirmation = 'are you sure you want to remove this product? '; If ($ this-> deleteConfirmation = null) $ this-> deleteConfirmation =' are you sure you want to delete this product? This may cause loss of related information. please proceed with caution! '; Foreach (array ('View', 'update', 'publish', 'offline', 'delete') as $ id) {$ button = array ('label' => $ this-> {$ id. 'buttonlabel'}, 'URL' => $ this-> {$ id. 'buttonurl'}, 'imageurl' => $ this-> {$ id. 'buttonimageurl'}, 'options' => $ this-> {$ id. 'buttonoptions'},); if (isset ($ this-> buttons [$ id]) $ this-> buttons [$ id] = array_merge ($ button, $ this-> buttons [$ id]); else $ this-> buttons [$ id] = $ button;} if (! Isset ($ this-> buttons ['Publish '] ['click']) {$ confirmation = ''; if (Yii: app () -> request-> enableCsrfValidation) {$ csrfTokenName = Yii: app ()-> request-> csrfTokenName; $ csrfToken = Yii: app ()-> request-> csrfToken; $ csrf = "\ n \ t \ tdata: {'$ csrftokenname':' $ csrftoken'}," ;}else $ csrf = ''; if ($ this-> afterPublish === null) $ this-> afterPublish = 'Function (){}'; $ this-> buttons ['Publish '] ['click'] = <
 
  
AfterPublish; jQuery ('# {$ this-> grid-> id }'). yiiGridView ('update', {type: 'post', url: jQuery (this ). attr ('href '), $ csrfsuccess: function (data) {jQuery (' # {$ this-> grid-> id }'). yiiGridView ('update'); afterPublish (th, true, data) ;}, error: function (XHR) {return afterPublish (th, false, XHR );}}); return false;} EOD;} if (! Isset ($ this-> buttons ['offline'] ['click']) {if (is_string ($ this-> offlineConfirmation) $ confirmation = "if (! Confirm (". CJavaScript: encode ($ this-> offlineConfirmation ). ") return false;"; else $ confirmation = ''; if (Yii: app ()-> request-> enableCsrfValidation) {$ csrfTokenName = Yii: app () -> request-> csrfTokenName; $ csrfToken = Yii: app ()-> request-> csrfToken; $ csrf = "\ n \ t \ tdata: {'$ csrfTokenName ': '$ csrfToken'}, ";}else $ csrf =''; if ($ this-> afterOffline === null) $ this-> afterOffline = 'Function () {} '; $ this-> buttons ['offline'] ['click'] = <
  
   
AfterOffline; jQuery ('# {$ this-> grid-> id }'). yiiGridView ('update', {type: 'post', url: jQuery (this ). attr ('href '), $ csrfsuccess: function (data) {jQuery (' # {$ this-> grid-> id }'). yiiGridView ('update'); afterOffline (th, true, data) ;}, error: function (XHR) {return afterOffline (th, false, XHR );}}); return false;} EOD;} if (! Isset ($ this-> buttons ['delete'] ['click']) {if (is_string ($ this-> deleteConfirmation) $ confirmation = "if (! Confirm (". CJavaScript: encode ($ this-> deleteConfirmation ). ") return false;"; else $ confirmation = ''; if (Yii: app ()-> request-> enableCsrfValidation) {$ csrfTokenName = Yii: app () -> request-> csrfTokenName; $ csrfToken = Yii: app ()-> request-> csrfToken; $ csrf = "\ n \ t \ tdata: {'$ csrfTokenName ': '$ csrftoken'}, ";}else $ csrf =''; if ($ this-> afterDelete = null) $ this-> afterDelete = 'Function () {} '; $ this-> buttons ['delete'] ['click'] = <
   
    
AfterDelete; jQuery ('# {$ this-> grid-> id }'). yiiGridView ('update', {type: 'post', url: jQuery (this ). attr ('href '), $ csrfsuccess: function (data) {jQuery (' # {$ this-> grid-> id }'). yiiGridView ('update'); afterDelete (th, true, data) ;}, error: function (XHR) {return afterDelete (th, false, XHR );}}); return false;} EOD ;}}}?>
   
  
 

In admin. php

Array (
'Class' => 'cbuttoncolumn ',
),

Changed:

Array (
'Class' => 'pbuttoncolumn ',
),

You can.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.