Knockout MVC Html.RadioButton - asp.net-mvc-3

I am trying to use this example:
<label><input type="checkbox" data-bind="checked: displayMessage" /> Display message</label>
<div data-bind="if: displayMessage">Here is a message. Astonishing.</div>
<script type="text/javascript">
ko.applyBindings({
displayMessage: ko.observable(false)
});
</script>
My question is how can display the message if the following RadioButton is checked.
<%= Html.RadioButton("answer", qm.DynamicQuestions[index].Answer_A, (qm.LearnerAnswers[index] == qm.DynamicQuestions[index].Answer_A))%><%= Html.Encode( qm.DynamicQuestions[index].Answer_A) %>
I would appreciate your suggestions.

I'm guessing that you want something like this
http://jsfiddle.net/gonefishern/ktcY5/
<ul data-bind="foreach: questions">
<li>
<span data-bind="text: description"></span>
<ul data-bind="foreach: availableAnswers">
<li>
<label>
<input type="radio" data-bind="value: value, checked: checked, attr: {name: $parent.id }" />
<span data-bind="text: value"></span>
</label>
</li>
</ul>
</li>
</ul>
var questions = [{
id: 'ID1',
description: ko.observable('Should I answer this?'),
availableAnswers: [{
value: 'Yes',
checked: ko.observable('Yes')
}, {
value: 'No',
checked: ko.observable(false)
}]
},{
id: 'ID2',
description: ko.observable('How about answering this?'),
availableAnswers: [{
value: 'Yes',
checked: ko.observable()
}, {
value: 'No',
checked: ko.observable('No')
}]
}];
var vm = {
questions: questions,
}
ko.applyBindings(vm);

Related

Vue component is mounted but not displaying

my vue component fetches data successfully from an API request, and return as laravel resource, i want to display product card base on the response. even though i can see it in the vue devTools, but its not displaying in my website.
My vue component:
<div class="clearfix visible-sm visible-xs">
<!-- Product Single -->
<div v-for="(product,key) in products" v-bind:key="product_slug" class="col-md-4 col-sm-6 col-xs-6">
<div class="product product-single">
<div class="product-thumb">
<div class="product-label">
<span>New</span>
<span class="sale">-20%</span>
</div>
<a :href="shop" class="main-btn quick-view"><i class="fa fa-search-plus"></i>
View
</a>
<a href='#'><img :src="product.product_image_1" alt=""
class="img-responsive" width="400px"></a>
</div>
<div class="product-body">
<h3 class="product-price">{{ product.price }}
<del class="product-old-price">{{ product.OldPrice }}</del>
</h3>
<div class="product-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o empty"></i>
</div>
<h2 class="product-name">{{ product.slug }}</h2>
<div class="product-btns">
<button type="submit" class="main-btn icon-btn"><i class="fa fa-heart"></i>
</button>
<button class="main-btn icon-btn"><i class="fa fa-exchange"></i>
</button>
<input type="hidden" name="id" id="product_id" :value="product.id">
<input type="hidden" name="name" id="product_name" :value="product.slug">
<input type="hidden" name="NewPrice" id="product_NewPrice" :value="product.price">
<button type="button" id="addCArt" class="primary-btn add-to-cart"> Add to Cart</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
products: [],
product: {
"id": '',
"slug": '',
"product_image_1": '',
"product_image_2": '',
"product_image_3": '',
"product_image_4": '',
"price": '',
"OldPrice": '',
"qty": '',
"stock_status": '',
"sku": '',
"short_description": '',
"description": '',
"product_links" : '',
},
product_slug: '',
pagination: {},
}
},
created() {
this.fetchProduct();
},
methods: {
fetchProduct(page_url) {
//assign variable to this
let vm = this;
// check if page url exist, = page url else = /api/shop
page_url = page_url || 'api/shop';
fetch(page_url)
.then(res => res.json())
.then(res => {
vm.products = res.data;
})
.catch(err => console.log(err));
},
}
}
</script>
the problem now is, after registering the components, it is not displaying, even though it fetches the data successfully, and i can see it in my devTools
EDIT
Silly me... The bootstrap classes visible-sm visible-xs, hide the products on large screens
One problem is when you are trying to bind the key on the v-for loop. The key should be something that is unique to each item in the loop:
https://v2.vuejs.org/v2/guide/list.html#Maintaining-State
Try something like this instead:
<div v-for="(product,key) in products" v-bind:key="product.id" class="col-md-4 col-sm-6 col-xs-6">
Or instead of id you can use any other property that is guaranteed to be unique on each product.
And another issue, like Adam said in the comments, is that you already have a product defined in your data() property, and then you are also calling each item in the loop product. You should assign unique names that don't conflict with each other.

