Dynamically bind data with button in Knockout.js? - asp.net-mvc-3

I'm using MVC3 with Knockout.js and want to attach some data from the api to my button with data-bind=addContribute in a template. This button should open a pop up box and I need the attached button data on that pop up. How can I do this?
My template:
<div>
<ul data-bind="template: {name: 'membersTemplate', foreach: viewModel.membersList}">
</ul>
</div>
<script id="membersTemplate" type="text/html">
<li>
<div class="fl width165">
<img src=" ${ image } " width="33" height="34" />
<span> ${ memberName } </span>
${ memberType }
</div>
<aside class="fr margint10">
<label> ${ contributions } </label>
<a href="#" class="sprite-add_img" id="openContribute" title="Add Contributes to Goals" data-bind="click: viewModel.addContribute" ></a>
</aside>
</li>
</script>

<script id="membersTemplate" type="text/html">
<li>
<div class="fl width165">
<img data-bind="attr : {src : img}" width="33" height="34" />
<span data-bind="text : memberName"></span>
<span data-bind="text : memberType"></span>
</div>
<aside class="fr margint10">
<label data-bind="text : contributions"></label>
<a href="#" class="sprite-add_img" id="openContribute" title="Add Contributes to Goals" data-bind="click: addContribute" ></a>
</aside>
</li>
</script>
membersList varibale in you code should be next
function SingleMember(img, name, type, contr)
{
var self = this
self.img = ko.observable(img)
self.memberName = ko.observable(name)
self.memberType = ko.observable(type)
self.contributions = ko.observable(contr)
self.addContribute = function() {
//
}
}
window.viewModel = new function()
{
var self = this
self.membersList = ko.observableArray()
self.membersList.push(new SingleMember(/*.... params1*/))
self.membersList.push(new SingleMember(/*.... params2*/))
}

Related

Call method other component in vue

How to call method other component in Vue?
I have component HeaderSearch
<template>
<form action="">
<div class="form-group">
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button">
<i class="fa fa-search"></i>
</button>
</span>
<input type="text" #keyup="search(keyword)" v-model="keyword" class="form-control" placeholder="Search...">
</div>
</div>
</form>
</template>
<script>
export default {
data(){
return { keyword: "" };
},
methods: {
search: function(keyword){
if(keyword == ''){
// Want call method fetchPost in PostHome component here
}else{
}
}
}
}
</script>
And I have component PostHome
<template>
<div>
<div class="box r_8 box_shadow" v-for="post in posts">
<div class="box_header">
<a :href="post.url">
<h3 class="mg_bottom_10" v-text="post.title"></h3>
</a>
<small v-text="post.description"></small>
<a :href="post.url" class="box_header_readmore">Read more</a>
</div>
<div class="box_body">
<a :href="post.url" v-show="post.thumbnail">
<img :src="post.thumbnail" class="img_responsive" style="min-height: 300px;background-color: #f1f1f1;
">
</a>
</div>
<div class="box_footer" v-show="post.tags.length > 0">
<ul>
<li v-for="tag in post.tags">
<a v-text="tag.name" href="javascript:void(0)"></a>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return {
posts: null,
}
},
methods: {
fetchPosts: function(){
var url = base_url + '/user/posts'
this.$http.get(url).then(res => {
this.posts = res.data;
});
}
},
created: function(){
this.fetchPosts();
}
}
</script>
I want when user type keyup to search then if
keyword == ''
call method fetchPost method in PostHome component
You can use Mixins if that method is reusable.
Reference: https://v2.vuejs.org/v2/guide/mixins.html

How to autocomplete a <li> with <a>

