1. PHP
  2. Yii Framework

Yii 2 – Membuat Star Rating

Star Rating

Website penyedia berita / artikel kebanyakan akan menyediakan sistem rating untuk tiap artikel yang ditulisnya. Dengan sistem rating ini, website akan memberikan kesempatan pada pengunjung untuk menilai konten/isi halaman yang . Bagi pemilik website, sistem rating ini dapat memberikan gambaran guna mengevaluasi penyajian isi website.

Ada banyak jenis sistem rating yang digunakan di website saat ini, salah satunya adalah star rating. Star Rating merupakan sistem rating yang menggunakan bintang sebagai nilainya, semakin banyak bintang yang dipilih, maka semakin besar juga nilai ratingnya.

Pada artikel ini, akan di contohkan bagaimana cara membuat Star Rating pada Yii 2 Framework.

Instalasi

Cara Instalasi extension ini dengan menggunakan composer.

Setelah anda memiliki composer terinstall, kemudian jalankan perintah ini pada command prompt.

php composer.phar require --prefer-dist alfa6661/yii2-raty "*"

atau tambahkan kode dibawah ini pada bagian require di file composer.json anda.

"alfa6661/yii2-raty": "*"

Penggunaan

Tanpa model

<?= \alfa6661\widgets\Raty::widget([
    'name' => 'user-vote',
    'options' => [
        // the HTML attributes for the widget container
    ],
    'pluginOptions' => [
        // the options for the underlying jQuery Raty plugin
        // see : https://github.com/wbotelhos/raty#options
    ]
]); ?>

Dengan Model

<?= \alfa6661\widgets\Raty::widget([
    'model' => $model,
    'attribute' => 'rating',
    'options' => [
        // the HTML attributes for the widget container
    ],
    'pluginOptions' => [
        // the options for the underlying jQuery Raty plugin
        // see : https://github.com/wbotelhos/raty#options
    ]
]); ?>

Dengan ActiveForm

<?= $form->field($model, 'point')->widget(\alfa6661\widgets\Raty::className(), [
    'options' => [
        // the HTML attributes for the widget container
    ],
    'pluginOptions' => [
        // the options for the underlying jQuery Raty plugin
        // see : https://github.com/wbotelhos/raty#options
    ]
]); ?>

Javascript events handling

// Available event callbacks: Read Only, Click, Mouseover and Mouseout
<?= \alfa6661\widgets\Raty::widget([
    'name' => 'user-vote',
    'options' => [
        'class' => 'pull-left',
        'id' => 'user-vote'
    ],
    'pluginOptions' => [
        'click' => new \yii\web\JsExpression('function(score, e) {
            alert(score);
        }')
    ]
]); ?>

Resource

Comments to: Yii 2 – Membuat Star Rating

    Your email address will not be published. Required fields are marked *

    Attach images - Only PNG, JPG, JPEG and GIF are supported.