Mvvm dropdwonlist in kendoui - mvvm

I have a dropdownlist in my page this is the code:
<div id="test">
Role: <span data-bind="text: role"></span>
</br>
Roles:<select id="roles" data-bind="source: roles, value: role" data-text-field="roleName" data-value-field="roleId" ></select>
<ul id="view" data-template="Access-template" data-role="listview" data-bind="source:Accesses"></ul>
<script id="Access-template" type="text/x-kendo-template">
<li>
<input type="checkbox" data-bind="checked: selected" />
<label data-bind="text: AccessName" />
</li>
</script>
</div>​
and I want that when I change the dropdownlist value it changes my role collection. This is my code:
var Accesses = [{
AccessName: 'Create',
selected: false
}, {
AccessName: 'Delete',
selected: false
}, {
AccessName: 'Update',
selected: true
}];
var Roles = [{
roleName: "Admin",
roleId: 1,
accessItem: Accesses
}, {
roleName: "User",
roleId: 2,
accessItem: Accesses
}];
var viewModel = kendo.observable({
roles: Roles,
accssesItem: Roles.accessItem
});
kendo.bind($("#test"), viewModel);

Figuring out what DropDown item is selected is a little... unintuitive, but here is what you want:
<script id="Access-template" type="text/x-kendo-template">
<li>
<input type="checkbox" data-bind="checked: selected" />
<label data-bind="text: AccessName" />
</li>
</script>
<select
data-role="dropdownlist"
data-bind="source: roles, events: { select: roleSelected }"
data-text-field="roleName"
data-value-field="roleId"></select>
<ul data-template="Access-template"
data-role="listview"
data-bind="source: accessItem"></ul>
...and...
$(document).ready(function () {
var roles = [{
roleName: "Admin",
roleId: 1,
accessItem: [{
AccessName: 'Create',
selected: true
}, {
AccessName: 'Delete',
selected: true
}, {
AccessName: 'Update',
selected: true
}]
}, {
roleName: "User",
roleId: 2,
accessItem: [{
AccessName: 'Create',
selected: false
}, {
AccessName: 'Delete',
selected: false
}, {
AccessName: 'Update',
selected: true
}]
}];
var viewModel = kendo.observable({
roles: roles,
accessItem: roles[0].accessItem,
roleSelected: function (e) {
this.set("accessItem", this.roles[e.item.index()].accessItem);
}
});
kendo.bind("body", viewModel);
});
Working jsFiddle here: http://jsfiddle.net/rally25rs/JHNm6/

Related

Foreach instead of options binding - replicate “optionsCaption” functionality?

I have this select field:
<select data-bind="foreach: $root.feeGroups, value: $root.selectedFee">
<optgroup data-bind="attr: {label: label}, foreach: fees">
<option data-bind="text: description, option: $data"></option>
</optgroup>
</select>
The feesGroup property:
self.feeGroups([
{ label: "NEW", fees: self.fees().filter(f => f.status === "New") },
{ label: "OLD", fees: self.fees().filter(f => f.status === "Old") }
]);
And the binding handler:
ko.bindingHandlers.option = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
console.log(ko.toJSON(value));
ko.selectExtensions.writeValue(element, value);
}
};
My issue is with the "optionsCaption", as I am using a foreach method to generate the inner options it doesn't automatically work like it would if I was able to use the "Options" binding. But I do need to have a "Please Select..." default option.
Is there a way to do it?
You can move the foreach binding to a virtual element and add an extra option in your view that represents the null value:
<select data-bind="value: selectedFee">
<option data-bind="text: 'Select an option', option: null"></option>
<!-- ko foreach: feeGroups -->
...
<!-- /ko -->
</select>
Keep in mind that the selectedFee observable will contain null when the placeholder is active.
// From: https://stackoverflow.com/a/11190148/3297291
ko.bindingHandlers.option = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
ko.selectExtensions.writeValue(element, value);
}
};
const fees = [
{ type: "old", description: "test1" },
{ type: "old", description: "test2" },
{ type: "new", description: "test3" },
{ type: "new", description: "test4" }
];
const feeGroups = [
{ label: "new", fees: fees.filter(f => f.type === "new") },
{ label: "old", fees: fees.filter(f => f.type === "old") }
];
const selectedFee = ko.observable(null);
selectedFee.subscribe(console.log.bind(console, "new selection:"));
ko.applyBindings({ feeGroups, selectedFee });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<select data-bind="value: selectedFee">
<option data-bind="text: 'Select an option', option: null"></option>
<!-- ko foreach: feeGroups -->
<optgroup data-bind="attr: {label: label}, foreach: fees">
<option data-bind="text: description, option: $data"></option>
</optgroup>
<!-- /ko -->
</select>

collections2 and meteor-cfs-autoform to upload and display image

