I have the following Kendo MVVM grid:
<div>
<div id="systemsGrid" data-role="grid"
data-sortable="true"
data-scrollable="true"
data-editable="false"
data-selectable="true"
data-toolbar="['create', 'save', 'cancel']"
data-bind="source: systems"
data-auto-bind="true
data-columns="[
{ 'field': 'SystemName', 'width': 200 },
{ 'field': 'SystemKey', 'width': 200 },
{ 'command': [{name: 'destroy', text: 'Delete'}], 'width': 38 }
]">
</div>
</div>
When a given row is clicked on, I want to use the SystemKey to look up additional data and display it on another grid. How do I capture this click event? I added this:
$("#systemsGrid").on("click", gridClick);
But this will also fire even if, say, the "Add new record" button is clicked. What is the proper way to respond to just a click on a row of the grid?
Why do not simply bind the change event to your gridClick?
Something like:
<div>
<div id="systemsGrid" data-role="grid"
data-sortable="true"
data-scrollable="true"
data-editable="false"
data-selectable="true"
data-toolbar="['create', 'save', 'cancel']"
data-bind="source: systems, events: { change: gridClick }"
data-auto-bind="true"
data-columns="[
{ 'field': 'FirstName', 'width': 100 },
{ 'field': 'City', 'width': 100 },
{ 'command': [{name: 'destroy', text: 'Delete'}], 'width': 100 }
]">
</div>
</div>
Where I bound gridClick using events: { change: gridClick }
You can see it here: http://jsfiddle.net/OnaBai/6XALD/2/
Related
I am creating a Todo Application using Vue.js, Express.js, and MongoDB.
I want to change the state of the fields that appear as v-for through the checkbox.
The code I want is to change the state of the text when the checkbox is checked and to show another element with v-if.
Here is the code I wrote: But it does not work.
If I have the wrong code, please help me.
List Component
<template>
<div class="todos">
<div v-for="todo in todos" v-bind:key="todo._id" class="todo">
<div>
<input type="checkbox" v-model="todo.done" v-on:click="completeTodo(todo)">
<del v-if="todo.done">{{todo.title}}</del>
<strong v-else>{{todo.title}}</strong>
</div>
</div>
</div>
</template>
<script>
export default {
data () {
return {
todos: {}
}
},
created () {
this.$http.get('/api/todos')
.then((response) => {
this.todos= response.data
})
},
methods: {
completeTodo (todo) {
todo.done = !todo.done
}
}
}
</script>
You don't need v-on:click="completeTodo(todo)". v-model is already doing the trick for you. Also, the todos in your code should be defined and instantiated as array not an object.
v-model is used for two way data binding. That means, whatever data you pass from your code will be bound to the checkbox value in this case and whenever a change is made from UI and value of checkbox is altered by user, that will be captured in v-model variable. v-model is a combo of :value prop and #change event in case of checkbox, hence, it is able to update data in both the ways.
Please refer this snippet.
var app = new Vue({
el: '#app',
data: {
todos: [
{
id: 1,
title: 'Study',
done: false
},
{
id: 2,
title: 'Work',
done: false
},
{
id: 3,
title: 'Play',
done: false
}
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="todos">
<div v-for="todo in todos" :key="todo.id">
<div>
<input type="checkbox" v-model="todo.done"/>
<del v-if="todo.done">{{todo.title}}</del>
<strong v-else>{{todo.title}}</strong>
</div>
</div>
</div>
</div>
I'm setting the selected option of a v-select using v-model, so when I open the screen it already comes with the result. That's working fine until I set autocomplete property in v-select tag. When I do that the option is chosen but the select doesn't show it.
Is there any issue with the autocomplete feature or it's the standard behavior for the component?
Vue.use(Vuetify);
var app = new Vue({
el: '#app',
data() {
return {
selected: 2,
country: [{
text: 'USA',
value: 1
}, {
text: 'Canada',
value: 2
}, {
text: 'England',
value: 3
}, {
text: 'France',
value: 4
}]
}
}
})
<script src="https://unpkg.com/vue#2.4.1/dist/vue.js"></script>
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet" />
<script src="https://unpkg.com/vuetify/dist/vuetify.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet" type="text/css">
<div id="app">
<v-app>
<v-card class="grey lighten-4 elevation-0">
<v-card-text>
<v-container fluid>
<v-layout row wrap>
<v-flex xs4>
<v-flex>
<v-subheader>Origin Country</v-subheader>
</v-flex>
<v-flex>
<v-select
:items="country"
v-model="selected"
label="Select"
class="input-group--focused"
single-line
bottom
autocomplete>
</v-select>
</v-flex>
</v-flex>
<v-flex>
Selected: {{ selected }}
</v-flex>
</v-layout>
</v-container>
</v-card-text>
</v-card>
</v-app>
</div>
Here it is jsfiddle link: https://jsfiddle.net/vcmiranda/huj9L4bq/
Thanks.
I test your code, vuetify will inject its classes after yours, after delete this line in v-select, it works.
class="input-group--focused"
Alternatively, using 'id' instead of 'class' is another option.
I am using struts2 autocomplete in a form. I will do a form submit after I choose all fields through autocomplete on various text boxes. Now, I don't want to allow user to type a random input in the auto complete text box and allow a submit. How to tackle this ?
EDIT:
<form class="form-horizontal" name="createproduct" role="form">
<div class="form-group">
<label for="density" class="col-sm-3 control-label">Select
Density</label>
<div class="col-sm-9">
<sj:autocompleter size="30" list="descArray" name="desc"
id="density" placeholder="Density" class="form-control"></sj:autocompleter>
</div>
</div>
<div class="form-group">
<label for="density" class="col-sm-3 control-label">Select
Material</label>
<div class="col-sm-9">
<sj:autocompleter size="30" list="matArray" name="mat" id="mat"
placeholder="Material" class="form-control"></sj:autocompleter>
</div>
</div>
..... //// more form elements go here ...... /////
Try Select2 jquery plugin.You can either fetch all the details and initialize the plugin or can fetch info on demand.
Fetching info based on input;
$("#material").select2({
tags: false,
minimumInputLength: 1, // Minimum Input length for search request
ajax: {
url:'actionName',
dataType:'json',
data:function(params){
return{
materialSearchTerm: params.term, // search term
page: params.page
};
},
processResults: function (data, page) {
var jsonResult = JSON.parse(data);
return {
results: jsonResult.result
};
}
}
});
actionName should return select input as JSON.
eg;- {result:[{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];}
I have this code...
$(function() {
var scheduler = $("#scheduler").kendoScheduler({
date: new Date("2015/1/1"),
views: [
{
type: "agenda",
eventTemplate: $("#event-template").html()
},
],
dataSource: [
{
id: 1,
start: new Date("2015/1/1 08:00 AM"),
end: new Date("2015/1/1 09:00 AM"),
title: "Test",
atendees: [1]
}
],
resources: [
{
field: "atendees",
dataSource: [
{ value: 1, text: "Jim" },
],
multiple: true
}
]
}).data("kendoScheduler");
$("#scheduler").on("click", ".edit-event",function() {
var dataItem = scheduler.occurrenceByUid($(this).data("uid"));
scheduler.editEvent(dataItem);
});
});
which works fine, but I am moving over to an MVVM approach can anyone help me get my new MVVM code to accept a template for my agenda view please. Code I have is
<script id="event-template" type="text/x-kendo-template">
<div class="edit-event" data-uid="#=uid#">
<div><b>Job</b>: #: title #</div>
<div>
<b>Staff:</b>
# for (var i = 0; i < resources.length; i++) { #
#: resources[i].text #
# } #
</div>
</div>
</script>
<div id="topper">
<div class="demo-section k-header wide">
<div>
<div data-role="scheduler" data-views="['agenda','month']" data-bind="source: tasks, visible: isVisible, events: { save: onSave }" data-template="event-template"></div>
</div>
</div>
</div>
Which does not work - doesn't crash, it just doesn't load the template I am also aware that this approach may use the template for all views not just agenda. Any help would be most appreciated.
To achieve the same behavior in MVVM you should define the view as object inside the "data-views" option:
<div data-role="scheduler"
data-views="['agenda',{ type: 'agenda', eventTemplate: $('\#event-template').html() }]"
data-bind="source: tasks, visible: isVisible, events: { save: onSave }"
data-template="event-template"></div>
I am using ChartJS with forms. What I am trying to do is get the value from the field to draw the specific chart segment. I want to use Knockout data-bind to get the chart to refresh when the number in the field is changed.
For some reason when I input a number it does not refresh automatically. However when I input a number and click refresh in the browser the number has held.
Can anyone help me so that the bind updates in realtime?
<body>
<form>
<input name="ValueStream" id="ValueStream" data-bind="value: ValueStream, valueUpdate: 'input'"></input>
<input type="text" name="ProductService" id="ProductService" value="200">
<input type="submit">
</form>
<div id="canvas-holder" style="width:30%">
<canvas id="chart-area" width="300" height="300"/>
</div>
<script>
var pAndS = document.getElementById("ProductService").value;
var vS = document.getElementById("ValueStream").value;
var polarData = [
{
value: pAndS,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: vS,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
window.onload = function(){
var ctx = document.getElementById("chart-area").getContext("2d");
window.myPolarArea = new Chart(ctx).PolarArea(polarData, {
responsive:true
});
};
</script>
</body>
Chart.js only draws the chart when you call the method. You'll need some way of seeing when the observable has changed and then call the PolarArea method again. You could try something like what is mentioned here: Detecting change to Knockout view model.