Wednesday, February 24, 2016

[Yii2 Framework] Export GridView dengan ext yii2-export

Tambahkan pada require di composer.json

lalu gunakan cmd masukke direktori tempat composer.json berada dan jalankan perintah:

tambahkan modules gridview pada config/main.php

buka index.php dan tambahkan script berikut, dan edit sesuai kebutuhan

[Yii2 Framework] BlameableBehavior dan TimestampBehavior

tambah kolom pada tabel
            create_by tipe data INT
            update_by tipe data INT
            create_at tipe data DATETIME
            update_at tipedata DATETIME
tambah fungsi beahavior pada model siswa.php



untuk edit tampilan di view klik disini

[Yii2 Framework] menampilkan tabel yang tidak berrelasi pada DetailView

edit view.php


sekarang kita buat fungsi relasi pada Siswa.php

Sunday, February 21, 2016

[Yii2 Framwork] Membuat Dropdown list pada form

Tambahkan scrip pada _form.php dan edit sesuai kebutuhan

1
2
3
4
5
6
7
8
9
10
11
12
13
    <?= $form->field($model, 'companies_company_id')->dropDownList(
     ArrayHelper::map(Companies::find()->all(),'company_id','company_name'),
     [
            'prompt'=>'Select Company',
        ]
    ); ?>
 
    <?= $form->field($model, 'branches_branch_id')->dropDownList(
     ArrayHelper::map(Branches::find()->all(),'branch_id','branch_name'),
     [
            'prompt'=>'Select Branch',
        ]
    ) ?>

[YII2 Framwork] Menampilkan, mengurutkan dan memfilter Model yang ber-Relasi Pada GridView

menggunakan model "Tour":
/**
 * @return \yii\db\ActiveQuery
 */
public function getCountry()
{
    return $this->hasOne(Country::className(), ['id' => 'country_id']);
}
 
/**
 * @return \yii\db\ActiveQuery
 */
public function getCity()
{
    return $this->hasOne(City::className(), ['id' => 'city_id']);
}
menambahkan nama country dan nama city pada GridView. didalam model  "TourSearch" :
class TourSearch extends Tour // extends from Tour see?
{
    // add the public attributes that will be used to store the data to be search
    public $city;
    public $country;
 
    // now set the rules to make those attributes safe
    public function rules()
    {
        return [
            // ... more stuff here
            [['city', 'country'], 'safe'],
            // ... more stuff here
        ];
    }
// ... model continues here
set pada gridview
// ... more grid configuration here
 'columns' => [
 // ... more columns configuration here
 [
 'attribute' => 'city',
 'value' => 'city.name'
 ],
 [
 'attribute' => 'country',
 'value' => 'country.name'
 ],
 // ... more stuff here
sekarang kita fokus pada fungction "search" pada "TourSearch"
public function search($params)
{
    // create ActiveQuery
    $query = Tour::find();
    // Important: lets join the query with our previously mentioned relations
    // I do not make any other configuration like aliases or whatever, feel free
    // to investigate that your self
    $query->joinWith(['city', 'country']);
 
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
 
    // Important: here is how we set up the sorting
    // The key is the attribute name on our "TourSearch" instance
    $dataProvider->sort->attributes['city'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['tbl_city.name' => SORT_ASC],
        'desc' => ['tbl_city.name' => SORT_DESC],
    ];
    // Lets do the same with country now
    $dataProvider->sort->attributes['country'] = [
        'asc' => ['tbl_country.name' => SORT_ASC],
        'desc' => ['tbl_country.name' => SORT_DESC],
    ];
    // No search? Then return data Provider
    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }
    // We have to do some search... Lets do some magic
    $query->andFilterWhere([
        //... other searched attributes here
    ])
    // Here we search the attributes of our relations using our previously configured
    // ones in "TourSearch"
    ->andFilterWhere(['like', 'tbl_city.name', $this->city])
    ->andFilterWhere(['like', 'tbl_country.name', $this->country]);
 
    return $dataProvider;
}
semoga bermanfaat, :)
sumber: http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | Best Buy Printable Coupons