Kartik的星级评估小部件:插件方法

时间:2018-01-12 13:01:34

标签: yii2

引言:

  1. 我有一个关于Yii2的博客。用于评分的小部件是: http://demos.krajee.com/widget-details/star-rating#basic-usage
    http://plugins.krajee.com/star-rating#installation
  2. 来自博客的每个帖子都有从数据库中读取的 AVERAGE RATING
  3. 用户选择自己的评分 - 隔离评级后,平均评分在RatingController中计算并记录在数据库中。
  4. 问题:

    用户点击星标后,如何动态显示新的计算评分(平均评分)?现在举例说明(例如2选民),如果平均评分是4.5并且用户选择例如3.5,那么之后它显示 3.5 而不是4.如果更新页面比它好,它显示 4 。如何应用"插件方法"?,例如'更新'或者'刷新'?

1 个答案:

答案 0 :(得分:0)

这个概念很简单,如果你集中注意力,你必须在点击星标后立即更新评级,然后你想要更新使用插件所需的评级Import Foundation class Master { let playerName : String = "" let hp : Int = 0 var isAlive : Bool { get{ return hp >0 } } let avatar : String? = nil init (playerName: String, hp: Int, isAlive: Bool, avatar: String? = nil ) { self.playerName = playerName self.hp = hp self.isAlive = isAlive self.avatar = avatar } extension Master: CustomStringConvertible { var description: String() { if(avatar.isEmpty){ return name + " - (" + isAlive + " / " + hp + ") - Nie posada awatara" }else{ return name + " - (" + isAlive + " / " + hp + ") - Posiada awatar" } } } } class MasterViewController: UIViewController { var masters = [Master]() override func viewDidLoad() { super.viewDidLoad() (0..<10).forEach { masters.append(Master(playerName: "q"+$0, hp: 12 + $0, avatar: nil) (playerName: "w"+$1, hp: 12 + $1, avatar: nil) (playerName: "e"+$2, hp: 12 + $2, avatar: nil) (playerName: "qw"+$3, hp: 12 + $3, avatar: nil) (playerName: "we"+$4, hp: 12 + $4, avatar: nil) (playerName: "2"+$5, hp: 12 + $5, avatar: nil))} let aliveMasters = masters.filter { $0.isAlive } let mappedMasters: [String] = masters.map { $0.playerName } print("Alive Masters are : \(mappedMasters)") } events提供methodsrating:change,使用update检测点击并向rating:change发出ajax post请求以更新评分在数据库中,然后将新评级作为响应发回,然后在ajax controller/action函数中更新星级评分插件,并使用新评级。

我将添加强调主要逻辑的代码,而不是计算平均值。

您的星级评分输入代码如下所示,查看success函数,了解如何调用success的{​​{1}}方法。

update

starrating

echo $formReview->field ( $review , 'rate' , [ 'inputOptions' =>
    [ 'class' => 'form-control' ] ] )->widget ( StarRating::classname () , [
    'pluginOptions' => [
        'step' => 0.50 ,
        'showClear' => true ,
        'showCaption' => true ,
        'filledStar' => '<i class="zmdi zmdi-star"></i>' ,
        'emptyStar' => '<i class="zmdi zmdi-star-outline"></i>' ,
    ] ,
    'pluginEvents' => [
        'rating:change' => "function(event, value, caption){
            $.ajax({
                url:'path/to/update-rating',
                method:'post',
                data:{rate:value},
                dataType:'json',
                success:function(data){
                    $(event.currentTarget).rating('update',data.rating);
                }
            });
        }"
    ]
] )->label ( false );