New Record And Update Record With Cgridview And Csqldataprovider

Hi everyone, this is my first post, but I am a follower of this forum for a long time, and I always found an answer to my doubts, but this one I didn’t.

I have a CGridView that is used to create or update records and it has CHTML textfields and dropdowns. When is a new record the grid is filled with defaults values like the image 5371

screen_007.gif

What I have done is two arrays of gridColumn that is choosed if is $model->isNewRecord or not, like the codes below:

Columns for new record




$gridColumns = array(

    array('name' => 'IND_ID', 'header' => '#', 'htmlOptions' => array('style' => 'width: 50px')),

    array('name' => 'IND_DESCRICAO'),

    array('name' => 'IND_UTILIZA', 

          'header' => 'Utiliza?',

          'type'=>'raw',

          'value'=>'CHtml::dropDownList("IND_UTILIZA","", 

                    array("0" => "Não","1" => "Sim"),

                    array("style" => "width: 70px")

                   )'

    ),

    array('name' => 'SCORECARD',

        'header' => 'Scorecard',

        'type'=>'raw',

        'value'=>'CHtml::dropDownList("SCORECARD","", 

                    array("0" => "Não","1" => "TNC","2" => "Master", "3" => "Todos"),

                    array("style" => "width: 80px")

                 )'

    ),

    array('name' => 'IND_OBJETIVO', 

          'type'=>'raw',

          'header' => 'Objetivo', 

          'value'=>'CHtml::textField("IND_OBJETIVO","",

                        array("class" => ($data[\'IND_TIPORESPOSTA\'] == 1)?"tipoResposta":"tamanho")

                    )'

         ),

(...)



Columns for Update Record




$gridColumns = array(

    array('name' => 'IND_ID', 'header' => '#', 'htmlOptions' => array('style' => 'width: 50px')),

    array('name' => 'IND_DESCRICAO'),

    array('name' => 'IND_UTILIZA', 

          'header' => 'Utiliza?',

          'type'=>'raw',

          'value'=>'CHtml::dropDownList("IND_UTILIZA",$data[\'IND_UTILIZA\'], 

                    array("0" => "Não","1" => "Sim"),

                    array("style" => "width: 70px")

                   )'

    ),

    array('name' => 'SCORECARD',

        'header' => 'Scorecard',

        'type'=>'raw',

        'value'=>'CHtml::dropDownList("SCORECARD", $data[\'SCORECARD\'], 

                    array("0" => "Não","1" => "TNC","2" => "Master", "3" => "Todos"),

                    array("style" => "width: 80px")

                 )'

    ),

    array('name' => 'IND_OBJETIVO', 

          'type'=>'raw',

          'header' => 'Objetivo', 

          'value'=>'CHtml::textField("IND_OBJETIVO",

                        ($data[\'IND_TIPORESPOSTA\'] == 1)? $data[\'OBJETIVO\']."%": $data[\'OBJETIVO\'],

                        array("class" => ($data[\'IND_TIPORESPOSTA\'] == 1)?"tipoResposta":"tamanho")

                    )'

         ),

(...)



The only difference is the value attribute of CHTML elements, and It is working so far.

I have almost sure that there is a much better way to do it, but I can´t figure it out.

Can someone help me with this?