I have generated a list with links inside in hierarchy. I am trying to make a form/input to let people type in words and it fetch the list(in hierarchy) with autocomplete. The autocomplete results are links. Any help would be great. Thanks
Something like this
<ul>
<li>
1
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
<li>
1
<ul>
<li>1.1</li>
<li>1.2</li>
</ul>
</li>
</ul>
Collect the autocomplete strings in an array using a JQuery selector and a map function
$(function() {
var availableTags = $('ul > li > a').map(function() {
return this.href;
}).toArray();
$("#tags").autocomplete({
source: availableTags
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<label>Items autocomplete</label>
<input id="tags" type="text" style="width: 200px;">
<span id="results_count"></span>
<p>
<ul>
<li>
1
<ul>
<li>1.1
</li>
<li>1.2
</li>
</ul>
</li>
<li>
1
<ul>
<li>1.1
</li>
<li>1.2
</li>
</ul>
</li>
</ul>
EDIT: second version where autocomplete uses the text in the <li>'s
$(function() {
var availableTags = $('ul > li > a').map(function() {
return $(this).text();
}).toArray();
$("#tags").autocomplete({
source: availableTags
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<label>Items autocomplete</label>
<input id="tags" type="text" style="width: 200px;">
<span id="results_count"></span>
<p>
<ul>
<li>
xxxx
<ul>
<li>xxyy
</li>
<li>xxzz
</li>
</ul>
</li>
<li>
xxyy
<ul>
<li>yyzz
</li>
<li>xxyz
</li>
</ul>
</li>
</ul>
Jsfiddle
see datalist and list in html5
datalist
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
$(function() {
var availableTags = $('ul > li > a').map(function() {
console.log($(this));
return {
label: $(this).text(),
value: $(this).attr('href')
};
}).toArray();
$("#tags").autocomplete({
source: availableTags,
select: function (event, target) {
var link = target.item.value;
console.log(link);
location.href = link;
}
});
});

Kendo UI Core ViewModel and Basic Databinding

I am having trouble getting basic databinding working.
It seems that I have to use .fetch for databinding to work. I feel this way isn't correct, plus the save method isn't called. Also the TabStrip has stopped working when using this method.
Is it always necessary to use the kendo application constructor at the end of each page? In another page if I use it the page does not show ???
I'm very new to Kendo.
The Json returned is
[{"JobID":8,"JobDescription":"Lets try these for apples","JobTemplateID":1,"JobTemplateName":"Standard Service","JobSignature":null,"JobNotes":null,"AssetName":null,"SiteName":"??? ","LocationName":"??? ","Priority":"???","JobLoggedOn":"2015-01-09T15:28:14","JobEstimatedStart":"2015-01-09T15:28:04.76","JobCompletedOn":null,"AssignedToName":"???"}]
and the code is
<!DOCTYPE html>
<html>
<head>
<title>Job Details</title>
<link rel="stylesheet" href="Content/Styles/Kendo/kendo.common.min.css" />
<link rel="stylesheet" href="Content/Styles/Kendo/kendo.default.min.css" />
<link rel="stylesheet" href="Content/Styles/Kendo/kendo.dataviz.mobile.min.css" />
<link rel="stylesheet" href="Content/Styles/Kendo/kendo.mobile.all.min.css" />
<script src="Scripts/Kendo/jquery.min.js"></script>
<script src="Scripts/Kendo/kendo.core.min.js"></script>
<script src="Scripts/Kendo/kendo.ui.core.min.js"></script>
<script src="Scripts/Kendo/kendo.mobile.button.min.js"></script>
<script src="Scripts/Kendo/kendo.mobile.listview.min.js"></script>
<script src="Scripts/Kendo/kendo.mobile.tabstrip.min.js"></script>
<script src="Scripts/Kendo/kendo.mobile.modalview.min.js"></script>
<script src="Scripts/Signature/jSignature.min.js"></script>
</head>
<body>
<div data-role="view" id="tabstrip-details" data-title="Details" data-layout="mobile-tabstrip">
<ul data-role="listview" data-style="inset" data-type="group">
<li>General Details
<ul>
<li>Site <span class="value" data-bind="text: jobDetails.SiteName"></span></li>
<li>Location <span class="value" data-bind="text: jobDetails.LocationName"></span></li>
<li>Asset <span class="value" data-bind="text: jobDetails.AssetName"></span></li>
<li>Priority <span class="value" data-bind="text: jobDetails.Priority"></span></li>
<li>Template <span class="value" data-bind="text: jobDetails.JobTemplateName"></span></li>
</ul>
</li>
<li>Dates and Assignee
<ul>
<li>Assigned To <span class="value" data-bind="text: jobDetails.AssignedToName"></span></li>
<li>Estimated Start <span class="value" data-bind="text: jobDetails.JobEstimatedStart"></span></li>
</ul>
</li>
</ul>
</div>
<div data-role="view" id="tabstrip-notes" data-title="Notes" data-layout="mobile-tabstrip">
</div>
<div data-role="view" id="tabstrip-steps" data-title="Steps" data-layout="mobile-tabstrip">
<ul data-role="listview" data-style="inset" data-type="group">
<li>Before Starting
<ul>
<li>
<label>
<input type="checkbox" />Step 1</label></li>
<li>
<label>
<input type="checkbox" />Step 2</label></li>
<li>
<label>
<input type="checkbox" />Step 3</label></li>
</ul>
</li>
<li>Step by Step
<ul>
<li>
<label>
<input type="checkbox" />Do This</label></li>
<li>
<label>
<input type="checkbox" />Do This</label></li>
<li>
<label>
<input type="checkbox" />Do This</label></li>
</ul>
</li>
</ul>
</div>
<div data-role="view" id="tabstrip-parts" data-title="Parts" data-layout="mobile-tabstrip">
</div>
<div data-role="view" id="tabstrip-photos" data-title="Photos" data-layout="mobile-tabstrip">
<input type="file" accept="image/*" capture>
<canvas></canvas>
</div>
<div data-role="view" id="tabstrip-done" data-title="Finish Job" data-layout="mobile-tabstrip">
<ul data-role="listview" data-style="inset" data-type="group">
<li>Completed On
<ul>
<li>
<label>
Date
<input type="date" data-bind="value: jobDetails.JobCompletedOn" />
</label>
</li>
<li>
<label>
Time
<input type="time" data-bind="value: jobDetails.JobCompletedOn" />
</label>
</li>
</ul>
</li>
<li>Sign Off
<ul>
<li>Signature <span class="value"><a data-role="button" data-rel="modalview" href="#modalview-sign">Sign Here</a></span></li>
</ul>
</li>
<li>Submit
<button data-bind="click: save">Job Done</button>
</li>
</ul>
</div>
<div data-role="layout" data-id="mobile-tabstrip">
<header data-role="header">
<div data-role="navbar">
<a class="nav-button" data-align="left" href="Home.html#JobList.html">Back</a>
<span data-role="view-title"></span>
</div>
</header>
<div data-role="footer">
<div data-role="tabstrip">
Details
Notes
Steps
Parts
Photos
Finish Job
</div>
</div>
</div>
<div data-role="modalview" id="modalview-sign" style="width: 90%; height: 70%">
<div data-role="header">
<div data-role="navbar">
<a data-align="right" data-click="closeModalView" data-role="button">Close</a>
</div>
</div>
<div id="signature"></div>
</div>
<style scoped>
.value {
float: right;
margin-top: 8px;
color: #bbb;
}
</style>
<script>
$("#signature").jSignature();
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "/api/job/getjob/8",
dataType: "json"
},
update: {
type: "POST",
url: "/api/job/SaveJob",
},
}
});
dataSource.fetch(function () {
var data = dataSource.data();
var job = data[0];
var viewModel = kendo.observable({
jobDetails: job,
save: function () {
this.dataSource.sync();
}
});
kendo.bind(document.body, viewModel);
});
var app = new kendo.mobile.Application(document.body);
</script>
<script>
function closeModalView() {
$("#modalview-sign").kendoMobileModalView("close");
}
</script>
</body>
</html>
Many thanks.

Data-binding works first time only, in a detail view

In this plunker sample, data-binding of the detail view works on first show, but does not update when navigating to other items.
What is missing ? Thanks!
Html:
<body>
<div data-role="view" data-model="aViewModel" id="aView">
<div data-role="header">
<div data-role="navbar">
<span data-role="view-title">Master</span>
</div>
</div>
<ul data-role="listview" data-style="inset"
data-bind="source: items, events : { click : onClickItem}"
data-template="itemsTemplate">
<script id="itemsTemplate" type="text/html" >
#:name#
</script>
</ul>
</div>
<div data-role="view" id="aDetailView"
data-model="aViewModel.currentItem"
data-show="aViewModel.onDetailViewShow">
<div data-role="header">
<div data-role="navbar">
<span data-role="view-title" data-bind="text:name"></span>
<a data-align="left" data-role="backbutton">Back</a>
</div>
</div>
<ul data-role="listview" data-style="inset" >
<li>Name <span data-bind="text: name"></span></li>
<li>Value <span data-bind="text: value"></span></li>
</ul>
</div>
<script src="script.js"></script>
JS:
var app = new kendo.mobile.Application(document.body );
ViewModel = function () {
var self = this;
function Item (name, value)
{
this.name = name;
this.value = value;
}
self.items = new kendo.data.DataSource({
data: [
new Item("AAA",1),
new Item("BBB",2),
new Item("CCC",3)
]
});
self.currentItem = null;
self.onClickItem = function(e) {
e.preventDefault();
self.set("currentItem", e.dataItem);
app.navigate("#aDetailView", "slide");
}
self.onDetailViewShow = function(e) {
}
self = kendo.observable(this);
return self;
};
var aViewModel = new ViewModel();

MVC Razor View - Wrapping rows constructed with a foreach loop every n items

I have a Razor view that has something like this in:
#foreach (var item in Model.Items)
{
<div class="product">
<a class="hide" href="#">
<img src="#" alt="Hide" />
</a><a href="#">
<img src="#" width ="138" height="140" alt="product name" />
</a>
<h4 class="title">#Html.Raw(#item.Name)</h4>
<span class="price">#item.Price.ToString("C")</span>
</div>
}
This is working well, and outputs the required html
However,
I need to wrap each "row" of with a
<div class="ClearFix"></div> - otherwise the layout gets messy after a few rows.
(In my case, a row is 5 product divs)
Is there any way of doing this?
#foreach (var row in Model.Items.Select((item, index) => new { index, item }))
{
if (row.index % 5 == 0)
{
<div class="ClearFix"></div>
}
<div class="product">
<a class="hide" href="#">
<img src="#" alt="Hide" />
</a>
<a href="#">
<img src="#" width ="138" height="140" alt="product name" />
</a>
<h4 class="title">#Html.Raw(#row.item.Name)</h4>
<span class="price">#row.item.Price.ToString("C")</span>
</div>
}
Shouldn't a simple int i and i % 5 == 0 and i > 5 do the trick?
If you truly need to wrap the contents in the clear fix div ...
#{
var grouping = new List<Item>();
var itemsPerRow = 5;
for (var i = 0; i < Model.Items.Count; i++)
{
var item = Model.Items[i];
grouping.Add(item);
if (i>0 && i%itemsPerRow == itemsPerRow-1)
{
<div class="clear-fix">
#foreach (var thing in grouping)
{
Html.RenderPartial("_ItemPartial", thing);
}
</div>
grouping.Clear();
}
}
}
and then in _ItemPartial.cshtml:
#model Item
<div class="product">
<a class="hide" href="#">
<img src="#" alt="Hide" />
</a><a href="#">
<img src="#" width ="138" height="140" alt="product name" />
</a>
<h4 class="title">#Html.Raw(#Model.Name)</h4>
<span class="price">#Model.Price.ToString("C")</span>
</div>

Resources