Addi
Addi

Reputation: 199

How do I extend a Yii2 kartik-v grid DataColumn to a BooleanColumn?

Currently:

Model: ProductSearch

<?php
    namespace frontend\models;

    use yii\base\Model;
    use yii\data\ActiveDataProvider;
    use frontend\models\Product;

    class ProductSearch extends Product
    {
      public function rules()
      {
        return [
        [['id','productcategory_id', 'productsubcategory_id'], 'integer'],
        [['name', 'surname','frequency','contactmobile', 'gc_number','specialrequest', 
          'sellstartdate', 'sellenddate', 'discontinueddate', 'modifieddate'], 'safe'],
        [['listprice'], 'number'],
        [['isactive'],'boolean'],
        ];
      }

      public function scenarios()
      {
         return Model::scenarios();
      }

      public function search($params)
      {
         $query = Product::find();

         $dataProvider = new ActiveDataProvider([
          'pagination' => ['pageSize' => 10],
          'query' => $query,
          'db'=> \frontend\components\Utilities::userdb(),
         ]);

         $this->load($params);
         if (!$this->validate()) {
            return $dataProvider;
         }

         $dataProvider->sort->attributes['productcategory_id'] = [
         'asc' => ['productcategory_id' => SORT_ASC],
         'desc' => ['productcategory_id' => SORT_DESC],
         ];
         $dataProvider->sort->attributes['productsubcategory_id'] = [
         'asc' => ['productsubcategory_id' => SORT_ASC],
         'desc' => ['productsubcategory_id' => SORT_DESC],
         ];

         $query->andFilterWhere([
            'id' => $this->id,
            'listprice' => $this->listprice,
            'isactive'=>$this->isactive,
            'productcategory_id' => $this->productcategory_id,
            'productsubcategory_id' => $this->productsubcategory_id,
            'frequency'=>$this->frequency,
            'name'=>$this->name,
            'surname'=>$this->surname,
            'gc_number'=>$this->gc_number,
            'sellstartdate' => $this->sellstartdate,
            'sellenddate' => $this->sellenddate,
            'discontinueddate' => $this->discontinueddate,
            'modifieddate' => $this->modifieddate,
        ])
        ->all();
        return $dataProvider;
    }
}

Current Grid Picture and Code

enter image description here

[
 'class' => 'kartik\grid\DataColumn',
 'attribute'=>'isactive',
 'value' => function ($dataProvider) {
                return $dataProvider->isactive; 
        },
 'filter'=>Html::activeCheckbox($searchModel,'isactive',ArrayHelper::map(Product::find()->indexBy('isactive')->asArray()->all(),'isactive','isactive'),[ 'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px']]),
 'filterType'=>GridView::FILTER_CHECKBOX,
 'filterInputOptions' => [
              'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px'],
              'label'=>'',
 ],
 'filterWidgetOptions'=>[
              'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px'],
              'type'=>\kartik\switchinput\Switchinput::CHECKBOX
 ],               
],


<?php 
    echo kartik\grid\GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $gridColumns,
    'bootstrap'=>true,
    'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px'],
    'containerOptions' => ['style'=>'overflow: auto'], 
    'pjax' => true,
    'pjaxSettings' =>[
                      'neverTimeout'=>true,
                      'options'=>['id'=>'kv-unique-id-7'],                      
                     ], 
    'responsiveWrap'=>true,
    'bordered' => true,    
    'striped' => true,
    'condensed' => true,
    'responsive' => true,
    'hover' => true,
    'floatHeader' => false,
    'showPageSummary' => true,
    'panel' => [
    'type' => GridView::TYPE_PRIMARY,
    'heading'=> $comptel,
    ],
    'exportConfig' => [
                   GridView::CSV => ['label' => Yii::t('app','Export as CSV'),'config' => 
$config_array, 'filename' => 'Houses_Printed_'.date('d-M-Y')],
                   GridView::HTML => ['label' => Yii::t('app','Export as HTML'),'config' => 
$config_array, 'filename' => 'Houses_Printed_'.date('d-M-Y')],
                   GridView::PDF => [ 'label' => Yii::t('app','Export as PDF'),'config' => 
$config_array, 'filename' => 'Houses_Printed_'.date('d-M-Y')], 
                   GridView::EXCEL=> ['label' => Yii::t('app','Export as EXCEL'), 'filename' => 
'Houses_Printed_'.date('d-M-Y')],
                   GridView::TEXT=> ['label' => Yii::t('app','Export as TEXT'), 'filename' => 
'Houses_Printed_'.date('d-M-Y')],
                 ],
]);
?>

If I change the format to 'boolean' I get yes's however I would prefer to see an image of a tick or a cross. A tick for the yes's and a cross for the no's.

How can I accomplish this given the above code? My data is filtering nicely so that if I check on the checkbox column under active, it either displays zero's or one's.

Upvotes: 0

Views: 846

Answers (1)

Addi
Addi

Reputation: 199

I have managed to sort this problem out by changing the classname to Boolean. The BooleanColumn extends from the DataColumn. I removed the:

'format' => 'boolean'

as well. Even though the filter in kartik\grid\BooleanColumn is auto-generated it is generating the following output for all my one's which is what I want:

enter image description here

Here is my final code:

[
 'class' => 'kartik\grid\BooleanColumn',
 'attribute'=>'isactive',
 'value' => function ($dataProvider) {
                return $dataProvider->isactive; 
        },
 'filter'=>Html::activeCheckbox($searchModel,'isactive',ArrayHelper::map(Product::find()->indexBy('isactive')->asArray()->all(),'isactive','isactive'),[ 'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px']]),
 'filterType'=>GridView::FILTER_CHECKBOX,
 'filterInputOptions' => [
              'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px'],
              'label'=>'',
 ],
 'filterWidgetOptions'=>[
              'options' => ['style'=> 'font-size:'.Yii::$app->session['sliderfontproduct'].'px'],
              'type'=>\kartik\switchinput\Switchinput::CHECKBOX
 ],               
], 

Upvotes: 0

Related Questions