Question

I just create Button dropdowns's Twitter Bootstrap for CGridView column. The button contains sub menus and each of them has attributes such as href, class, data-, etc.. which can set through dropdownMenuItems attribute in CGridView widget.

In view

$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'ajaxUpdate'=>false,
  'columns'=>array(
        array(
            'name'=>'ID',
            'type'=>'raw',
            'value'=>'$data->ID',
        ),
        array(
            'name'=>'NAME',
            'type'=>'raw',
            'value'=>'$data->NAME',
        ),
        array(
            'class'=>'BootstrapButtonDropdownColumn',                                       
            'name'=>'',
            'sortable'=>false,
            'dropdownMenuItems'=>array(
                array('label'=>'<i class="icon-edit"></i> Edit', 'link'=>$this->createUrl('site/update')),
                array('itemClass'=>'divider'), 
                array('label'=>'<i class="icon-remove"></i> Remove', 'link'=>'#', 'itemHtmlOptions'=>array('class'=>'remove-item','data-id'=>'$data->ID','data-toggle'=>'modal'))
        ),
   ),                                              
),

Class file

class BootstrapButtonDropdownColumn extends CDataColumn {

    public $buttonLabel = 'Action';
    public $buttonClass = 'btn btn-small dropdown-toggle';
    public $buttonHtmlOptions=array();
    public $dropdownMenuItems=array();

     protected function renderDataCellContent($row, $data) 
     {
        $html = '<div class="btn-group pull-right">';

        $buttonOption = $this->buttonHtmlOptions;
        $buttonOption['class'] = $this->buttonClass;
        $buttonOption['data-toggle'] = 'dropdown';
        $html .= CHtml::htmlButton($this->buttonLabel . ' <span class="caret"></span>', $buttonOption);     

        $html .= '<ul class="dropdown-menu">';

        for ($i=0; $i<count($this->dropdownMenuItems); $i++)
        {
            if (isset($this->dropdownMenuItems[$i]['itemClass']))
                $html .= '<li class="' . $this->dropdownMenuItems[$i]['itemClass'] . '">';
            else
                $html .= '<li>';

            $label = '';
            $link = '#';
            $itemHtmlOptions = null;

            if (isset($this->dropdownMenuItems[$i]['label']))
                $label = $this->dropdownMenuItems[$i]['label'];

            if (isset($this->dropdownMenuItems[$i]['link']) && !empty($this->dropdownMenuItems[$i]['link']))
                $link = $this->dropdownMenuItems[$i]['link'];

            if (isset($this->dropdownMenuItems[$i]['itemHtmlOptions']))
                $itemHtmlOptions = $this->dropdownMenuItems[$i]['itemHtmlOptions'];

            $html .= CHtml::link($label, $link, $itemHtmlOptions);
            $html .= '</li>';
        }

        $html .= '</ul></div>';
        echo $html;
    }
}

There's a problem, I'd like to set value to data-id attribute on remove menu item but it not work. When see in Browser Element Inspect the data-id not contain record id but it show string $data->id instead.

  <div class="btn-group pull-right open">
      <button class="btn btn-small dropdown-toggle" data-toggle="dropdown" name="yt0" type="button">
          Action <span class="caret"></span>
      </button>
      <ul class="dropdown-menu">
          <li><a href="/site/update"><i class="icon-edit"></i> Edit</a></li>
          <li class="divider"><a href="#"></a></li>
          <li><a class="remove-item" data-id="$data->ID" data-toggle="modal" href="#"><i class="icon-remove"></i> Remove</a></li>
      </ul>
  </div>

or see in this image

Please give me some advice, Thank you.

Was it helpful?

Solution

Thank for advice but, Is there another way to pass id into class

You have ability to access $row and $data. I think all you need just add more below line into your custom button class

if (isset($this->dropdownMenuItems[$i]['itemHtmlOptions'])){
                $itemHtmlOptions = $this->dropdownMenuItems[$i]['itemHtmlOptions'];
                $itemHtmlOptions['data-id'] = $data->ID; // <==added line, maybe you want to use condition or put it somewhere else instead, depend your decision
            }

Of cause now you don't need 'data-id'=>$data->ID from your view anymore, remove it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top