preload the selected values of a kendo multiselect that using a server bound data source and templated tags - initialization

I have a kendo multiselect as follows.
$("#tags").kendoMultiSelect({
change: onChange,
dataSource: {
transport: {
prefix: "",
read: {
url: "/OpsManager/Api/Activity/SearchResourcesTagged",
data: getSubmitData
}
},
serverFiltering: true,
filter: [],
schema: { errors: "Errors" }
},
itemTemplate: $('#resourceItemTemplate').html(),
tagTemplate: $('#resourceTagTemplate').html(),
dataValueField: "k",
value: [{"k":"[109]","n":"All Open Alerts","icon":"!","all":105}]
});
with the following templates:
<script id="resourceItemTemplate" type="text/x-kendo-template">
<span data-icon="#:data.icon#" class="#: data.s || '' #"> #:data.n #</span>
# if (data.d) { #
<div class="details">#: data.d #</div>
# } #
# if (data.details) { #
<div class="details k-state-disabled">
# for (var v in data.details) {
var t = typeof data.details[v];
if (t != "object" && t != "function" && v != "uid") { #
<div class="k-button">#: v #: #: data.details[v] #</div>
# } } #
</div>
# } #
</script>
<script id="resourceTagTemplate" type="text/x-kendo-template">
<span data-icon="#:data.icon#" class="tag-content #: data.s || '' #"> #:data.n #</span>
</script>
<select id="tags" multiple="multiple" name="tags"></select>
I'm trying to preload a specific selection and I can't seem to get it to work.
selection:
[{"k":"[109]","n":"All Open Alerts","icon":"!","all":105}]
I've put the initialized value in place according to their documentation and looking the multiselect object up inside the browser I see the passed in object inside _initialValues but I don't see anything inside _dataItems or in the tag list on the ui.
Any clues how to get this working?

Thanks to #OnaBai,
The problem is a difference in expected content of the datasources. The datasource originally loaded is not looking for "[109]" but rather "some string query" and then provides a specific list around that search. Thus I need to initialize a fake datasource for the control and then immediately switch out the data source for the dynamic one.
$("#tags").kendoMultiSelect({
change: onChange,
dataSource: {
transport : {
read: function (op) {
op.success([{"k":"[109]","n":"All Open Alerts","icon":"!","all":105}]);
}
}
},
itemTemplate: $('#resourceItemTemplate').html(),
tagTemplate: $('#resourceTagTemplate').html(),
dataValueField: "k",
value: ["[109]"]
});
$("#tags").data("kendoMultiSelect").setDataSource({
transport: {
read: {
url: "OpsManager/Api/Activity/SearchResourcesTagged",
data: getSubmitData
}
},
serverFiltering: true,
filter: [],
schema: { errors: "Errors" }
});
after that it works exactly like expected.

Two problems:
Define value in multiselect and not in dataSource.
Set value to an array with the list of key values that you want to initially load (in your case just "[109]").
Something like:
$("#tags").kendoMultiSelect({
change: onChange,
dataSource: {
transport: {
prefix: "",
read: {
url: "/OpsManager/Api/Activity/SearchResourcesTagged",
data: getSubmitData
}
},
serverFiltering: true,
filter: [],
schema: { errors: "Errors" }
},
itemTemplate: $('#resourceItemTemplate').html(),
tagTemplate: $('#resourceTagTemplate').html(),
dataValueField: "k",
value: ["[109]"]
});
Example in here

Related

angular js : ng-class use

i get a data for RestApi and i want create a function to retrieve data from json
if the defautl == null or the default. value = false . i change the color using ng -class of the component.
1:
commandStateTypes:{
defaut interne:null
defaut liaison:{date: "2016-05-30T01:01:04", value: true, name: "defaut liaison", idComponent: 1}}
component:{id:1 ,idInstallation:1,name:"Commande1"}
2:
commandStateTypes:{
defaut interne:null
defaut liaison:{date: "2016-05-30T01:01:04", value: true, name: "defaut liaison", idComponent: 2}}
component:{id:1 ,idInstallation:1, name:"Commande 2"}
controller :
$scope.status = { state: ['OK', 'KO'] }
Service.GetComponentsHelpers(function (data)
{
$scope.componentsHelper = data;
},
function (data)
{
});
$scope.GetStateByComponents = function ()
{
angular.forEach($scope.componentsHelper, function (componentsState, key)
{
angular.forEach($scope.componentsHelper.commandStateTypes, function(value, key){
});
});
}
chtml:
<div class="ajoutComponent">
<div class="motorsVitesse" ng-class="{ GetStateByComponents() ? 'mDangerV':'mDefaultV'}" alt="{{status.state[1]}}"> {{status.state[1]}}</div>
</div>
Try this in HTML:
ng-class="{'mDangerV': GetStateByComponents(), 'mDefaultV': !GetStateByComponents()}"

Issue Related to Autocomplete Textbox in knockout js

I have webapplication with knockoutjs
i have to implement Autocomplete textbox in a webform,
I got following tutorial
https://www.npmjs.com/package/knockout.autocomplete
i have added following code:
var viewModel = {
keywords: ko.observableArray([
'abstract', 'add', 'alias', 'as', 'ascending',
'async', 'await', 'base', 'bool', 'break'
])
};
ko.applyBindings(viewModel);
<input data-bind="autocomplete: { data: keywords, maxItems: 6 }" value=""/>
it works fine, but it binds text only, and i want to bind Text and Value Pair.
how can i achieve in above code?
Thanks
I haven't tested this, but it looks like it allows you to pass a formatter, so you can set up how it will be displayed to the user.
var viewModel = {
keywords: ko.observableArray([
{ name: 'Abstract', value: 'abstract' },
{ name: 'Break', value: 'break' }
]),
format: function (value) {
// This is what is shown in the list, only pass what you want
return value.name;
},
selected: ko.observable(),
onSelect: function (value) {
// value should be your name/value object
viewModel.selected(value);
}
};
<input data-bind="autocomplete: {
data: keywords,
maxItems: 6,
format: format,
onSelect: onSelect
}" value=""/>

