I am making a simple Vue Quiz application to learn Vue, and I am having some issues I am hoping a code review can help resolve:
with the quiz radio selections, you can select the correct answer but you cannot select each wrong answer it only selects the last one.
I am also trying to figure out how to add the wrong and right roles if the answered were answered, and unanswered if it was unanswered.
the score changes after hitting submit which is not desired.
https://jsfiddle.net/Jamescodes/cgptvn6r/823/
<div id="app">
<header>
<h1>Quiz Application</h1>
</header>
<div class="container">
<h2>{{ quiz.title }}</h2>
<div class="results" v-if="quizComplete">
<h3>Results</h3>
<p>You got {{ score() }} / {{ quiz.questions.length }} questions correct</p>
<span class="percentage">{{ score() }}0 %</span>
</div>
<div class="card" v-for="(question, index) in quiz.questions">
<h4>{{ index + 1 }}. {{ question.text }}</h4>
<code v-html="question.code" v-if="question.code">{{ question.code }} </code>
<ul>
<li v-for="response in question.responses">
<label>
<input #click="questionIndex++;" type="radio" v-bind:value="response.correct" v-bind:name="index" v-model="userResponses[index]"> {{response.text}}
</label>
</li>
</ul>
</div>
<button #click="submit(); quizComplete = true;">Submit</button>
<div class="answerAll">
<p>Answer all questions before submitting. Unanswered questions are displayed in yellow.</p>
</div>
</div>
</div>
// Create a quiz object with a title and four questions.
// A question has one or more answer, and one or more is valid.
var quiz = {
title: 'Quiz 1 - HTML / CSS / JS Practice',
questions: [
{
text: "What is the purpose of HTML and CSS",
responses: [
{text: 'HTML handles the data, CSS handles routing.'},
{text: 'HTML tells the browser what to display, css tell it what style to dispaly it in', correct: true},
{text: 'HTML styles the page ,css make sit interactive.'},
{text: 'HTML makes the page interactive, CSS styles the page.'},
]
}, {
text: "Which of these html tags contains correct syntax?",
responses: [
{text: 'A'},
{text: 'B'},
{text: 'C',correct: true},
]
}, {
text: "What is the output of the following code:",
code:
`<pre>var answer = 5;
for(var i = 0; i < 10; i+=2){
answer+- i;
}
console.log(answer) </pre>`,
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "Question 4",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "Question 5",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "Question 6",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "Question 7",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "Question 8",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "What is the convention for variable naming in javascript",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}, {
text: "What is the difference between == and === in javascript?",
responses: [
{text: 'Right answer', correct: true},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
{text: 'Wrong answer'},
]
}
]
};
new Vue({
el: '#app',
data: {
quiz: quiz,
questionIndex: 0,
// quiz complete
quizComplete: false,
// An array initialized with "false" values for each question
// It means: "did the user answered correctly to the question n?" "no".
userResponses: Array(quiz.questions.length).fill(false)
},
// The view will trigger these methods on click
methods: {
// Return "true" count in userResponses
score: function() {
return this.userResponses.filter(function(val) { return val }).length;
},
submit: function() {
quizComplete = true;
}
}
});
Related
I have a table with these headers:
headersfav: [
{text: 'Datum', value: 'createdate'},
{text: 'Nachname', value: 'lastname'},
{text: 'Vorname', value: 'firstname'},
{text: 'Adresse', value: 'address', sortable: true},
{text: 'Status', value: 'status'},
],
I use it in a v-data-table component like this:
<v-data-table
:headers="headersfav"
:items="itemsfav"
:search="searchfav"
:custom-filter="customSearch"
:sort-by="['createdate']"
multi-sort
:sort-desc="[true]"
>
For the address field, I use this:
<template #item.address="{ item }">{{ item.street }}, {{ item.city }}</template>
When I tried to sort it, nothing happens, must I use here a custom-sort function? And how can I make it?
Since the property address seems to be an object, you have to point to the actual property upon it needs to be sorted, this is done in the header definitions in the value property:
In this case it could be by address.street or address.city
headersfav: [
{text: 'Datum', value: 'createdate'},
{text: 'Nachname', value: 'lastname'},
{text: 'Vorname', value: 'firstname'},
{text: 'Adresse', value: 'address.street', sortable: true},
{text: 'Status', value: 'status'},
],
I want to create a data table that has a column that contains a sparkline for each item in the data table. Is that possible?
Here's a mock-up of what I'm aiming for:
You have the option to use the item.name slot. Create a separate column for chart, then use its slot to put the sparkline:
headers: [
{
text: "Dessert (100g serving)",
align: "start",
sortable: false,
value: "name",
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" },
{ text: "Carbs (g)", value: "carbs" },
{ text: "Protein (g)", value: "protein" },
{ text: "Iron (%)", value: "iron" },
{ text: "Chart", value: "chart" },
],
<template v-slot:item.chart={item}>
<v-sparkline
:value="value"
:gradient="gradient"
:smooth="radius || false"
:padding="padding"
:line-width="width"
:stroke-linecap="lineCap"
:gradient-direction="gradientDirection"
:fill="fill"
:type="type"
:auto-line-width="autoLineWidth"
auto-draw
></v-sparkline>
</template>
Here is the full example: https://codesandbox.io/s/white-hill-spwzs?file=/src/components/HelloWorld.vue:1174-1618
I have below code
<a-table :dataSource="data" :columns="columns" :pagination="false">
<span slot="rate" slot-scope="rate">
<b-form-input type='number' v-model="rate"></b-form-input>
</span>
</a-table>
Data is set as below
data() {
return {
columns: [
{
title: "Data 1 ",
dataIndex: "group",
},
{
title: "Data 2",
dataIndex: "rate",
scopedSlots: { customRender: "rate" },
}
],
};
},
How I need to update the value of rate. How can i get the value in my handle submit function?
I am having one select field and want to handle select field picker events manually. Following code for selectfield :
{
xtype: 'selectfield',
label: 'Choose one',
name:'abcd',
usePicker:true,
options: [
{text: 'First Option', value: 'first'},
{text: 'Second Option', value: 'second'},
{text: 'Third Option', value: 'third'}
],
defaultPhonePickerConfig: {
hideOnMaskTap: true,
listeners: {
change: function(ths, val) {
console.log('change event called');
},
pick: function(ths, The, slot) {
console.log('pick event called');
PICKER_CONFIG = null;
if (PICKER_CONFIG != true) {
if (The[slot.getName()] != undefined && The[slot.getName()] != null && The[slot.getName()] != "") {
// Ext.getCmp('contractList').setValue(The[slot.getName()]);
ths.fireEvent('change', ths, The);
ths.hide();
}
}
},
cancel: function() {
console.log('cancel called');
PICKER_CONFIG = true;
},
show:function(){
console.log('show called');
}
}
},
pick is not getting called because i am using base css .
Here is my app.scss
#import 'sencha-touch/base';
But its working if i am using sencha-touch default and default/all css
like:
#import 'sencha-touch/default';
#import 'sencha-touch/default/all;
but, i don't want to use it
Is there any way to get that pick by using sencha-touch/base css.
hmm...
Can you tell me from where you get "pick" event ?
And what it supposed to do ?
I'm guessing that its doing the same thing as "change".
And how do you know its because you are using base css ?
from what I read the base css is sencha will be using bare minimum styling for components to work and the rest will be up to user. so there should be no effect on event components behaviour.
This is what i wrote to set Picker in SelectField:
{
xtype: 'selectfield',
usePicker: true,
displayField: 'text',
valueField: 'value',
options: [
{ text: 'Choose Dosage', value: 'default' },
],
defaultPhonePickerConfig : {
listeners: {
change: function(thePicker, newValue, oldValue) {
//check if custom variable has been set to false
if (newValue.slotsNumeric != null && newValue.slotsDecimal != null) {
var total = newValue.slotsNumeric + newValue.slotsDecimal;
// set the select field to show the correct selected value!!
var DecimalPicker = Ext.ComponentQuery.query('selectfield')[0];
DecimalPicker.setOptions({
text: total,
value: total
});
}
},
},
useTitles: true,
hideOnMaskTap: true,
slots: [
{
name : 'slotsNumeric',
title : 'Numeric',
align: 'center',
data : [
{text: '0', value: 0},
{text: '1', value: 1},
{text: '2', value: 2},
{text: '3', value: 3},
{text: '4', value: 4},
{text: '5', value: 5},
{text: '6', value: 6},
{text: '7', value: 7},
{text: '8', value: 8},
{text: '9', value: 9},
]
},
{
name : 'slotsDecimal',
title : 'Decimal',
align: 'center',
data : [
{text: '.0', value: .0},
{text: '.1', value: .1},
{text: '.2', value: .2},
{text: '.3', value: .3},
{text: '.4', value: .4},
{text: '.5', value: .5},
{text: '.6', value: .6},
{text: '.7', value: .7},
{text: '.8', value: .8},
{text: '.9', value: .9},
]
}
]
}
}
ExtJS4
I'm trying to move a treenode to gridpanel. I want its associated data to be inserted here. But whenever I move the treenode, it creates an empty row at that position.
I've created the tree as:
var treeData = {
text: 'Business Processes',
children:[
{text: 'Process7', lob: 'Lob7', leaf:true},
{text: 'Process8', lob: 'Lob8', leaf:true},
{text: 'Process9', lob: 'Lob9', leaf:true},
{text: 'Process10', lob: 'Lob10', leaf:true},
{text: 'Process11', lob: 'Lob11', leaf:true},
{text: 'Process12', lob: 'Lob12', leaf:true}
]
};
bpTreeStore = Ext.create('Ext.data.TreeStore',{});
bpTreeStore.setRootNode(treeData);
new Ext.TreePanel({
id:'businessProcessTree',
store : bpTreeStore,
viewConfig: {
plugins:{
ptype: 'treeviewdragdrop',
dropGroup: 'dragGroup',
dragGroup: 'dragGroup',
dragText: 'Place the node to grid'
}
}
});
And the grid as
Ext.define('BusinessProcessStoreModel',{
extend:'Ext.data.Model',
fields: [
{ name:'businessProcessName', type:'string' },
{ name:'businessProcessLob', type:'string' }
]
});
bpMappingStore = Ext.create('Ext.data.ArrayStore', {
model:'BusinessProcessStoreModel',
data:[
['Process1', 'Lob1'],
['Process2', 'Lob2'],
['Process3', 'Lob3']
]
});
var bpMappingGrid = Ext.create('Ext.grid.Panel',{
id:'bpGridPanel',
region:'center',
frame:true,
layout:'fit',
height:300,
columns:[
{ header:'Process Name', dataIndex:'businessProcessName' },
{ header:'Process Lob', dataIndex:'businessProcessLob' }
],
store:bpMappingStore,
viewConfig: {
plugins:{
ptype: 'gridviewdragdrop',
dropGroup: 'dragGroup',
dragGroup: 'dragGroup',
dragText: 'Place the node to grid'
},
listeners: {
drop: function(node, data, overModel, dropPosition)
{
var position = (dropPosition=='after'?overModel.index + 1: overModel.index -1);
Ext.getCmp('bpGridPanel').store.insert(position, [[data.records[0].data.text, 'LOB']]);
//data.records[0].data.lob is undefined don't know why
}
}
}
});
Please tell me how I refer to the 'text' and 'lob' of the treenode to be inserted into the two columns of the gridpanel.
Here I've created my own
I solved the problem. Make sure that the Treenode being dragged must contain the attributes in the model which the grid is using.
Here, treedata can be defined as
var treeData = {
text: 'Business Processes',
children:[
{text: 'Process7', businessProcessName: 'Process7', businessProcessLob: 'Lob7', leaf:true},
{text: 'Process8', businessProcessName: 'Process8', businessProcessLob: 'Lob8', leaf:true},
{text: 'Process9', businessProcessName: 'Process9', businessProcessLob: 'Lob9', leaf:true},
{text: 'Process10',businessProcessName: 'Process10', businessProcessLob: 'Lob10', leaf:true},
{text: 'Process11', businessProcessName: 'Process11', businessProcessLob: 'Lob11', leaf:true},
{text: 'Process12', businessProcessName: 'Process12', businessProcessLob: 'Lob12', leaf:true}
]
};
this.bpTreeStore = Ext.create('Ext.data.TreeStore',{root: this.treeData});
But when we assign this treedata object to the treestore. It removes the new attributes (businessProcessName and businessProcessLob).
So to add them, a loop can be run.
for(var index in treeData.children)
{
this.bpTreeStore.tree.root.childNodes[index].data.businessProcessName = treeData.children[index].businessProcessName;
this.bpTreeStore.tree.root.childNodes[index].data.businessProcessLob = treeData.children[index].businessProcessLob;
}
After this, dragging works fine :)