if should add an id attribute to the tr element of CGridView

i think it’s better to add id property to the generated tr . for example when we use the tableDnd jquery plugin: tableDndPlugin that requires a table and its row must have the id property .

since we can get the dataRowSet 's keys ,why not render it as a row’s id ?





 

        /**

         * Renders a table body row.

         * @param integer $row the row number (zero-based).

         */

        public function renderTableRow($row)

        {

                if($this->rowCssClassExpression!==null)

                {

                        $data=$this->dataProvider->data[$row];

                        echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'">';

                }

                else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)

                        echo '<tr class="'.$this->rowCssClass[$row%$n].'">';

                else

                        echo '<tr>';

                foreach($this->columns as $column)

                        $column->renderDataCell($row);

                echo "</tr>\n";

        }






you see yii just give the tr a class property .

in the CBaseListView you can get the keys of every rows :




 /**

         * Renders the key values of the data in a hidden tag.

         */

        public function renderKeys()

        {

                echo CHtml::openTag('div',array(

                        'class'=>'keys',

                        'style'=>'display:none',

                        'title'=>Yii::app()->getRequest()->getUrl(),

                ));

                foreach($this->dataProvider->getKeys() as $key)

                        echo "<span>".CHtml::encode($key)."</span>";

                echo "</div>\n";

        }



all right ! :lol:

may be i should write a subclass to do that:




class EGridView extends CGridView

{


    protected $rowSetKeys = array();


    public $rowIdPrefix = 'gr_';


    public function init(){

        parent::init();


        $this->rowSetKeys = $this->dataProvider->getKeys();

    }


    /**

     * @param $row

     * ad row id property to the generated tr element

     */

    public function renderTableRow($row){

        $rowId  =$this->rowIdPrefix.$this->rowSetKeys[$row];


        if($this->rowCssClassExpression!==null)

        {

            $data=$this->dataProvider->data[$row];

            echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'" id="'.$rowId.'">';

        }

        else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)

            echo '<tr class="'.$this->rowCssClass[$row%$n].'" id="'.$rowId.'">';

        else

            echo '<tr id="'.$rowId.'">';

        foreach($this->columns as $column)

            $column->renderDataCell($row);

        echo "</tr>\n";

    }


}






Good Job Dude… :thumb up:

Thanks, it’s really work… :thumb up:

Just write in grid options




'rowHtmlOptionsExpression'=>'array("id"=>$data->id)',



http://www.yiiframework.com/doc/api/1.1/CGridView#rowHtmlOptionsExpression-detail