Binding KendoGrid to a local object

I want to bind a KendoGrid to an object array so that it reflects what ever the user enters. The object will have two fields ExceptionName and ExceptionType. ExceptionType needs to be a dropdown of 5 items (this is working). The ExceptionName will be free text.
If I double click on the kendo grid, I can edit, but it does not reflects in the object. Same thing for Delete & new row. (So I think I am doing something wrong in the binding or in the declaration of the object)
Below, find a snippet of my code:
Object array:
var authorizationInformation = [{
id:1,
exemptionName: "",
exemptionType: "Unknown"
}];
KendoGrid:
$("#AuthorizationGrid").kendoGrid({
columns: [{
field: "exemptionName", title: "Exemption Name"
},
{
field: "exemptionType",
title: "Exemption Type",
template: function (value) {
for (var i = 0; i < exemptionTypeList.length; i++) {
if (exemptionTypeList[i].exemptionType == value.exemptionType) {
return exemptionTypeList[i].description;
}
}
},
editor: function (container) {
var input = $('<input id="exemptionType" name="exemptionType">');
input.appendTo(container);
// initialize a dropdownlist
input.kendoDropDownList({
dataTextField: "description",
dataValueField: "exemptionType",
dataSource: exemptionTypeList
}).appendTo(container);
}
},
{
command: "destroy"
}],
dataSource: authorizationInformation,
editable: true,
scrollable: false,
});
Any suggestion would be appreciated.
Thanks, M

Kendo UI Grid with Drop down Template Not updating value after save to server

