简体   繁体   中英

cgridview related fields filtering

I have a problem that I have solved for all but one of the cgridview filters, which is a related field.

I am using a solution Seenivasan has supplied but the related field status does not get added to the string.

here is the cgridview :-

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'tbl-orders-grid',
    'dataProvider'=>$dataProvider,
    'afterAjaxUpdate'=>"function() {
                                                jQuery('#Orders_date_first').datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['id'], {'showAnim':'fold','dateFormat':'yy-mm-dd','changeMonth':'true','changeYear':'true','constrainInput':'false'}));
jQuery('#Orders_date_last').datepicker(jQuery.extend({showMonthAfterYear:false}, jQuery.datepicker.regional['id'], {'showAnim':'fold','dateFormat':'yy-mm-dd','changeMonth':'true','changeYear':'true','constrainInput':'false'}));
                                                }",

'filter'=>$model,
'columns'=>array(
    array(
        'name'=>'id',
        'type'=>'raw',
        'value'=>'str_pad($data->id,8-strlen($data->id),"0",STR_PAD_LEFT)',
        'htmlOptions'=>array('style' => 'text-align: right;width: 60px;')
    ),
    array(
            'name'=>'date_order_placed',
            'filter'=>$dateisOn,
            'value'=>'date("d-m-Y",strtotime($data->date_order_placed))',
            ), 
    array(
      'name'=>'status',
      'type'=>'raw',
      //'htmlOptions'=>array('id' => 'order_status_search'),
      'value'=>'CHtml::value($data,"status.orders_status_name")',
      'filter'=>CHtml::listData(OrderStatus::model()->findAll(
                  array(
                   'select'=>array('orders_status_name'),
                   'distinct'=>true

                  )),"orders_status_name","orders_status_name")//this is the focus of your code

   ),

    array(
        'class'=>'EButtonColumnWithClearFilters',
         'clearVisible'=>true,
        'template'=>'{view}{email}',
        'buttons'=>array
        (
            'email' => array
            (
                'label'=>'Reprint invoice and email to yourself',
                'imageUrl'=>Yii::app()->request->baseUrl.'/images/email.png',
                'url'=>'Yii::app()->createUrl("orders/ureprint", array("id"=>$data->id))',


            ),

        ),
    ),
),
)); ?>

here is the php and js that adds the current filter values to the url.

echo CHtml::button('List Orders',array('id'=>'listelement'));

Yii::app()->clientScript->registerSCript('test','
$("body").on("click","#listelement",function(){
        var str="&";
    $.each($("#tbl-orders-grid input").serializeArray(),function(i,j){
            str=str+j.name+"="+j.value+"&";

            });


            window.location="'.CHtml::normalizeUrl(array('orders/index')).'"+str;
    });

');

The other filters, dates and id, work perfectly. They are added to the url above. Status is not.

1- you need to define new filters in your model serach() methode for example like

 $criteria->compare('Restaurant.Name',$this->Name,true);  

Restaurant is your relation key , name : value from another table related to this fk

2- you have to add value of gridview column like :

view:

array(
          'name' => 'Restaurant.Name',
          'header' => 'Username',
          'filter' => CHtml::activeTextField($model, 'name'),
          'value' => '$data->Restaurant->Name',

This link will help you yii CGridView filter with relations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM