How to get count on v-if list with variable? - vuejs2

I want to show one list based one variable, but if I show {{ i }} represents entire count, how I get count only the current list?
Thats my code:
<tr v-for="(item, i) in placares" v-if="item.dificuldade == dificuldadeAtual.nome">
<td>{{ i }}</td>
<td>{{ item.nome }}</td>
<td>{{ item.fase }}</td>
<td>{{ item.pontos | formatNumber }}</td>
</tr>
The app is running here: http://digitacao.serraonline.net.br/

Your question is not so clear but i think this will help you:
<tr v-for="(item, index) in placares">
<div v-if="item.dificuldade == dificuldadeAtual.nome">
<td>{{ index }}</td>
<td>{{ item.nome }}</td>
<td>{{ item.fase }}</td>
<td>{{ item.pontos | formatNumber }}</td>
</div>
</tr>
Don't use v-for and v-if in same element, v-for will get priority.

It's not semantic, i solved the problem using one method to filter the list
methods:{
filterPlacar(dificuldade){
return _.take(_.filter(this.placares, item => {
return item.dificuldade.indexOf(dificuldade) >=0;
}),15);
}
}
And the v-for:
<tr v-for="(item, j) in filterPlacar(dificuldadeAtual.nome)">
<td>{{ ++j }}</td>
<td>{{ item.nome }}</td>
<td>{{ item.fase }}</td>
<td>{{ item.pontos | formatNumber }}</td>
</tr>

You can replace v-if with v-show like:
<tr v-for="(item, i) in placares" v-show="item.dificuldade == dificuldadeAtual.nome">
<td>{{ i }}</td>
<td>{{ item.nome }}</td>
<td>{{ item.fase }}</td>
<td>{{ item.pontos | formatNumber }}</td>
</tr>
or use v-if in your tds like:
<tr v-for="(item, i) in placares">
<template v-if="item.dificuldade == dificuldadeAtual.nome">
<td>{{ i }}</td>
<td>{{ item.nome }}</td>
<td>{{ item.fase }}</td>
<td>{{ item.pontos | formatNumber }}</td>
</template>
</tr>

Related

Why Nuxt 3 Not Updating Realtime Upon Data Change on Props?

i have this problem in which the HTML page is not updating upon props variables change. Please help me.
I make a table in my child element based on the array passed by the parent, here is the child code:
<table>
<thead>
<th>Rank</th>
<th>Nama</th>
<th>Blank</th>
<th>Error</th>
<th>Warning</th>
<th>Clean</th>
<th>Total</th>
</thead>
<tbody>
<tr v-for="(datae, index) in props.ranking" :key="index">
<td>{{ index }}</td>
<td>{{ '(' + datae.username + ') ' + datae.realname }}</td>
<td>{{ datae.blank }}</td>
<td>{{ datae.error }}</td>
<td>{{ datae.warning }}</td>
<td>{{ datae.clean }}</td>
<td>{{ datae.total }}</td>
</tr>
</tbody>
</table>
Here is the child script:
<script setup lang="ts">
var props = defineProps({
ranking: {
type: Array,
required: false,
}
})
watch(() => props.ranking, (newValue, oldValue) => console.log('tes'))
</script>
I am expecting the html table update upon props.ranking change
I noticed a couple of issues here. It might help you.
First of all, you used the props keyword in the template, it's not needed.
<!-- props.ranking -> ranking -->
<tr v-for="(datae, index) in ranking" :key="index">
<td>{{ index }}</td>
<td>{{ '(' + datae.username + ') ' + datae.realname }}</td>
<td>{{ datae.blank }}</td>
<td>{{ datae.error }}</td>
<td>{{ datae.warning }}</td>
<td>{{ datae.clean }}</td>
<td>{{ datae.total }}</td>
</tr>
The second point is that you watch an array prop, but it isn't changed, only the properties (items) of this array will be changed. So your watcher callback never fires. To fix this, you can pass the deep option to watch as a third argument, like this:
watch(() => props.ranking,
(newValue, oldValue) => console.log('tes'),
{ deep: true }
)
Source: watch arrays

Using v-for in a table

I have a table is populated with some info and I would like to format the table like the picture
Unfortunately the excel sheet which I have no control over is formatted so:
I want any row that has only a Equipment type to span whole row. All other rows should appear as normal table row.
I am using following vue template:
<table>
<caption>
SHS Scrap Table
</caption>
<thead>
<tr>
<th>Make</th>
<th>Model #</th>
<th>Bar Code</th>
<th>Serial #</th>
<th>Location</th>
<th>Condition</th>
</tr>
</thead>
<tbody v-for="item in scrapDataEmptyRowsRemoved" :key="item">
<tr v-if="item['Equipment Type']">
<td class="equipt-type" colspan="6">
Equipment Type - {{ item["Equipment Type"] }}
</td>
</tr>
<tr v-else>
<td>{{ item["Make"] }}</td>
<td>{{ item["Model #"] }}</td>
<td>{{ item["Bar Code"] }}</td>
<td>{{ item["Serial #"] }}</td>
<td>{{ item["Location"] }}</td>
<td>{{ item["Condition"] }}</td>
</tr>
</tbody>
</table>
The only problem is that looking in Devtools I see that every row has a Tbody which is not semantically correct. Any idea's on how to correct this. If I use a container around the v-if v-else all formatting breaks down.Thanks...
Update the only problem is Vite is objecting to moving :key attribute to the v-else:
I dont what other unique key they want.
Update II - Ok apparently if I use different object keys Vite is ok with that ie :key="item['Equipment Type'] and on v-else :key="item['Make']. Does that seem correct?
You can move the v-for in a template tag, that won't be rendered in the DOM.
<tbody>
<template v-for="item in scrapDataEmptyRowsRemoved" :key="item">
<tr v-if="item['Equipment Type']">
<td class="equipt-type" colspan="6">
Equipment Type - {{ item["Equipment Type"] }}
</td>
</tr>
<tr v-else>
<td>{{ item["Make"] }}</td>
<td>{{ item["Model #"] }}</td>
<td>{{ item["Bar Code"] }}</td>
<td>{{ item["Serial #"] }}</td>
<td>{{ item["Location"] }}</td>
<td>{{ item["Condition"] }}</td>
</tr>
</template>
</tbody>

Target a specific cell background color in a v-for loop

With vuejs I fill a table with some data with this code :
<tr v-for="droit in listedroit">
<td>{{ droit.id_u }}</td>
<td>{{ droit.role }}</td>
<td>{{ droit.id_e }}</td>
<td>{{ droit.droits }}</td>
<td v-bind:style="{ 'background-color': statusColor }">STATUS</td>
</tr>
statusColor is computed in my app.js and returned to the template.
Some rows need a red cell, others a green one (I check if the rights are RO(green) or RW(red)).
The problem is I failed to target a specific cell of a row, and if I set statusColor to 'red', the whole column is red.
How can I achieve this ?
Thanks a lot for your help.
You can use a method instead of a computed property
<template>
<tr v-for="droit in listedroit">
<td>{{ droit.id_u }}</td>
<td>{{ droit.role }}</td>
<td>{{ droit.id_e }}</td>
<td>{{ droit.droits }}</td>
<td v-bind:style="{ 'background-color': statusColor(droit.rights) }">STATUS</td>
</tr>
</template>
export default{
methods: {
statusColor(rights){
if(rights === 'RO'){
return 'green';
} else {
return 'red';
}
}
}
}

How to add class name using Vue.js to a table column which is part of a Vue.js Template? [duplicate]