VueJs: How to create a select where options come from a query to other model

I'm new on VueJs and I don't know why I have the following problem:
I'm creating a view called Owners.vue where I show pub owners. In UpdateProfile.vue I show the owner data and here is where I have my problem: I'd like to build a select where the options are the possible pubs stored in my table "pubs":
My vue component is as follows:
UpdateProfile.vue
<template>
<confirm title="Edit User" ok="Save user" :show="show"
v-on:save="save"
v-on:close="close">
<div class="field">
<label class="label">Name</label>
<div class="control">
<input class="input" type="text" placeholder="User name" v-model="data.name">
</div>
</div>
<div class="field">
<label class="label">Lastname</label>
<div class="control">
<input class="input" type="text" placeholder="last name" v-model="data.lastname">
</div>
</div>
<div class="field">
<label class="label">Email</label>
<div class="control">
<input class="input" type="email" placeholder="email" v-model="data.email">
</div>
</div>
<!--Owner Pubs-->
<div class="field">
<label class="label">Pubs</label>
<div v-for="pub in data.userPubsOwned" class="control">
<input class="input" type="text" placeholder="Pub tapps" v-model="pub.name">
<div class="button is-danger" #click="deletePubFromOwner(pub.id)">
<span class="icon"><i class="far fa-trash-alt"></i></span>
<span>Delete</span>
</div>
</div>
<br>
</div>
<!--Owner Pubs-->
<!--Add Pubs to Owner-->
<div class="field">
<label class="label">Add new Pub</label>
<div class="select">
<select v-model="pubs">
<option v-for = "pub in pubs" :value="pub.id" >{{pub.name}}</option>
</select>
</div>
<br>
<br>
<div class="button is-info" #click="addPubToOwner()">
<span class="icon"><i class="fas fa-save fa-lg"></i></span>
<span>Add Tapp</span>
</div>
</div>
<!--Add Pubs to Owner-->
</confirm>
import User from "../../models/user";
export default {
props: {
show: Boolean,
data: Object,
},
data() {
return {
selected: null,
data: new User(),
pubs: [],
pub: new Pub(),
}
},
computed: {
},
methods: {
save() {
this.$emit('save', this.data);
},
close() {
this.$emit('close');
},
hasRootPermissionsAndIsNotRoot() {
return this.CONSTANTS.hasRootPermissions() && this.data.permissions !== this.CONSTANTS.ROOT_USER.permissions;
},
addPubToOwner(){
this.api.post('/owners/' + this.data.id + '/' + this.selected).then(response => {
this.data = response.data;
});
},
deletePubFromOwner(ownerpub) {
this.api.delete('/owners/' + this.data.id + '/' + ownerpub).then(response => {
this.data = response.data;
});
},
}
}
I just need to show all the pubs stored in my table pub...do I have to create a function? And how it would be?
Thanks a lot for your help!!
Yes, create a method in the mounted() section. I use a similar process to show all of the flavors/prices of a product in a shopping cart. Here is my code that you can use and hopefully extrapolate your answer from:
Mounted function to load upon vue mount
mounted: function() {
this.getPrice();
},
getPrice() function:
getPrice: function(){
axios.post('/getproductinfo', this.$data.model)
.then((response) => {
console.log(response);
this.display_name = response.data.display_name;
this.price = '$' + response.data.price;
})
.catch(error => {
this.errors.record(error.response.data.errors);
});
},
And finally the code in your view blade file
<select class="centerSelect" v-show="!loading && ordering" ref="quantitySelect" v-model="model.id" name="code_id" #change="getPrice">
#foreach ($code as $item)
<option value="{{$item->id}}">{{$item->display_name}}</option>
#endforeach
</select>

Kendo TabStrip and Kendo Templates

