knockout dropdown selection change - drop-down-menu

Hi Friends i am trying to use knockoutjs on one of my web pages in mvc
razor. From the following ViewModel i am achieving the "ADD MORE"
functionality.But My Problem is that how can we achieve the following
functionality:
(1) The second dropdown should be invisible when the dropdown for
"Type of Tobacco" is selected to "Choose..."
(2)When we choose some other value (other than "Choose") second
dropdown should be populated containing value 1 to 10....
(3) When we select "Others" instead of drop down a textbox should
appear
HERE IS MY ATTEMPT:
<script src="#Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/knockout-2.1.0.js")" type="text/javascript"></script>
<script type="text/html" id="ddlSelection">
<div><select></select> yrs.</div>
<div><input type="text" data-bind=""></input></div>
</script>
<div id="container">
#*<div data-bind="template:{name:'smoker'}"></div>*#
<table cellpadding="3" cellspacing="4">
<tbody data-bind="foreach: seats">
<tr>
<td>
Type of Tobacco/Nicotine consumed :
</td>
<td>
<select data-bind="options: $root.availabledrugs, value: Drug, optionsText: 'DrugName'"></select>
</td>
<td><select></select></td>
</tr>
<tr>
<td>
Quantity : <input data-bind="value: Name" />
</td>
<td>
Frequency : <select data-bind="options: $root.availablefrequency, value: Frequency, optionsText: 'frequency'"></select>
</td>
<td data-bind="text: FormatPrice"></td>
</tr>
</tbody>
</table>
<button data-bind="click:AddConsumption">Add New One</button>
</div>
<script type="text/javascript">
function setconsumption(name, initdrug,initfrequency) {
var self = this;
self.Name = name;
self.Drug = ko.observable(initdrug);
self.Frequency = ko.observable(initfrequency);
self.FormatPrice = ko.computed(function () {
return self.Drug().Price ? "$" + self.Drug().Price.toFixed(2) : "none";
});
}
function ConsumptionViewModel() {
var self = this;
self.availabledrugs = [{ "DrugName": "Choose...", "Price": 0 },
{ "DrugName": "Cigarettes", "Price": 10 },
{ "DrugName": "Cigar", "Price": 20 },
{ "DrugName": "Others", "Price": 30}];
self.availablefrequency = [{ "frequency": "Choose..." }, { "frequency": "freq1" }, { "frequency": "freq2"}];
self.seats = ko.observableArray(
[new setconsumption("", self.availabledrugs[0], self.availablefrequency[0])]);
self.AddConsumption = function () {
self.seats.push(new setconsumption("", self.availabledrugs[0], self.availablefrequency[0]));
};
}
ko.applyBindings(new ConsumptionViewModel());
</script>

Its hard to guess for me that what you are trying to achieve. But i think you are looking for visible binding in knockout.
The visible binding causes the associated DOM element to become hidden or visible according to the value you pass to the binding.
And second think should be the optionsCaption comes under options binding.
If you use optionsCaption with options binding than ko will prepend an extra option in the select list, which will be selected by default and contains value undefined.
By using this both i have create a fiddle according to your requirement. Check this:
Demo fiddle
Hope this is what you are looking for!

Related

how to add filters and search in tables in vuejs?

