Tuesday 25 March 2014

Check Yii CGridview empty

If you are working on Yii framework then sometime you need to check whether your CGridView widget (Generated by Yii) has empty data or not. Recently I've faced problem where I've to redirect user to create page if there is no data in CGridView depending on user's role and conditions. After couple of googling I've found few simple lines and modified my model code which worked for me.

In model: Put CActiveDataProvider into a variable:
<?php
    $dataProvider = new CActiveDataProvider($this, array(
        'criteria' => $criteria,
        'pagination' => array('pageSize' => 10),
        'sort' => array(
            'defaultOrder' => 'create_dttm DESC',
        )
    ));
?>
Check if dataprovider is empty or not:
<?php
  if (!isset($_REQUEST['ajax']) && $dataProvider->totalItemCount < 1) {
   Yii::app()->getController()->redirect(array('create'));
  } else {
   return $dataProvider;
  }    
?>
If the above code doesn't works then you can also return an empty data provider like this:
return new CActiveDataProvider($this, array('data' => array()));

Tuesday 4 March 2014

Simple form cloning in JQuery

Simple JQuery-HTML based form cloning functionality. It not only clones the normal textfield, textarea, file field and dropdown field but also JQuery Datepicker and Maskmoney field.
Maskmoney is just a simple way to create masks to your currency form fields with JQuery. For more reference and live demo check this link : http://plentz.github.io/jquery-maskmoney/

See the demo for more information:


Monday 3 March 2014

JQuery selectors and how to use them

Get selected text from dropdown

$("#myid option:selected").text();

Get selected value from dropdown

$("#myid option:selected").value();

Get input type text value

$("#myid input[type=text]").val();

Get checked radiobutton value by class

$(".myclass").click(function() {
  var input = $('.myclass:checked').val();
  alert(input);
});

Check whether radiobutton or checkbox checked or not

$("#test").click(function() {
   var check = $('#test').attr('checked');
   if(check === "checked")
        //condition
   else
        //condition
});

Clear all form fields (i.e. text-field, file-field, drop-down, check-box and radio-buttons)

$('input[type="text"]').val('');
$('input[type="file"]').val('');
$('input[type="radio"]').prop('checked', false);
$('input[type="checkbox"]').prop('checked', false);
$("#mydropdown option:selected").prop("selected", false);

If you want to exclude any one field then you can do like this. Ex. you want to exclude one drop-down field

Option1:

$('select[name != "test"]').val('');

Option2:

$("select:not([name='User[name]'], [name='User[email]'])").val("");

Event based on the value of data attribute

$( "input[data-role-type='privilege_role']" )

Get concatenated name in Yii dropDownList

If you have two fields like firstname and lastname in your table then sometime you need to show concatenated name in dropDownList that is name with first name and last name. Following is the post where you will find how to deal with this in Yii Framework.

First of all you need to create method in your model.

Model (User in this case)

<?php
public function getConcatened(){
    return $this->firstname . '  ' . $this->firstname;
}
?>

Then call that function from CHtml list data by passing function name as a third parameter.

In drop down list:

<?php
$list = CHtml::listData(User::model()->findAll(array('order' =>     'firstname')), 'id', 'concatened');
echo $form->dropDownListRow($model, 'id', $list);
?>