I have to load different KENDO templates dynamically on each tab click of Tabstrip . The datasource for all tabs are same . I am trying to do it as below but its not working . Please advise.(Kendo TabStrip and Kendo Templates)
<div class="tabs-details-wrapper" id="Tab1">
<ul>
<li class="k-state-active" >Identification</li>
<li>Planning</li>
<li>Implementation</li>
<li>Review</li>
<li>Management Response</li>
</ul>
<div>
<div class="row">
<div class="col-xs-12 col-md-5">
<div class="">
<div class="form-horizontal" ></div>
</div>
</div>
</div>
</div>
</div>
<script id="details-templateTab1" type="text/x-kendo-template">
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Year of Completion</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly"></span>
</div>
</div>
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Title</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly"></span>
</div>
</div>
</script>
<script id="details-templateTab2" type="text/x-kendo-template">
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Hello1</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly">
</span>
</div>
</div>
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Title1</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly">
</span>
</div>
</div>
</script>
$(".tabs-details-wrapper").kendoTabStrip({
select: function (e) {
//var data = this.dataSource.at($(e.item).index());
var templateSelector = ""
// alert(e);
if (e.item.innerText == "Identification") templateSelector = kendo.template($("#details-templateTab1").html())
else templateSelector = kendo.template($("#details-templateTab2").html()) ;
// alert(e.contentElement);
//templateSelector = kendo.template($("#details-templateTab1").html());
$(e.contentElement).html(templateSelector);
// kendo.bind(e.contentElement, data);
// $(e.contentElement).html(template(this.dataSource.at($(e.item.innerText).index())))
},
// template: $("#details-templateTab1"),
animation: false
//dataSource: ds
// dataSource: ds,
});
I have started a jsBin to assist with your issue. When I click each tab it loads content like you desribe. Do you have a datasource javascript object to add to your code?
Need more info, please.
$(".tabs-details-wrapper").kendoTabStrip({
select: function (e) {
//var data = this.dataSource.at($(e.item).index());
var templateSelector = ""
// alert(e);
if (e.item.innerText == "Identification") templateSelector = kendo.template($("#details-templateTab1").html())
else templateSelector = kendo.template($("#details-templateTab2").html()) ;
// alert(e.contentElement);
//templateSelector = kendo.template($("#details-templateTab1").html());
$(e.contentElement).html(templateSelector);
// kendo.bind(e.contentElement, data);
// $(e.contentElement).html(template(this.dataSource.at($(e.item.innerText).index())))
},
// template: $("#details-templateTab1"),
animation: false
//dataSource: ds // what is your DS?
// dataSource: ds,
});
I think the problem is that in your datasource you are not defining the "dataContentField". Once I added this in conjunction with a datasource I started seeing dynamic results from select function.
Here is my JavaScript
$(".tabs-details-wrapper").kendoTabStrip({
select: function (e) {
var templateSelector = ""
if (e.item.innerText == "Identification") templateSelector = kendo.template($("#details-templateTab1").html())
else templateSelector = kendo.template($("#details-templateTab2").html());
$(e.contentElement).html(templateSelector(this.dataSource.at($(e.item).index())))
},
animation: false,
dataTextField: "Name",
dataContentField: "Content",
dataSource: [
{ Name: "Identification", YearOfCompletion: "1998", Title: "This is first title", Content: "", customMessage: "This is custom message 1" },
{ Name: "Planning", YearOfCompletion: "1999", Title: "This is second title", Content: "", customMessage: "This is custom message 2" },
{ Name: "Implementation", YearOfCompletion: "2000", Title: "This is third title", Content: "", customMessage: "This is custom message 3" },
{ Name: "Review", YearOfCompletion: "2001", Title: "This is fourth title", Content: "", customMessage: "This is custom message 4" },
{ Name: "Management Response", YearOfCompletion: "2002", Title: "This is fifth title", Content: "", customMessage: "This is custom message 5" }
]
});
It didn't really matter what i put in the content property of the dataSource because this is getting replaced on select.
Here is my html
<div class="tabs-details-wrapper" id="Tab1"></div>
<script id="details-templateTab1" type="text/x-kendo-template">
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Year of Completion</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly">#=YearOfCompletion#</span>
</div>
</div>
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Title</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly">#=Title#</span>
</div>
</div>
</script>
<script id="details-templateTab2" type="text/x-kendo-template">
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Hello1</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly">#=customMessage#</span>
</div>
</div>
<div class="form-group">
<label class="col-xs-12 col-md-4 control-label">Title1</label>
<div class="col-xs-12 col-md-8">
<span class="control-readonly">#=Title#</span>
</div>
</div>
</script>
Is this what your looking for?

how to create daterange filtering in high chart?