I am learning vue js and building a SPA i want to know how do I add filters and search using an input tag. I also want to add a feature that when i click on a particular name on the table it should open the profile of that person also a select all functionality
<template>
<div class="animated fadeIn">
<input type="text" placeholder="Enter Name" v-model="searchText">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>College Name</th>
<th>College City</th>
<th>Level Name</th>
<th>Submitted</th>
<th>Pending</th>
<th>Completed</th>
<th>Approved</th>
<th>Rejected</th>
</tr>
</thead>
<tbody>
<tr v-for="profile in profilesdata">
<td>{{profile.first_name}}</td>
<td>{{profile.college_name}}</td>
<td>{{profile.college_city}}</td>
<td>{{profile.level_name}}</td>
<td>{{profile.submitted}}</td>
<td>{{profile.pending}}</td>
<td>{{profile.complete}}</td>
<td>{{profile.approve}}</td>
<td>{{profile.rejected}}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default{
name: 'profiles',
data () {
return{
profilesdata: []
}
},
created:function(){
this.loadlike()
},
methods:{
loadlike:function(){
this.$http.get('/api/profiles').then(function (res) {
this.profilesdata = res.body
console.log(53+this.profiles)
})}}}
</script>
you could probably return computed list instead of profilesData
<template>
...
<input type="text" placeholder="Enter Name" v-model="searchText">
...
<tr v-for="profile in computedProfilesData">
...
</template>
<script>
export default{
...
data () {
return {
...
// - you need info for searchText
searchText: ''
}
}
...
computed: {
computedProfilesData(){
let searchString = this.searchText;
return this.profilesdata.filter((profile) => {
// example
profile.first_name.indexOf(searchString) !== -1;
})
}
}
</script>
there is a lot of different ways to do this, this is just one of them.
you can pass that searchString to API and return result list, it all comes to that
what do you really need.
You can make a computed for the filtered data:
computed: {
filteredProfiles() {
return this.profilesdata.filter(profile => {
// TODO filter profile with this.searchText
})
}
}
And then change the v-for to loop over the filtered data: <tr v-for="profile in filteredProfiles">

Populate Multi-select list from Ajax in Knockout

I have an editable table based off of the Knockoutjs example. I've been trying to populate a multi-select dropdown list from an AJAX call to ASP MVC controller. I've looked through this example, this one, and this one but am still having issues getting it to work.
Here's the html:
<tbody data-bind='foreach: users' class="table-striped">
<tr>
<td><input class='required number form-control' maxlength="9" stringlength="9" data-bind='value: id, uniqueName: true' /></td>
<td><input class='required form-control' data-bind='value: firstName' /></td>
<td><input class='required form-control' data-bind='value: lastName' /></td>
<td>
<select data-bind="options: MAMUserGroupsListOptions, selectedOptions: MAMSelectedGroup">
<!--dropdown list goes here-->
</select>
</td>
<td><a href='#' data-bind='click: $root.removeUser'>Delete</a></td>
</tr>
</tbody>
Here's the javascript for the knockout portion:
var UserModel = function (users) {
var self = this;
self.users = ko.observableArray(users);
//add user
self.addUser = function () {
self.users.push({
id: "",
firstName: "",
lastName: "",
MAMUserGroupsListOptions: "",
MAMUserGroups: ""
});
};
//remove user
self.removeUser = function (user) {
self.users.remove(user);
};
var viewModel = new UserModel([
{
id: ko.observable(""), firstName: ko.observable(""), lastName: ko.observable(""),
MAMUserGroupsListOptions: ko.observable(),//this is where I need to populate
MAMSelectedGroups: ko.observable(),//this is the result of the multiselect
}
]);
ko.applyBindings(viewModel);
Here's the controller:
[HttpGet]
public ActionResult MAMUserGroupsList()
{
var MAMUserGroupsListOptions = db.MAMUserGroupsListModels.Select(x => new {
MAMUserGroupName = x.MAMUserGroupName
}).ToList();
return Json(MAMUserGroupsListOptions, JsonRequestBehavior.AllowGet);
}
When the controller is called via AJAX, this is what returns:
{MAMUserGroupName: "MAMGroup1"}, {MAMUserGroupName: "MAMGroup2"}, etc...
I'm trying to get the multi-select dropdown to have "MAMGroup1", MAMGroup2, etc.
I've been banging my head against the wall on this- any help would be much appreciated.
Looks like you just want to map the array of objects to extract the one value of interest. If you start with your result in a variable named ajaxResult,
MAMUserGroupsListOptions(ajaxResult.map(obj => obj.MAMUserGroupName));
will assign the values to your observable array. Or if the value is available to you when you're creating your viewmodel,
MAMUserGroupsListOptions: ko.observable(ajaxResult.map(obj => obj.MAMUserGroupName))

Access to parent scope in kendo template

I'm try to access parent data scope in kendo grid template, is it a bad practice use "this.parentDataItem"? Or exist other ways? As final result in my example I want hide first column.
My Grid:
<div id="someGrid"
data-role="grid"
data-row-template="someTemplate"
data-bind="source: gridDataSource"
data-columns="[
{ title: 'FirstColumn', width: '100px' },
{ title: 'SecondColumn', width: '100px'},
]">
</div>
My template:
<script id="someTemplate" type="text/x-kendo-template">
<tr role="row" data-uid="${uid}">
<td class="check-row"># if(this.isFirstColumnVisible) { # InvisibleText # } #</td>
<td class="check-row">VisibleText</td>
</tr>
<script>
My model
{
isFirstColumnVisible: false,
gridDataSource: ...
}
This did the trick:
parent().parent().myProperty

popup a dialog and submit partial view in asp.net mvc

So what I am trying to do here is that in the View,
when I click on the button, I want to pass some values generated from the foreach loop (in my case Country and City) to the javascript function.
Inside that javascript function, I want to open up a dialog(partial view) by passing those values (countryName, cityName) to the controller.
Hence in my controller, it will pass those values to the partial view which will be appeared as a dialog and can submit the form.
I've tried with the version without the pop-up dialog (it worked), but having a hard time doing it with a dialog. #3 works btw, but I think I am having a trouble with #1 and #2. Any help would be appreciated. Thanks.
View:
#model IEnumerable<test.Models.Employee>
<table class="table">
<tr>
<th>Country</th>
<th>City</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Country)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
<button onclick="OpenDialog(item.Country, item.City)">
Open Dialog
</button>
</td>
</tr>
}
</table>
<div id="dialog"></div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/jquery-ui-1.12.0.js"></script>
<script>
function OpenDialog(countryName, cityName) {
$('#dialog').dialog({
autoOpen: true,
width: 400,
resizable: false,
title: 'My Table',
modal: true,
open: function(event, ui) {
$(this).load('#Url.Action("getEmployee", "Employee", new
{
country = countryName,
city = cityName
})');
},
buttons: {
"Close": function () {
$(this).dialog("close");
}
}
});
}
</script>
}
Controller:
public ActionResult getEmployee(string country, string city)
{
var viewModel = new EmployeeViewModel()
{
Country = country,
City = city
};
return PartialView("EmployeeDialog", viewModel);
}
Partial View:
#model test.ViewModels.EmployeeViewModel
#using (Html.BeginForm("PostEmployee", "Employee", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.HiddenFor(model => model.Country)
#Html.HiddenFor(model => model.City)
#Html.EditorFor(model => model.Comments)
<input type="submit" value="Submit"/>
}
#Url.Action() is razor code and is parsed in the server before its sent to the view. countryName and cityName are javascript variables and do not even exist at that point (they are not in scope). Change the code in the OpenDialog() function to
var url = '#Url.Action("getEmployee", "Employee")';
var data = { country: countryName, city: cityName };
$(this).load(url, data);
As a side note, there is really no need to be calling the server each time to return values that you already have in the view. You could just render the form in the dialog initially (for a default EmployeeViewModel and then in the OpenDialog just set the values of the inputs for properties Country and City, for example $('#Country').val(countryName);

Issue while validating the form using JQuery

Scripts
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script src="jquery.validate.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#Field2").rules("add", { regex: "^[a-zA-Z0-9]{0,3}$" });
$.validator.addMethod(
'regex',
function (value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
}, "My Error Message");
$('#MyForm').validate(
{
rules: {
Field1: {
required: false,
range: [1, 9999999999]
}
},
messages: {
Field1: {
range: jQuery.validator.format("Please enter a value between {0} and {1}.")
}
}
,
highlight: function (element) {
$(element).closest('.MyForm').removeClass('success').addClass('error');
},
success: function (element) {
}
});
});
</script>
Form
<form action="#" id="MyForm" name="MyForm">
<table id="tblData">
<tr>
<td>
<label>
Field1</label>
<input id="Field1" name="Field1" type="text" value="" />
</td>
</tr>
<tr>
<td>
<label>
Field2</label>
<input id="Field2" name="Field2" />
</td>
</tr>
<tr>
<td>
<span id="submitButton" class="k-button">Next</span>
<input type="submit" class="button" name="submitButtonTest" id="submitButtonTest"
value="Validation Test">
</td>
</tr>
</table>
</form>
Issue
$.data(...) is undefined
Not validating Field1.
Not showing error messages
JSFiddle
Demo is here
You can't define a rule for a field before you enable validation for the form that contains it. So you need to put
$("#Field2").rules("add", { regex: "^[a-zA-Z0-9]{0,3}$" });
after
$("#myForm").validate(...);
FIDDLE
If you want to specify a message when adding the rule, you can do:
$("#Field2").rules("add", {
regex: "^[a-zA-Z0-9]{0,3}$",
messages: {
regex: "Must be 0 to 3 alphanumeric characters"
}
});
See the documentation of .rules()

Resources