简体   繁体   English

Opencart定制:用于评级的服务器端脚本

[英]Opencart customization: server-side script for rating

I've added a custom table into the Opencart database, where I have a field/column, called average_rating (value = null to 5 ). 我已经在Opencart数据库中添加了一个自定义表,其中有一个字段/列,称为average_rating (值= null5 )。

In my (custom) template (.tpl file) I've added a code to get and show the rating of current record from database. 在我的(自定义)模板(.tpl文件)中,我添加了代码以获取和显示数据库中当前记录的等级。

Here is the code within .tpl file: 这是.tpl文件中的代码:

<div class="form-group">
  <label class="col-sm-2 control-label" for="input-average_rating"><?php echo $entry_average_rating; ?></label>
    <div class="col-sm-10">
      <input type="hidden" name="average_rating" value="<?php echo $average_rating; ?>" id="input-average_rating" />

        <?php for ($i = 0; $i < $average_rating; $i++) { ?>
          <div class="rating_hover" id="<?php echo 'r' . ($i+1) ?>" title="<?php echo $i+1 ?>" data-toggle="tooltip"><i class="fa fa-star"></i></div>
        <?php } ?>

        <?php for ($i = $average_rating; $i <= 4; $i++) { ?>
          <div class="rating_normal" id="<?php echo 'r' . ($i+1) ?>" title="<?php echo $i+1 ?>" data-toggle="tooltip"><i class="fa fa-star"></i></div>
        <?php } ?>

    </div>
</div>

For the blue-stars, I use .rating_hover class, for the grey-ones: .rating_normal class (see the picture below). 对于蓝星,我使用.rating_hover类,对于灰色代表: .rating_normal类(请参见下图)。

平均评分:自定义Opencart编辑表

All this stuff works fine. 所有这些东西工作正常。 But now I want to do something I have no experience with and I would appreciate any tip concerning my question. 但是,现在我想做一些我没有经验的事情,对于我提出的问题,我将不胜感激。

Question: When a mouse pointer is over a grey star, it must become blue, like the ones before it. 问题:当鼠标指针悬停在灰色星星上时,它必须变成蓝色,就像之前的一样。 And when clicked on a star, my hidden input must get the value of title attribute of the clicked div-element. 当单击星号时,我的隐藏输入必须获取所单击的div元素的title属性的值。 I wouldn't like to write a client-side Javascript to do this. 我不想编写客户端Javascript来做到这一点。 Could somebody give a tip on how to do this with JSON or AJAX... or somehow please? 有人可以提供有关如何使用JSON或AJAX进行操作的提示吗?

I mean: something like this: 我的意思是:像这样:

$('div[id=\'r*\']').onmouseover({
    // for (i=$average_rating; i<=[current_id]; i++) {
    // ??? document.getElementById('r[i]').style.ClassName = 'someclass';
});

Javascript-alternative works fine, but I still have problems with JSON-script: This is how javascript works: Inside every div-element I added following commands: Javascript替代方法可以正常工作,但JSON脚本仍然存在问题:这是JavaScript的工作原理:在每个div元素内,我添加了以下命令:

 <div ... onclick="rOnClick(<?php echo ($i+1) ?>);" onmouseover="rOnMouseOver(<?php echo ($i+1) ?>);" onmouseout="rOnMouseOut(<?php echo ($i+1) ?>);" ... >

And my Javascript functions are now, as follows: 现在,我的Javascript函数如下:

  <script type="text/javascript">
        function rOnMouseOver(id) {
    var ar = parseInt(document.getElementById('input-average_rating').value);

    if (isNaN(ar)) {
        ar = 0;
        }

    for(i = (ar+1); i <= id; i++) {
        document.getElementById('r' + i).className = 'rating_hover';
        }
    }

function rOnMouseOut(id) {
    var ar = parseInt(document.getElementById('input-average_rating').value);

    if (isNaN(ar)) {
        ar = 0;
        }

    for(i = 1; i <= ar; i++) {
        document.getElementById('r' + i).className = 'rating_hover';
        }

    for(i = (ar+1); i <= id; i++) {
        document.getElementById('r' + i).className = 'rating_normal';
        }
    }

function rOnClick(id) {
    document.getElementById('input-average_rating').value = id;

    for(i = 1; i <= id; i++) {
        document.getElementById('r' + i).className = 'rating_hover';
        }

    for(i = (id+1); i <= 5; i++) {
        document.getElementById('r' + i).className = 'rating_normal';
        }
    }
  </script>

Please add another css class 'rating' in all rating divs. 请在所有评分div中添加另一个CSS类“ rating”。 Also you will be needed to add a different class 'rated' for existing/clicked rated value. 另外,您还需要为现有/单击的额定值添加一个不同的“额定”类别。 Then add following script: 然后添加以下脚本:

$('.rating').hover(
     // Handles the mouseover
  function() {
    $(this).prevAll().andSelf().addClass('rating_over');
    $(this).nextAll().removeClass('rating_normal'); 
   },
    // Handles the mouseout
   function() {
     $(this).prevAll().andSelf().removeClass('ratings_over');
     $('.rated').addClass('ratings_over'); // back to rated one

   }
);



$('.rating').bind('click', function() {
   $('.rating').removeClass('rated');
   $(this).addClass('rated');
   $('#input-average_rating').val($(this).attr('title'));

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM