I have the following code in a
jsfiddle example
var remainingFalilities = [
{"Text":"Facility 1","Value":"1"},
{"Text":"Facility 2","Value":"2"},
{"Text":"Facility 3","Value":"3"},
{"Text":"Facility 4","Value":"4"},
{"Text":"Facility 5","Value":"5"},
{"Text":"Facility 6","Value":"6"},
{"Text":"Facility 7","Value":"7"},
];
var ds = new kendo.data.DataSource({
data: remainingFalilities
});
var selectedFacilities = [
{"Text":"Facility 8","Value":"8"},
{"Text":"Facility 9","Value":"9"}];
var dataSource = new kendo.data.DataSource({
data: selectedFacilities,
batch: false,
pageSize: 20,
schema: {
model: {
id: "Value",
fields: {Text: { type: "string" }}
}
}
});
$("#FacilityGrid").kendoGrid({
dataSource: dataSource,
autoBind: true,
editable: { mode: "inline" },
sortable: true,
selectable: true,
toolbar: ["create"],
columns: [
{ command: ["destroy", "edit"], title: " ", width: "250px" },
{ field: "Text", title: "Facility Name", editor: facilityDropDownEditor },
]
});
function facilityDropDownEditor(container, options) {
$('<input required data-text-field="Text" data-value-field="Value" data-bind="value: Text"/>')
.appendTo(container)
.kendoDropDownList({
autoBind: true,
dataSource: ds
});
}
The Problem is, when a user clicks "Add new record" it adds a new item with the facility name dropdown showing the 1st item in the list. If the user clicks "Update", it saves the record but blanks out the facility name. The reason is, there really isn't an item selected in the dropdown. I know this because the selected value is null when it is passed to my controller code. So, I really want to know how to either
Not show the first item in the list until the user actually selects it in the list OR
set the selected item to the first item in the list so the value will not be null when passed to my controller.
The answer for both ways would be even better.
To not showing the dropdown's items until user select them manually use optionLabel
function facilityDropDownEditor(container, options) {
$('<input required data-text-field="Text" data-value-field="Value" data-bind="value: Text"/>')
.appendTo(container)
.kendoDropDownList({
autoBind: true,
dataSource: ds,
optionLabel: "Select an item..."
});
}
OR
Use index to put initially selected item.
function facilityDropDownEditor(container, options) {
$('<input required data-text-field="Text" data-value-field="Value" data-bind="value: Text"/>')
.appendTo(container)
.kendoDropDownList({
autoBind: true,
dataSource: ds,
index : 1
});
}
One option, not listed among your proposals, is setting optionLabel that is displayed when the value is null.
$('<input required data-text-field="Text" data-value-field="Value" data-bind="value: Text"/>')
.appendTo(container)
.kendoDropDownList(
{
autoBind: true,
dataSource: ds,
optionLabel:"Select Facility"
}
);
Your jsfiddle modified here
Related
I have this grid with a custom multi-select filter. When I click the filter icon an AJAX method calls an API to grab filter values. The method call is defined in the DocType column.
Here is my grid builder function:
$("#attachmentsGrid").kendoGrid({
pageable: {
change: function() {
CH.RelatedItems.GridPager();
}
},
scrollable: false,
sortable: true,
dataSource: attachments,
filterable: {
extra: false,
operators: {
string: {
startswith: "Starts with",
eq: "Is equal to",
neq: "Is not equal to",
contains: "Contains",
}
}
},
columns:
[
{
title: "id",
field: "Id", //turns out this needs to be lower case
sortable: false,
filterable: false,
template: "<input name='cbCheckboxColumn' type='checkbox' onclick='CH.RelatedItems.ToggleAttachmentsByCheckboxColumn()' data-row='#: Id#' />",
headerTemplate: "<input id='cbCheckboxHeader' type='checkbox' onclick='CH.RelatedItems.ToggleAttachmentsByCheckboxHeader()'",
width: "18px",
},
{
title: "Exists?",
field: "IsAlreadyAttached",
type: "boolean",
sortable: false,
template: "<div style='text-align: center'>#: IsAlreadyAttached #</div>",
filterable: false,
width: "110px",
},
{
title: "Icon",
field: "DocType",
sortable: true,
template: "<div style='text-align: center'><img src='" + config.SPHostUrl + "/_layouts/images/#: DocIcon #' /></div>",
width: "60px",
filterable: {
ui: CH.RelatedItems.AttachmentsIconFilter,
extra: false,
},
},
{
title: "File Name",
field: "Name",
sortable: true,
filterable: false,
template: "<a href='#: Url#' target='_blank'>#: Name#</a>",
width: "650px",
},
{
title: "Modified",
field: "LastModifiedDateOnly",
template: "<span>#: LastModified#</span>",
sortable: true,
width: "250px",
filterable: {
ui: CH.RelatedItems.AttachmentsDateFilter,
extra: false
}
},
],
dataBound: CH.RelatedItems.PostGridDataBound
});
The AttachmentsIconFilter function is here:
pub.AttachmentsIconFilter = function (element) {
var menu = $(element).parent();
menu.find(".k-filter-help-text").text("Show document types:");
menu.find("[data-role=dropdownlist]").remove();
var multiSelect = element.kendoMultiSelect({
dataSource: {
transport: {
read: {
datatype: "jsonp",
url: config.WorkPaperViewRelativePath + "/_RelatedItemsPopup/GetAttachmentsUniqueDocIcons/" + config.SPQueryString
}
}
},
}).data("kendoMultiSelect");
menu.find("[type=submit]").on("click", { widget: multiSelect }, CH.RelatedItems.AttachmentsFilterByIcon);
}
When the AttachmentsIconFilter runs, the AJAX call get made twice:
Kendo Double GET
I thought that the property: filter: { extra: false } was supposed to prevent this double GET?
Also, when I select my filters and the grid make a new request to get the filtered records, it does a double POST. The function for the call to filter the grid is here:
pub.AttachmentsFilterByIcon = function (e) {
var icons = e.data.widget.value();
var filter = { logic: "or", filters: [] };
for (var i = 0; i < icons.length; i++)
{
filter.filters.push({ field: "DocIcon", operator: "Contains", value: icons[i].Contains });
}
$("#attachmentsGrid").data("kendoGrid").dataSource.filter(filter);
}
Any help here? What am I missing?
I have a kendoDropDownList column on a grid but I can't get the text of the item to display. It keeps showing the value!
var sexes = [{ "Text": "Male", "Code": "M" }, { "Text": "Female", "Code": "F" }];
function sexDropDownEditor(container, options) {
$('<input required data-value-field="Code" data-text-field="Text" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
dataTextField: "Text",
dataValueField: "Code",
dataSource: sexes,
template: "#=data.Text#"
});
}
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
selectable: "row",
filterable: true,
sortable: true,
height: 275,
toolbar: ["create"],
columns: [
....
{ field: "Sex", title: "Sex", editor: sexDropDownEditor }
....
{ command: ["edit", "destroy"], title: " ", width: "250px" }],
editable: "inline"
});
When the row is not being edited I would like the text of the selected item to be displayed but for some reason it displays the value.
Any ideas?
Sorted this by adding a template to the grid column (similar to the suggestion of Robin Giltner)
<script id="sexTemplate" type="text/x-kendo-template">
#if (Sex !== null) {#
#= Sex === 'M' ? 'Male' : 'Female' #
#}#
</script>
{ field: "Sex", title: "Sex", editor: sexDropDownEditor, template: kendo.template($("#sexTemplate").html()) },
I have a kendo mvvm grid in that how to keep dropdown list when it is on editable mode
I am new to using this kendo and mvvm concepts please help me
kendo.bind($("#example"), viewModel);
var grid = $("#example div[data-role='grid']").data("kendoGrid");
$.each(grid.columns, function (idx, column) {
if (column.field == "CourseName")
column.editor = viewModel.xx;
});
xxx: function name(container, options) {
$('<input id="ddl" data-text-field="Name" data-value-field="Name" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
dataTextField: "Name",
dataValueField: "Id",
dataSource: {
transport: {
read: {
url: "/controller/method",
dataType: "json"
}
}
},
autoBind: false,
index: -1
});
}
I have a kendo grid and I want to add a footerTemplate, but the value for the footerTemplate will be dynamic (other computations will be involved.) My question now, how to use the computed value to the footherTemplate?
Below is my sample code.
var computedValue= compute();
$("#grid").kendoGrid({
dataSource: {
data: setData(),
pageSize: 10
},
sortable: true,
scrollable: false,
pageable: true,
columns: [
{ field: "UnitPrice", title: "Unit Price",
footerTemplate: "Price : #=computedValue#"
},
{ field: "UnitsOnOrder", title: "Units On Order"},
{ field: "UnitsInStock", title: "Units In Stock"}
]
});
as you can see, the value for the footerTemplate is from a "var computedValue", now when I do that nothing happens. What is the correct way in order to show the value?
Thanks
Simply use a function for the footer template. Then, your function will be called each time the grid updates its contents.
footerTemplate: function(data) {
return "Price: " + compute();
}
You can use your function like this.
Kendo grid
$("#grid").kendoGrid({
dataSource: window.ds,
scrollable: false,
pageable: true,
editable: true,
columns: [
{ field: "Name", title: "Name" },
{ field: "Value", title: "Value",
footerTemplate: "Total:<span id='myId'> #=window.calc()#</span>" }
]
});
Javasript Function
<script>
function calc() {
// assume this to be dynamically determined
var field = "Value";
// assume this to be dynamically determined
var dataSource = window.ds;
// some custom calc logic
var newValue = 0;
$.each(dataSource.data(), function(index, model) {
newValue += model.get(field);
});
return newValue;
}
</script>
Refrence Link
I have column defined with template : "object.type" and it is dropdownlist (there are multiple types to search).
It has editor:
editor : function (container, options) {
$('<input data-text-field="display" data-value-field="id" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
index: 0,
dataTextField: "display",
dataValueField: "id",
dataSource: usergroupConf.permissions
});
},
so element for this column is object with keys id and display, e.g.
{
"id":"1",
"display":"Big"
}
now, I have defined filterable property for that column:
filterable : {
extra : false,
ui : function(element) {
element.kendoDropDownList({
index: 0,
dataTextField: "display",
dataValueField: "id",
dataSource: usergroupConf.permissions
});
}
}
when I click on filter box it displays filter fine, but when I select some value from it I get error :
Uncaught TypeError: undefined is not a function
So in short,
how to filter columns in kendo's grid which are dropdowns?
So, after massive research, soulution is following. Use foreign keys in kendo grid.
Create additional field which is key of the value selected in
dropdown
For that column, crate editor which would be bind to dropdown and it
would change this new field
{
field: 'permission_id',
title : 'Permission',
// values : usergroupConf.permissions,
template : "#= permission.display #",
editor : function (container, options) {
// bind it to permission, but update permission_id as well (because of enabled filtering)
$('<input data-text-field="display" data-value-field="id" data-bind="value:permission"/>')
.appendTo(container)
.kendoDropDownList({
index: 0,
dataTextField: "display",
dataValueField: "id",
dataSource: usergroupConf.permissions,
select : function(e) {
options.model.permission_id = this.dataItem(e.item.index()).id;
}
});
},
filterable : {
extra : false,
ui : function(element) {
element.kendoDropDownList({
dataSource: usergroupConf.permissions,
dataTextField: "display",
dataValueField: "id",
optionLabel: "--Select Value--"
});
}
}
},
This is the datasource
dataSource: new kendo.data.DataSource({
// pageSize: 10,
serverPaging: false,
schema: {
model: {
id : 'id',
fields: {
'id' : {},
'name' : {
editable : false,
nullable : false
},
'permission' : {
editable : true
},
'permission_id' : { // we'll have permission_id to enable filter (kendo doesn't support filtering through objects (what permission column is) by default)
editable : true,
type : 'number'
}
}
}
},
so in the end, when populating data you will have to add permission as object:
{
"id":1
"display":"Move"
}
and
permission_id:1