Menus

Monday 19 December 2016

Mannually set a where condion for dataprovider in specific controller method



This code queryParams take from user index form.

public function actionIndex()
    {
        
            $searchModel = new StudentsSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }


To add additional where condition for data provider.


public function actionIndex()
    {            
            $ccid = Yii::$app->user->identity->ccid;
            $searchModel = new StudentsSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $dataProvider->query->andWhere('center_ccid = '.$ccid);         
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

Usages:



$dataProvider = $searchModel->search([$searchModel->formName()=>['result_type'=>$result->result_type,'date'=>$result->date]]);
$query->andFilterWhere(['<>', 'role', $this->role]);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $role = 'regular');
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['lang'=>'ENG']);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams+['EmployeeSearch' => ['<>', 'role' =>'regular']]);



Yii2 How to pass a range to search model


You can also use BETWEEN construction:
$query->andFilterWhere(['between', 'price', $this->min_price, $this->max_price]);

yii2 data provider default sorting


defaultOrder contain a array where key is a column name and value is a SORT_DESC orSORT_ASC 


$dataProvider->setSort([
'defaultOrder' => ['date'=>SORT_DESC,'result_type'=>SORT_ASC,'chance'=>SORT_ASC],
]);


$dataProvider = new ActiveDataProvider([
            'query'=> $query,
            'sort'=> [
                   'defaultOrder'=> ['your_column'=>SORT_ASC]]
       ]);





Tuesday 6 December 2016

Getting value form a table using Ajax

Yii2 Ajax Operations

Value selected from a database table using ajax in yii2

Example explains to get amount of Rate and Ta from  staffdetails table with depend on staff  drop down list changed in staff log book view.




First create tow tables staffdetails and stafflogbook and do CRUD operations of these tables.

Table staffdetails
Fields
id
staff_name
.
.
rate
ta
.
.

Table stafflogbook
Fields
id
staffdetails_id
.
.
.
.

Open staff log book view and add to javascript change event of staff dropdown list controller.

<?php

$script = <<< JS
    $('#stafflogbook-staffdetails_id').change(function(){
      var id = $(this).val();
        $.get("index.php?r=master/staffdetails/get-rate-ta", {staff_id : id}, function(data) {
        
        var data = $.parseJSON(data);  
        
        $('#stafflogbook-rate').attr('value',data.rate);
        $('#stafflogbook-ta').attr('value',data.ta);
        
        });
   });        
JS;
$this->registerJs($script);

?>


* Get some value form staffdetails for write a function in StaffdetailsController

     public function actionGetRateTa($staff_id){
        //Find the staff_id from the Staffdetails table
        $rate = Staffdetails::findOne(['id'=>$staff_id]);
        echo json::encode($rate);      
    }


So json need to add json helper class

use yii\helpers\Json;



Video  - Getting value form a table using Ajax





Error in Javascript Function Call On Yii2 Dropdown Active Form


HOW to Use onChange on DropDownList

onchange function in dropDownList yii2


Use

$this->registerJs($script, \yii\web\View::POS_END);

The last part means - add this script straightforward at the end of page.


Code add in header area on view form

<?php

...

$script = <<< JS
    $('#students-course_applied').change(function(){
      var id = $(this).val();
       
       
        alert(id);
       
        test();
       
   });  
     
        function test()
        {
        alert("Test Function");
        }
JS;
$this->registerJs($script, \yii\web\View::POS_END);

?>



Yii dropdown call javascript function


<?= $form->field($model, 'admission_mode')->dropDownList(
                    ArrayHelper::map(Groupitems::find()->andwhere(['category'=>13])->orderBy('description')->all(),'slid','description'),
                    ['prompt'=>'Select One' , 'onchange'=>'test();']
                    )   ?>








Thank you for visiting