- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
你好,我正在尝试实现这个 symfony 3.4 RatingBundle
https://github.com/blackknight467/StarRatingBundle
我得到了这个结果
css 工作完美,但星星不可点击,所以问题是 js 和星星之间没有链接
这是我的代码#update:我已经把js代码放到了twig页面中,让代码更清晰。
html. Twig
{% extends 'base.html.twig' %}
{% block body %}
<h1>Lists of Posts !</h1>
<div class="album py-5 bg-light">
<div class="container">
<h2>Search A Post !!</h2>
<div class="sidebar-search">
<div class="input-group custom-search-form">
<input type="text" id="search" class="form-control" placeholder="Search here">
</div>
<!-- /input-group -->
</div>
<ul class="nav" id="side-menu">
<li>
<a href="#"> resultats de recherche<span class="fa arrow"></span></a>
<ul class="nav nav-second-level" id="entitiesNav">
</ul>
</li>
</ul><br><br><br><br>
<script type="text/javascript" src="{{ asset('assets/js/jquery-2.2.3.min.js') }}"></script>
<script type="text/javascript">
$(function(){
// $( '.rating' ).click(function() {
// alert(parseInt($(this).find('input').val()));
// });
var labelWasClicked = function labelWasClicked(){
var input = $(this).siblings().filter('input');
if (input.attr('disabled')) {
return;
}
input.val($(this).attr('data-value'));
}
var turnToStar = function turnToStar(){
if ($(this).find('input').attr('disabled')) {
return;
}
var labels = $(this).find('div');
labels.removeClass();
labels.addClass('star');
}
var turnStarBack = function turnStarBack(){
var rating = parseInt($(this).find('input').val());
if (rating > 0) {
var selectedStar = $(this).children().filter('#rating_star_'+rating)
var prevLabels = $(selectedStar).nextAll();
prevLabels.removeClass();
prevLabels.addClass('star-full');
selectedStar.removeClass();
selectedStar.addClass('star-full');
}
}
$('.star, .rating-well').click(labelWasClicked);
$('.rating-well').each(turnStarBack);
$('.rating-well').hover(turnToStar,turnStarBack);
});
jQuery(document).ready(function() {
var searchRequest = null;
$("#search").keyup(function() {
var minlength = 1;
var that = this;
var value = $(this).val();
var entitySelector = $("#entitiesNav").html('');
if (value.length >= minlength ) {
if (searchRequest != null)
searchRequest.abort();
searchRequest = $.ajax({
type: "GET",
url: "{{ path('ajax_search') }}",
data: {
'q' : value
},
dataType: "text",
success: function(msg){
//we need to check if the value is the same
if (value===$(that).val()) {
var result = JSON.parse(msg);
$.each(result, function(key, arr) {
$.each(arr, function(id, value) {
if (key === 'posts') {
if (id !== 'error') {
console.log(value[1]);
entitySelector.append('<li><b>'+value[1]+'</b><a href="/pidev_symfony-officiel/web/app_dev.php/livre/detailedlivre/'+id+'">'+'<img src="/pidev_symfony-officiel/web/livimages/'+value[0]+'" style="width: 50px; height: 70px"/>'+'</a></li>');
} else {
entitySelector.append('<li class="errorLi">'+value+'</li>');
}
}
});
});
}
}
});
}
});
});
</script>
<div class="post-container">
<div class="row">
<section class="featured section-padding">
<div class="container">
<div class="row">
<div class="col-12 text-center">
<div class="heading">
<h1 class="section-title">Livres</h1>
<br>
</div>
</div>
{% for livre in livres %}
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4">
<div class="featured-box">
<figure>
{#<img class="img-fluid" src="{{ asset('livimages/' ~ livre.image) }}" style="width: 270px;height: 350px">#}
<a href="{{ path('detailed_livre',{'id': livre.idLivre}) }}"><img class="img-fluid" src="{{ asset('livimages/' ~ livre.image) }}" style="width: 50px; height: 70px" alt=""></a>
</figure>
<div class="content-wrapper">
<div class="feature-content">
<h4>{{ livre.libelle }}</a></h4>
<p class="listing-tagline">{{ livre.description|trim }}</p>
<p class="rating">{{ '4'|rating }}</p>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
</section>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascripts %}
{% endblock %}
PHP:
<?php
namespace Esprit\LoisirBundle\Form;
use blackknight467\StarRatingBundle\Form\RatingType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LivreType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('libelle')
->add('description' ,TextareaType::class)
->add('file')
->add('auteur')
->add('url')
->add('type', ChoiceType::class, array('label' => 'Type',
'choices' => array(' PDF' => 'pdf',
'Audio' => 'audio'),
'required' => true,))
->add('categorie', ChoiceType::class, array('label' => 'Categorie',
'choices' => array(
' Historique' => 'historique',
' Biographique' => 'biographique',
' Politique' => 'politique',
' Voyages' => 'Voyages',
'Jeunesse' => 'Jeunesse'),
'required' => true,))
->add('rating', RatingType::class, [
'label' => 'Rating'
]);
}/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Esprit\LoisirBundle\Entity\Livre'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'esprit_loisirbundle_livre';
}
}
Controller .php
<?php
namespace Esprit\LoisirBundle\Controller;
use Esprit\LoisirBundle\Entity\Livre;
use Esprit\LoisirBundle\Entity\Utilisateur;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Livre controller.
*
*/
class LivreController extends Controller
{
/**
* Lists all livre entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
//$livres = $em->getRepository('EspritLoisirBundle:Livre')->findAll();
$livres = $em->getRepository('EspritLoisirBundle:Livre')->findBy([], ['idLivre' => 'DESC']);
return $this->render('livre/index.html.twig', array('livres' => $livres));
}
/**
* Creates a new livre entity.
*
*/
public function newAction(Request $request)
{
/* ====== sesssion try */
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$user1=new Utilisateur();
$user1= $this->getDoctrine()->getRepository(Utilisateur::class) ->find($user->getId());
/* ====== sesssion try */
$livre = new Livre();
$form = $this->createForm('Esprit\LoisirBundle\Form\LivreType', $livre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/* ===== session */
$livre->setIdUtilisateur($user1);
/* ===== session */
$em = $this->getDoctrine()->getManager();
/* =====Image Up====== */
$livre -> uploadPicture();
/* =====Image Up====== */
$em->persist($livre);
$em->flush();
return $this->redirectToRoute('livre_show', array('idLivre' => $livre->getIdlivre()));
}
return $this->render('livre/new.html.twig', array(
'livre' => $livre,
'form' => $form->createView(),
));
}
/**
* Finds and displays a livre entity.
*
*/
public function showAction(Livre $livre)
{
$deleteForm = $this->createDeleteForm($livre);
return $this->render('livre/show.html.twig', array(
'livre' => $livre,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing livre entity.
*
*/
public function editAction(Request $request, Livre $livre)
{
$deleteForm = $this->createDeleteForm($livre);
$editForm = $this->createForm('Esprit\LoisirBundle\Form\LivreType', $livre);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('livre_edit', array('idLivre' => $livre->getIdlivre()));
}
return $this->render('livre/edit.html.twig', array(
'livre' => $livre,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a livre entity.
*
*/
public function deleteAction(Request $request, Livre $livre)
{
$form = $this->createDeleteForm($livre);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($livre);
$em->flush();
}
return $this->redirectToRoute('livre_index');
}
/**
* Creates a form to delete a livre entity.
*
* @param Livre $livre The livre entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(Livre $livre)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('livre_delete', array('idLivre' => $livre->getIdlivre())))
->setMethod('DELETE')
->getForm()
;
}
public function showdetailedAction($id)
{
$em= $this->getDoctrine()->getManager();
$liv=$em->getRepository('EspritLoisirBundle:Livre')->find($id);
return $this->render('@EspritLoisir/Livre/detailedpost.html.twig', array(
'libelle'=>$liv->getLibelle(),
'description'=>$liv->getDescription(),
'image'=>$liv->getImage(),
'auteur'=>$liv->getAuteur(),
'url'=>$liv->getUrl(),
'type'=>$liv->getType(),
'categorie'=>$liv->getCategorie(),
//'posts'=>$liv,
'comments'=>$liv,
'idLivre'=>$liv->getIdLivre()
));
}
public function listlivreAction(Request $request)
{
$em=$this->getDoctrine()->getManager();
$livres=$em->getRepository('EspritLoisirBundle:Livre')->findAll();
return $this->render('livre/list.html.twig', array(
"livres" =>$livres
));
}
public function searchAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$requestString = $request->get('q');
$livres = $em->getRepository('EspritLoisirBundle:Livre')->findEntitiesByString($requestString);
if(!$livres) {
$result['posts']['error'] = "0 books given ";
} else {
$result['posts'] = $this->getRealEntities($livres);
}
return new Response(json_encode($result));
}
public function getRealEntities($livres){
foreach ($livres as $livres){
$realEntities[$livres->getIdLivre()] = [$livres->getImage(),$livres->getLibelle()];
}
return $realEntities;
}
}
样式:
https://github.com/blackknight467/StarRatingBundle/blob/master/Resources/public/css/rating.css
js :
https://github.com/blackknight467/StarRatingBundle/blob/master/Resources/public/js/rating.js
我的主要问题是如何在html页面中调用它
$('.star, .rating-well').click(labelWasClicked);
$('.rating-well').each(turnStarBack);
$('.rating-well').hover(turnToStar,turnStarBack);
按要求在 new.html.twig 中测试:
{% extends 'base.html.twig' %}
{% form_theme form 'bootstrap_4_layout.html.twig' %}
{% block body %}
<link rel="stylesheet" type="text/css" href="{{ asset('assets/public/css/rating.css') }}" />
<script src="{{ asset('assets/public/js/rating.js') }}"></script>
<h1>Livre creation</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Create" class="btn btn-primary" />
{{ form_end(form) }}
{{ '2'|rating }}
<ul>
<li>
<a href="{{ path('livre_index') }}">Back to the list</a>
</li>
</ul>
{% endblock %}
最佳答案
如果我理解 library如果工作正常,那么我认为 rating
twig 过滤器仅用于显示评级。
为了实际评分,您需要将 RatingType
放在一个表单中(就像您所做的那样)显示该表单,然后提交并保存结果。
在您提供的 twig 文件中,您似乎在遍历 Livre
(我假设)实体的集合,在这种情况下,您只能查看 livre 的评级。现在我看到你在 {{ '4'|rating }}
中有一个硬编码的 4
,但我想在某些时候它应该变成 {{ livre .rating|rating }}
(假设您将在 rating
列下的 Livre
实体中保留评分)以反射(reflect)实际评分。
您添加到 twig 文件中的 javascript 代码放错了地方。就像我提到的那样,您似乎处于“查看”环境中。要实际使用此脚本给出评级,您需要在显示 LivreType
表单的页面上(因为它是包含 RatingType
字段的页面)。如果可以,请粘贴包含 LivreType
表单的 Twig 文件。
javascript 代码也是不必要的,因为您应该能够使用 asset
twig 函数从包本身导入脚本(这是您粘贴到 twig 文件中的内容)。它应该,根据 the readme file ,看起来像这样:
<script src="{{ asset('bundles/starrating/js/rating.js') }}"></script>
如果你想在查看 Livre
实体的集合时能够给出评分,恐怕你需要改变整个方法,有很多方法可以实现这一点,例如显示 RatingType
表单而不仅仅是评分。
另一个注意事项 - 我不确定您使用的库是否按预期工作。我自己没试过,但是来自
what I can see ,似乎评级的实际值不正确。在 5 星系统(默认)和点击第 4 星的情况下,我链接的代码将给出 2 的值,因为在
data-value="{{ stars - star + 1 }}"
stars
是
5
而
star
是
4
。
其实那里的代码是对的,只是stars的div元素是按降序添加的。
关于jquery 不能从外部 symfony 包工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524932/
我需要为打开的 xlsx 文件取消隐藏工作表 TAB,为此,我使用 VBS 文件打开 xlsm 文件并激活宏(位于模块中)。 当我手动运行宏时,它可以工作。 当它通过vbs激活时,它只能看到包含宏的x
我正在使用 Google Cloud Compute Engine 安装气流并使其保持正常运行。安装很好,现在它在主机上运行:0.0.0.0:8080 我有此 VM 实例的外部 IP 地址,但是我无法
我们可以在 GWT 中使用这个 $entry 方法来允许外部 javascript 执行 java 方法。 你可以在他们的文档 https://developers.google.com/web-to
在 Cython 的“Hello World”和 C 数学库中调用函数的示例之后 here ,我真正想做的是将我自己的 C 代码放在一个单独的文件中,并在 Cython 中使用它。关注 this ,我
我一直在试验 JSON Pointers引用和重用 JSON schemas . 按照示例,我能够引用在另一个 JSON 模式中声明的特定属性,一切都按预期进行,但是我还没有找到一种方法来扩展基本 J
我正在使用 X.jar 并添加到我的 AspectJ 项目(在 eclipse 中)。我已经为 X.jar 中的 myMethod() 方法编写了切入点和建议。 但是aspectj 并没有拦截这个方法
我正在 Controller 中创建一个自定义指令,并在 ng-repeat 中调用它,如下所示: HTML: JS: 在测试指令中,我按如下方式调用 loadDat
我正在尝试加载服务器上本地存在的 HTML 页面,位于名为 HTML-FIles 的文件夹中。 我想使用 jquery 加载一个文件并将其内容显示在 div 中。 现在,我可以加载文件,但在 div
我正在尝试根据初始选择从 JSON 文件生成选择菜单。我见过很多不同的方式,人们为此编写了一个函数,但想要一些非常简单的东西。 HTML: Please select Practis
我的目标是从 HTML 文档中获取文本,该文档不会调用 .jsp 文件中的任何函数。 我环顾四周,我以为我已经找到了问题的答案,但它似乎不起作用,其他答案包括使用 jQuery(我既不熟悉也不允许使用
我正在尝试从外部 JSON 文件获取文件内容,但我一直在警报中收到 null。 JS: function getText() { var result = null; var file
我正在加载一个外部 javascript 文件,该文件仅填充有 int 或字符串或 bool 值或数组的变量。类似... varBool=false; var1="var1"; var2="var2:
我的数据存储在外部 Javascript 文件中。 看起来像这样, window.videos = [{ "name": "Sample data", "duration": 154,
我有一个包含 Google ADWords 的 HTML 页面,以及来自外部 URL 的 ajax 调用,我想获取 json 来自 url 的数据。外部API也是我做的。API Controller
我试图看看是否有一种简单的方法可以通过外部 JavaScript 函数访问 Controller 的内部范围(与目标 Controller 完全无关) 我在这里看到了其他几个问题 angular.el
我尝试在运行外部命令时终止脚本,结果出现错误。考虑这个简单的代码: try { where.exe Test-App } catch { Write-Error "Exception
我在 test.js 中定义了一个外部 JS 函数 function InvokeSupport(ID, TimeStamp, Hash) { var sUrl = '' + "?uid="
如果我想将变量从外部 js 文件提取到另一个外部 js 文件。我该怎么做? 例如,如果我有一个名为 example1.js 的文件,其中包含以下代码 var test = 1; 如何获取变量 tes
我正在尝试使用 java 从外部 jar 中读取文件..例如,我有两个 jar 文件。一个是“foo.jar”,另一个是“bar.jar”。 “bar.jar”内部是文件“foo-bar.txt”。如
在我的 Java 应用程序中,我希望从未实际加载的类文件以及也未加载的 jar 文件中读取字节码内容。理想情况下,我需要能够获取任何给定的 jarfile,并找到其中的所有类。因此,考虑以下情况: 我
我是一名优秀的程序员,十分优秀!