We are using a server based simple grid. The grid reads and updates data for a remote data source. It has two columns that are using drop down editors. Everything seems to work fine except that after saving, when editor closes, the changed values are not displayed in the edited row. It still shows the old value. When we try to refresh the grid using the sync event, it changes the order of the rows however, it does update the values on refresh.
It seems like the template function is not executed after the update is completed. How to edit the grid / code to ensure that the changed value is reflected in the grid?
Grid Definition code:
$("#servicetype-grid").kendoGrid({
pageable: true,
toolbar: [{name: "create", text: ""}, { template: kendo.template($("#servicetype-search-template").html())}],
columns: [
{
field: "serviceName", title: "Service Name"
},
{
field: "billCalculationTypeId",
editor: calculationTypeDropDownEditor,
template: function(dataItem) {
return kendo.htmlEncode(dataItem.billCalculationTypeName);
},
title: "Bill Calculation Type"
},
{
field: "payCalculationTypeId",
editor: calculationTypeDropDownEditor,
template: function(dataItem) {
return kendo.htmlEncode(dataItem.payCalculationTypeName);
},
title: "Pay Calculation Type"
},
{
command: [
{ name: "edit", text: { edit: "", cancel: "", update: "" }},
{ name: "destroy", text:""}
],
title: "Actions"
}
],
dataSource: dataSource,
sortable: {
mode: "single",
allowUnsort: false
},
dataBound: function(e) {
setToolTip();
},
edit: function(e) {
$('.k-grid-update').kendoTooltip({content: "Update"});
$('.k-grid-cancel').kendoTooltip({content: "Cancel"});
},
cancel: function(e) {
setToolTip();
},
editable: {
mode: "inline",
confirmation: "Are you sure that you want to delete this record?"
}
});
Drop down function is defined as:
function calculationTypeDropDownEditor(container, options) {
$('<input required data-text-field="name" data-value-field="id" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
autoBind: false,
dataSource: {
transport: {
read: {
dataType: "jsonp",
url: baseURL + "rips/services/calculationType/lookupList"
}
}
}
});
}
After doing some search on Google and browsing through different examples on Kendo site, I finally was able to resolve this issue. Following is my understanding of the problem and solution:
When we are using drop down box or combo box as a custom editor, generally we have a different datasource that contains a list options with an id and a value to show. I defined the template as "#=field.property#" of the field that I was looking up. In my case it was calculation type. Following is my model:
model: {
id: "serviceId",
fields: {
serviceName: { field:"serviceName", type: "string", validation: { required: { message: "Service Name is required"}} },
billCalculationType: { field: "billCalculationType", validation: { required: true}},
payCalculationType: { field: "payCalculationType", validation: { required: true} }
}
In above, billCalculationType and payCalculationType are supposed to be drop down list values displaying the calculation type name but storing the id of the corresponding calculation type. So, in my grid definition, I added following:
{ field: "billCalculationType",
editor: calculationTypeDropDownEditor,
template:"#=billCalculationType.name#",
title: "Bill Calculation Type" },
Where calculation dropdown editor is a function that builds a drop down from external data source. So, it works fine. However, for the template definition to work in (field.property) format, the server must return the value as a class for this field and not a simple text. So, in my server service, I returned in following format:
{"billCalculationType":{"id":"4028828542b66b3a0142b66b3b780001","name":"Hourly","requiredDetails":false},"payCalculationType":{"id":"4028828542b66b3a0142b66b3b960002","name":"Kilometers","requiredDetails":false},"serviceId":"4028828542b66b3a0142b66b3c16000a","serviceName":"XYZ"}
Notice that the billCalculationType and payCalculationType are returned as objects with name and id as properties. This allows the template to work properly.
Hope this helps.

Knockoutjs: bind dynamic iframes in foreach to parent

I am trying to bind iframe and parent window so that I can change/update an observable in either the iframe or parent window and both views will update with new value.
Here is working sample: http://jsfiddle.net/NnT78/26/
I have tweaked some sample code that I have found and have it working great as follows;
HTML:
<iframe src="http://fiddle.jshell.net/zVPF8/11/show/" data-bind="bindIframe: $data"></iframe>
But when I put the same html in a foreach bind it get an error;
HTML:
<ul data-bind="foreach: iframes">
<li>
<iframe data-bind="attr: {src: src}, bindIframe: $data"></iframe>
</li>
</ul>
Error:
Uncaught ReferenceError: Unable to parse bindings.
Bindings value: text: someProperty
Message: someProperty is not defined
Here is my Knockoutjs ViewModel code;
ko.bindingHandlers.bindIframe = {
init: function(element, valueAccessor) {
function bindIframe() {
try {
var iframeInit = element.contentWindow.initChildFrame,
iframedoc = element.contentDocument.body;
} catch(e) {
// ignored
}
if (iframeInit)
iframeInit(ko, valueAccessor());
else if (iframedoc){
ko.applyBindings(valueAccessor(), iframedoc);
}
};
bindIframe();
ko.utils.registerEventHandler(element, 'load', bindIframe);
}
};
function ViewModel() {
var self = this;
self.someProperty = ko.observable(123);
self.clickMe = function(data, event) {
self.someProperty(self.someProperty() + 1);
}
self.anotherObservableArray = ko.observableArray([
{ name: "Bungle", type: "Bear" },
{ name: "George", type: "Hippo" },
{ name: "Zippy", type: "Unknown" }
]);
self.iframes = ko.observableArray([
{ src: "http://fiddle.jshell.net/zVPF8/6/show/", type: "Bear" },
{ src: "http://fiddle.jshell.net/zVPF8/6/show/", type: "Hippo" },
{ src: "http://fiddle.jshell.net/zVPF8/6/show/", type: "Unknown" }
]);
};
// Bind outer doc
ko.applyBindings(new ViewModel());
See http://jsfiddle.net/NnT78/26/ for sample of single iframe working and dynamic iframes in foreach bind not working.
Thanks in advance!
When in a foreach binding, $data is different; it's the current item in the array. You can fix your example by changing the iframe to bind to $root instead.
<iframe data-bind="attr: {src: src}, bindIframe: $root"></iframe>
http://jsfiddle.net/mbest/NnT78/29/

Resources