This question already has answers here:
Difference between v-bind and {{}}?
(3 answers)
How can I solve "Interpolation inside attributes has been removed. Use v-bind or the colon shorthand"? Vue.js 2
(6 answers)
Closed 5 years ago.
I need to assign class name from my visitor object (visitor.iso_code) to a table column. However Vue.js will not allow me to do it this way. I've tried v-bind:class and :class and neither of them work. Any help would be appreciated. Thank you in advance.
<template lang="html">
<div>
<table id="vue_generated_visitors_table" class="table">
</table>
<tbody>
<tr v-for="visitor in visitors_table">
<th scope="row">{{ visitor.id }}</th>
<td>{{ visitor.created_at }}</td>
<td><img src="images/blank.png" class="{{visitor.iso_code}}" /> {{ visitor.country }}</td>
<td>{{ visitor.city }}</td>
<td>{{ visitor.state_name }}</td>
<td>{{ visitor.postal_code }}</td>
<td>{{ visitor.lat }}</td>
<td>{{ visitor.lon }}</td>
<td>{{ visitor.timezone }}</td>
<td>{{ visitor.currency }}</td>
<td>{{ visitor.userAgent }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script type="text/javascript">
export default {
props: ['visitors_table','method'],
data () {
return {
}
},
methods: {}
}

validate Min option of integer field in Symfony2.1 application

I created my form with this code :
public function creerConferenceAction($id = null) {
$message = '';
if (isset($id)) {
$conference = $this->getDoctrine()
->getRepository('gestionConferenceApplicationBundle:Conference')
->find($id);
if (!$conference) {
throw $this->createNotFoundException('No conference found for id ' . $id);
}
}else{
$conference = new Conference();
}
$form = $this->createFormBuilder($conference)
->add('titre', 'text')
->add('ville', 'text')
->add('lieu', 'text')
->add('date_debut', 'date', array(
'widget' => 'single_text',
'format' => 'yyyy-MM-dd',
))
->add('date_fin', 'date')
->add('nbMin', 'integer')
->add('nbMax', 'integer')
->add('dateLimiteInscription', 'date')
->getForm();
$request = $this->getRequest();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($conference);
$em->flush();
return $this->redirect($this->generateUrl('_acceuil'));
}
}
return $this->render('gestionConferenceApplicationBundle:acceuil:creerConference.html.twig', array(
'form' => $form->createView(),
));
}
and I want to add this constraint : the user cannot enter a value less than 5 in those fields :
->add('nbMin', 'integer')
->add('nbMax', 'integer')
I tested this (I take it from the reference book) :
# src/gestionConference/ApplicationBundle/Resources/config/validation.yml
gestionConference\ApplicationBundle\Entity\Conference:
properties:
age:
- Min: { limit: 18, message: You must be 18 or older to enter. }
but it do nothing
how can I achieve this
thank you in advance
-------------------EDIT-------------------EDIT------------------------------
I read another time the reference and I realized that the min option is removed in the latest version of symfony 2.1.3
and I replaced it by :
# src/gestionConference/ApplicationBundle/Resources/config/validation.yml
gestionConference\ApplicationBundle\Entity\Conference:
properties:
nbmin:
- Range:
min: 120
minMessage: You must be at least 120cm tall to enter
but no change
here is the twig page :
{% extends "gestionConferenceApplicationBundle::layout.html.twig" %}
{% block content %}
<div id="welcome">
<div class="content">
<form action="" method="post" {{ form_enctype(form) }}>
{{ form_errors(form) }}
<table>
<tr>
<td>{{ form_label(form.titre, 'Titre : ') }}</td>
<td>{{ form_widget(form.titre) }}</td>
<td>{{ form_errors(form.titre) }}</td>
</tr>
<tr>
<td>{{ form_label(form.ville, 'Ville : ') }}</td>
<td>{{ form_widget(form.ville) }}</td>
<td>{{ form_errors(form.ville) }}</td>
</tr>
<tr>
<td>{{ form_label(form.lieu, 'Lieu : ') }}</td>
<td>{{ form_widget(form.lieu) }}</td>
<td>{{ form_errors(form.lieu) }}</td>
</tr>
<tr>
<td>{{ form_label(form.date_debut, 'Date de début : ') }}</td>
<td>{{ form_widget(form.date_debut) }}</td>
<td>{{ form_errors(form.date_debut) }}</td>
</tr>
<tr>
<td>{{ form_label(form.date_fin, 'Date de fin : ') }}</td>
<td>{{ form_widget(form.date_fin) }}</td>
<td>{{ form_errors(form.date_fin) }}</td>
</tr>
<tr>
<td>{{ form_label(form.nbMin, 'Nombre minimal de participants : ') }}</td>
<td>{{ form_widget(form.nbMin) }}</td>
<td>{{ form_errors(form.nbMin) }}</td>
</tr>
<tr>
<td>{{ form_label(form.nbMax, 'Nombre maximal de participants : ') }}</td>
<td>{{ form_widget(form.nbMax) }}</td>
<td>{{ form_errors(form.nbMax) }}</td>
</tr>
<tr>
<td>{{ form_label(form.dateLimiteInscription, 'Date limite d inscription : ') }}</td>
<td>{{ form_widget(form.dateLimiteInscription) }}</td>
<td>{{ form_errors(form.dateLimiteInscription) }}</td>
</tr>
{{ form_rest(form) }}
<tr>
<td align="center" colspan=3 >
<input type="submit" style="width: 80px;height: 30px;margin-right: 25px;" value="valider" />
<input type="reset" style="width: 80px;height: 30px;" value="initialiser" />
</td>
</tr>
</table>
</form>
</div>
</div>
{% endblock %}