引言:
问题:
用户点击星标后,如何动态显示新的计算评分(平均评分)?现在举例说明(例如2选民),如果平均评分是4.5并且用户选择例如3.5,那么之后它显示 3.5 而不是4.如果更新页面比它好,它显示 4 。如何应用"插件方法"?,例如'更新'或者'刷新'?
答案 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
提供methods
和rating: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 );