I am using collections2 and meteor-cfs-autoform for schemas and to upload and display image using cfs:gridfs storage adaptor package but unable to show images in template.Can you please tell me where i am doing mistake and its solution.
collections.js
Recipes = new Mongo.Collection('recipes');
Reviews = new Mongo.Collection('reviews');
RecipesImages = new FS.Collection("recipesImages", {
stores: [new FS.Store.GridFS("recipesImages")]
});
RecipesImages.allow({
insert: function(userId, doc) {
return true;
},
update: function(userId, doc, fieldNames, modifier) {
return true;
},
download: function(userId) {
return true;
},
fetch: null
});
Recipes.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Recipe Name",
max: 100
},
ingredients: {
type: [Object],
minCount: 1
},
"ingredients.$.name": {
type: String
},
"ingredients.$.amount": {
type: String
},
description: {
type: String,
label: "How to prepare ",
},
time: {
type: Number,
label: "Time (Minutes)",
},
image: {
type: String,
autoform: {
afFieldInput: {
type: "cfs-file",
collection: 'recipesImages',
label: 'Recipe Picture'
}
}
}
}));
recipes.js
Template.recipes.helpers({
'showRecipes':function(){
return Recipes.find();
},
'images': function () {
return RecipesImages.findOne({_id:id}) ;
}
})
recipes.html
<template name="recipes">
{{#each showRecipes}}
<div class=" col-md-4">
<div class="panel panel-default mb " >
<div class="panel-image">
<img src="{{this.url }}" class="panel-image-preview" />
</div>
<div class="panel-body">
<h4>{{name}}</h4>
<p>{{description}}</p>
</div>
<div class="panel-footer text-center" >
<p>its footer </p>
</div>
</div>
</div>
{{/each}}
</template>
Are you importing cfs:ui? If not, you should include it so that you can use the FS.GetFile helper. Then you can use this code.
Recipes Template
<template name="recipes">
{{#each showRecipes}}
<div class=" col-md-4">
<div class="panel panel-default mb " >
<div class="panel-image">
{{#with FS.GetFile "recipesImages" image}}
<img src="{{url}}" class="panel-image-preview" />
{{/with}}
</div>
<div class="panel-body">
<h4>{{name}}</h4>
<p>{{description}}</p>
</div>
<div class="panel-footer text-center" >
<span class="glyphicon glyphicon-open-file"></span>
</span><small> {{time}}</small>
<span class="glyphicon glyphicon-heart likes" style="vertical-align:middle"></span><small>2 </small>
<span class="glyphicon glyphicon-share"></span>
</div>
</div>
</div>
{{/each}}
</template>
Recipes helpers
Template.recipes.helpers({
'showRecipes':function(){
return Recipes.find();
}
});
Here's the link to cfs:ui documentation

Bootstrap Twitter Modal - Clearing dialog input fields

I have created a modal dialog using Bootstrap and I'm having a problem with the Modal dialog box not clearing form input fields after the box is closed or after it's submitted.
HTML
<!-- Password Reset Modal -->
<div id="passwModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4>Forgot Password?</h4>
<p>Please enter your registered email address below and a new password will be sent to you.</p>
</div>
<div class="modal-body">
<div id="sent"><!-- Display sent message after password reset -->
<form class="form-horizontal" id="reset">
<div class="control-group">
<label class="control-label" for="Email">Username</label>
<div class="controls">
<input class="input-large" type="email" id="Username" name="Username" placeholder="Email Address">
</div>
</div>
<div class="controls">
<button class="btn btn-inverse" id="close" value="CANCEL" data-dismiss="modal" aria-hidden="true">CLOSE</button>
<button class="btn btn-success" id="submit">SEND</button>
</div>
</form>
</div><!-- end of sent -->
</div>
</div><!-- end of Password Reset Modal -->
JavaScript (Validation & ajax)
$(document).ready(function () {
jQuery.validator.addMethod("accept", function (value, element, param) {
return value.match(new RegExp("." + param + "$")); });
$("#register").validate({
rules: {
FirstName: {
required: true,
accept: "[a-zA-Z]+"
},
LastName: {
required: true,
accept: "[a-zA-Z]+"
},
Pwd: {
required: true,
minlength: 8
},
CPwd: {
required: true,
minlength: 8,
equalTo: "#Pwd"
},
Email: {
required: true,
email: true
},
Agree: "required"
},
messages: {
FirstName: {
required: "First name required.",
accept: "Letters only please."
},
LastName: {
required: "Last name required.",
accept: "Letters only please."
},
Pwd: {
required: "Please create a password.",
minlength: "Password must be at least 8 characters."
},
CPwd: {
required: "Please confirm password.",
minlength: "Password must be at least 8 characters.",
equalTo: "Passwords do not match."
},
Email: "Email address is not valid.",
Agree: "Please accept our policy."
},
submitHandler: function() {
$.ajax({
type: "POST",
url: "../process.php",
data: $('form#register').serialize(),
success: function (msg) {
$("#thanks").html(msg)
},
error: function () {
alert("failure");
}
});
}
});
}); // end document.ready
Just an update to the previous answer for anyone now using the new Bootstrap, currently v3.1.1, that needs this answered!
$('#passwModal').on('hidden.bs.modal', function (e) {
$('#Username').val('');
});
To ensure that your form will be empty when you will reopen the modal, you can do this for every field on your form. It will delete the last value of your field on the hidden state of the modal when this one is closing :
$('#passwModal').on('hidden', function () {
$('#Username').val("");
});

codeigniter datatables get data from form

Pls help basically I want to filter my datatable using form.
each input has a value that would affect the query with submitted form.
here is my JQUERY.
$("#submit").click(function (e) {
$('#table').dataTable
({
"sAjaxSource": "index.php/report/get_report",
"sServerMethod": "POST",
'fnServerData': function (url, data, callback) {
// Add new data
dataString = $("#myform").serialize();
$.ajax({
'url': "index.php/report/get_report",
'data': dataString,
'type': 'POST',
'success': callback,
'dataType': 'json',
'cache': true
});
},
'bServerSide' : true,
"aaSorting": [[ 3, "desc" ]],
"bPaginate": true,
"bSortClasses": false,
"bAutoWidth": true,
"bInfo": true,
"iDisplayLength" : 3,
"bScrollCollapse": true,
"oLanguage": {
"sSearch": "Search:"
},
"bDestroy": true
});
});
and this is my HTML FORM
<form name="myform">
<label>Employee:</label>
<input type="text" name="employeeid" id="employeeid" title="Type Employee" />
<label>Training Type: </label>
<select name="trainingtype" id="trainingtype" >
<option value="" selected="selected">All</option>
<option value="1">Externally Facilitated Training</option>
<option value="3">Internally Facilitated Training</option>
<option value="2">Webcast/E-Learning</option>
</select>
<label>Datestart</label>
<input type="text" class="field size3" name="datestart" id="datepicker_s" />
<label>Dateend </label>
<input type="text" class="field size3" name="dateend" id="datepicker_e" />
<input type="hidden" id="txtsearchid" name="txtsearchid">
<input type="button" class="button" value="Submit" id="submit" />
when I submit my form I get nothing.
Im I doing the right way?
pls help.
GOT IT
"fnServerData": function ( sSource, aoData, fnCallback ) {
//REQUIRED: Add a Post variable with the object value
aoData.push(
{ "name": "txtsearchid", "value": $( "#txtsearchid" ).val() },
{ "name": "datestart", "value": $( "#datepicker_s" ).val() },
{ "name": "dateend", "value": $( "#datepicker_e" ).val() },
{ "name": "trainingtype", "value": $( "#trainingtype" ).val() }
);
$.ajax( {
dataType: 'json',
type: "POST",
url: sSource,
data: aoData ,
success: fnCallback
} );
},
this must be the solution for my problem. I didnt used the serialized instead of push
Use Firebug add ons from Mozilla to check the error

Bind checbox list to dropdownlist in kendo ui by mvvm [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a dropdownlist in my view i want in my view.I want when change my dropdownlist,a java script template bind dropdownlist.i write this but dont work plead help me.
var roles=[{
code:1,
roleName: "Admin",
access: [
{ id: 1, description: "create", selected: true},
{id: 2, description: "delete", selected: false},
{ id: 3, description: "update", selected: false}
]
} ,{
code:2,
roleName: "user",
access: [
{ id: 1, description: "create", selected: true},
{id: 2, description: "delete", selected: true},
{ id: 3, description: "update", selected: false}
]
}];
var viewModel = kendo.observable({
Roles:roles,
role:"Admin",
accessRole:null
});
kendo.bind($("#example"), viewModel);
this my view code
<div id="example">
Current Role :<span data-bind="text: role"></span>
<br>
<select type="text" id="RoleName" data-bind="source: Roles, value:role" data-text-field="roleName">
<select/>
<ul data-template="row-template" data-bind="source: accessRole.access"></ul>
</div>
<script id="row-template" type="text/x-kendo-template">
<li>
<input type="checkbox" data-bind="checked: selected" />
<label data-bind="text: description" />
</li>
</script>
​
and this is onlne code:
http://jsfiddle.net/shahr0oz/K4X3T/19/
​
I get it.
<div id="example">
Current Role :<span data-bind="text: role.roleName"></span>
<br>
<select type="text" data-bind="source: Roles,value:role}" data-text-field="roleName">
<select/>
<ul data-template="row-template" data-bind="source:role.access"></ul>
</div>
<script id="row-template" type="text/x-kendo-template">
<li>
<input type="checkbox" data-bind="checked: selected" />
<label data-bind="text: description" />
</li>
</script>
​ var roles=[{
code:1,
roleName: "Admin",
access: [
{ id: 1, description: "create", selected: true},
{id: 2, description: "delete", selected: true},
{ id: 3, description: "update", selected: false}
]
} ,{
code:2,
roleName: "user",
access: [
{ id: 1, description: "create", selected: false},
{id: 2, description: "delete", selected: false},
{ id: 3, description: "update", selected: false}
]
}];
var viewModel = kendo.observable({
Roles:roles,
role:roles[0]
});
kendo.bind($("#example"), viewModel);
​
http://jsfiddle.net/shahr0oz/psEVy/