Can anyone can code range date filtering for high chart . I don't know how to code it.my chart fetch data from the database and i want user can choose the date to look the chart.
so here my code:
?php
foreach($chart as $result){
$project[] = $result->project_name;
$value[] = (float) $result->count;
}
?>
<script>
$(function () {
$('#project').highcharts({
chart: {
type: 'column',
margin: 75,
options3d: {
enabled: false,
alpha: 10,
beta: 25,
depth: 70
}
},
title: {
text: 'CHART',
style: {
fontSize: '18px',
fontFamily: 'Verdana, sans-serif'
}
},
subtitle: {
text: 'Project',
style: {
fontSize: '15px',
fontFamily: 'Verdana, sans-serif'
}
},
plotOptions: {
column: {
depth: 25
}
},
credits: {
enabled: false
},
xAxis: {
categories: <?php echo json_encode($project);?>
},
exporting: {
enabled: false
},
yAxis: {
title: {
text: 'Total'
},
},
tooltip: {
formatter: function() {
return 'The value for <b>' + this.x + '</b> is <b>' + Highcharts.numberFormat(this.y,0) + '</b>, in '+ this.series.name;
}
},
series: [{
name: 'Report Data',
data: <?php echo json_encode($value);?>,
shadow : true,
dataLabels: {
enabled: true,
color: '#045396',
align: 'center',
formatter: function() {
return Highcharts.numberFormat(this.y, 0);
}, // one decimal
y: 0, // 10 pixels down from the top
style: {
fontSize: '13px',
fontFamily: 'Verdana, sans-serif'
}
}
}]
});
});
</script>
<div class="form-inline">
<a>FROM</a>
<div class="form-group">
<div class="input-group date date-picker" data-date-format="dd-mm-yyyy">
<input type="text" name="start_date" class="form-control" readonly id="start_date" size="8">
<span class="input-group-btn">
<button class="btn default" type="button">
<i class="fa fa-calendar"></i>
</button>
</span>
</div>
</div>
TO
<div class="form-group">
<div class="input-group date date-picker" data-date-format="dd-mm-yyyy">
<input type="text" name="end_date" class="form-control" readonly id="end_date" size="8">
<span class="input-group-btn">
<button class="btn default" type="button">
<i class="fa fa-calendar"></i>
</button>
</span>
</div>
</div>
<button class="btn btn-primary blue" id="btn-search">Submit</button>
</div>
<div class="row">
<div class="col-md-12">
<!-- BEGIN CHART PORTLET-->
<div class="portlet light bordered">
<div class="portlet-title">
<div class="caption">
<i class="icon-bar-chart font-green-haze"></i>
<span class="caption-subject bold uppercase font-green-haze"><?php echo isset($breadcrumb) ? $breadcrumb : ''; ?></span>
<span class="caption-helper">column and line mix</span>
</div>
</div>
<div class="portlet-body">
<div id="project"> </div>
</div>
</div>
<!-- END CHART PORTLET-->
</div>
</div>
how to create daterange filtering in high chart?

Show icon instead of error message in knockout required field validation

<input type="text" data-bind="value:Firstname" />
<i class="fa fa-exclamation-circle"></i>
<i class="fa fa-check-circle"></i>
<input type="text" data-bind="value:Lastname" />
<i class="fa fa-exclamation-circle"></i>
<i class="fa fa-check-circle"></i>
<button type="button" data-bind="click: submit">Submit</button>
Need to show the icon for each textbox instead of showing required message while clicking the submit button itself.
contd:
fa-exclamation-circle icon style for required validation
fa-check-circle icon style for valid
Im very new to knockout so please tell me know how to do this kind of validation.
you can do the same using custom templates. created a fiddle for the same http://jsfiddle.net/nuDJ3/246/
HTML
<div data-bind="validationOptions: {messageTemplate: 'myCustomTemplate'}">
<input data-bind="value: firstName" />
<br />
<input data-bind="value: lastName" />
</div>
<script type="text/html" id="myCustomTemplate">
<span data-bind="if: field.isModified() && !field.isValid(), attr: { title: field.error }"><i class="fa fa-exclamation-circle"></i></span>
<span data-bind="if: field.isModified() && field.isValid(), attr: { title: field.error }"><i class="fa fa-check-circle"></i></span>
</script>
ViewModel
var ViewModel = function () {
var self = this;
self.firstName = ko.observable().extend({ required: { message: "firstName" }});
self.lastName = ko.observable().extend({ required: { message: "lastName" }});
}
var viewModel = new ViewModel();
ko.applyBindings(viewModel);
// modify the values to trigger validation
viewModel.firstName('');
viewModel.lastName('');
it is working fine for me.
Simply you can use the ko if binding :
http://knockoutjs.com/documentation/if-binding.html
Basicly if you want run the logic you should take in if binding
<input type="text" data-bind="value:Firstname" />
<!--ko if: validationIstrue-->
<i class="fa fa-exclamation-circle"></i>
<!--/ko -->
<!--ko ifnot: validationIstrue-->
<i class="fa fa-check-circle"></i>
<!--/ko-->
<button type="button" data-bind="click: submit">Submit</button>
or you can write a validation helper class for js, components works as.
http://knockoutjs.com/documentation/component-overview.html

Resources