mau tanya dong ,
ane punya controller untuk actionAdmin macem gini :
public function actionAdmin()
{
$kategoriberita = new Kategori;
if(isset($_GET['Kategori']))
$kategoriberita->attributes=$_GET['Kategori'];
// if(isset($_POST['Kategori']))
// $kategoriberita->attributes=$_POST['Kategori'];
//
// $targ = array('admin');
// foreach($kategoriberita->attributes as $k => $v){
// $targ['Kategori['.$k.']'] = $v;
// }
//
// if (isset($_POST['Kategori']))
// {
// $this->redirect($targ);
// return;
// }
$model=new Kategori('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Kategori']))
$model->attributes=$_GET['Kategori'];
$this->render('admin',array(
'model'=>$model,
'kategoriberita'=>$kategoriberita,
));
}
untuk model ane buat gini :
<?php
/**
* This is the model class for table "kategori".
*
* The followings are the available columns in table 'kategori':
* @property integer $id
* @property string $nama
*/
class Kategori extends CActiveRecord
{
public $kategori="Politik";
/**
* Returns the static model of the specified AR class.
* @return Kategori the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'kategori';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('nama', 'required'),
array('nama', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, nama', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'nama' => 'Kategori',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('nama',$this->nama,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public static function getKategory(){
$criteria = new CDbCriteria;
$criteria->select='nama';
$kategori = Kategori::model()->findAll($criteria);
foreach($kategori as $row){
$option[$row->nama]=$row->nama;
}
return $option;
}
}
untuk view admin, macem gini :
<?php
$this->breadcrumbs=array(
'Kategoris'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Kategori', 'url'=>array('index')),
array('label'=>'Create Kategori', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('kategori-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Kategoris</h1>
<? $form=$this->beginWidget('CActiveForm')?>
<? echo $form->labelEx($kategoriberita,'Pilih Kategori : ')?>
<? echo $form->dropDownList($kategoriberita, 'kategori', Kategori::getKategory())?>
<? echo CHtml::submitButton('Search',array('admin'))?>
<? $this->endWidget()?>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'kategori-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'nama',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
nah, yang ingin tanyakan, ane pingin ngesearch data kategori berdasarkan menu dropdown yang ane pilih , gmana ya gan biar action saat tombol search di klik maka data yang tampil di table bakalan keluarin data sesuai pilihan dropdown
makasih ya buat ya mau jawab …