Hi is there any way to pass detailRow reference to a function using kendo grid template column?
Here is my trail.
function detailInit(e) {
detailRow = e.detailRow;
detailRow.find("#mygrid").kendoGrid({
dataSource: {
data: empModel,
},
columns: [
{
field: "empId",
title: "Emp ID",
template: '<a href="\\#" onclick="showEmpDetails(\'#= detailRow #\')"> }
]
});
});
Try to place every detailRow you retrieve at the detailInit into globally located array, then pass this index to your click method (or some sort of key - could be dictionary, rows has uid) and then make the method to read the row details from your array/collection based on the id you have passed in. (Ideally read the row details directly from your datasource by the uid, no need to duplicate the data.) Please refer to below code as pseudo code, I didn't have a chance to run it.
var rows = new Array();
$('.clickable').click(function () {
// get the id for your detail
var detailId = this.attr('detailId');
// pass it further
showEmpDetails(detailId);
});
function detailInit(e) {
detailRow = e.detailRow;
// add roe to your (cache) collection
rows[rows.legth] = detailRow ;
detailRow.find("#mygrid").kendoGrid({
dataSource: {data: empModel},
columns: [
{
field: "empId",
title: "Emp ID",
// add attribute onto your a tag
template: '<a href="\\#" detailId = "#= rows.legth #" class="clickable">'
}
]
});
});
function showEmpDetails(id) {
// read from your collection and do what you need
console.log(rows[id]);
}
Related
I have setup a Kendo Grid with a column which contains a ComboBox editor template assigned. This works well and retrieves the items I am expecting:
columnSchema.push({ field: "Comment", title: 'Comment', editor: commentDropDownEditor, template: "#=Comment.Description#" });
Produces the following:
My issue is that I am now trying to extend the properties of the 'Comment' column so that users can enter new items i.e. comments that will, in turn, be saved to the database for reuse.
So far, I have done the following...
Setup the commentDropDownEditor function:
function commentDropDownEditor(container, options) {
$('<input name="' + options.field + '"/>')
.appendTo(container)
.kendoComboBox({
suggest: true,
change: onComboBoxChange,
autoBind: false,
optionLabel: "Select comment...",
dataTextField: "Description",
dataValueField: "ID",
dataSource: {
type: "json",
autoSync: true,
transport: {
read: "CommentsAsync_Read",
create: {
url: "CommentsAsync_Create",
dataType: "json"
}
}
}
});
$('<span class="k-invalid-msg" data-for="' + options.field + '"></span>').appendTo(container);
}
Hooked up the change event to the following function:
function onComboBoxChange(e) {
var combo = e.sender;
// check if new value is a custom one
if (!combo.dataItem()) {
// select the newly created dataItem after the data service response is received
combo.one("dataBound", function () {
combo.text(combo.text());
});
// create a new dataItem. It will be submitted automatically to the remote service (autoSync is true)
combo.dataSource.add({ Description: combo.text() });
}
}
I can see the onComboBoxChange function being hit through the Console Window (including the line where the new item is being added to the combo box datasource) but the associated Create transport function is not being executed nor is the item being added to the Combo Box.
Create function in the Controller is as follows:
[HttpPost]
public async Task<ActionResult> CommentsAsync_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<Comment> comments)
{
if (comments.Any())
{
//await _manager.CreateCommentAsync(comments);
}
return Json(comments.ToDataSourceResult(request, ModelState));
}
Is there something I am missing/doing wrong here to get this to work?
I have a Kendo UI combobox object something like this :
widget: "kendoComboBox",
options: {
dataTextField: "#:userFirstName# #:userLastName#",
dataValueField: "userId",
template: "#:userFirstName# #:userLastName#",
change: function (e) {
that.model.fn.bringUserData();
}
}
I can arrange the template, but i cannot dataTextField value depends on that template. It is possible to make it "userId" etc. But seems not possible to set selected value as #:userFirstName# #:userLastName#. (dataTextFieldTemplate doesn't work.)
Could you help me to solve this?
Correct, you cannot make it a composition of two fields. It needs to be a field per se. What you can do is when reading data from the DataSource create an additional field that is the concatenation of those two fields. You can add to you DataSource definition something like this:
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "..."
}
},
schema: {
parse: function(response) {
$.each(response, function(idx, elem) {
elem.fullName = elem.firstName + " " + elem.lastName;
});
return response;
}
}
});
Then the options for the combobox are simply:
options: {
dataTextField: "fullName",
dataValueField: "userId",
...
}
See it in action here : http://jsfiddle.net/OnaBai/12hpLeux/1/
I want to have multiple datatextField column as im returning a custom List which returns me list with property Name,Status and PID, but i can't use multiple columns on my DatatextField i-e Name and Status so that i can use them for my template property shown below,
Name and Status column is necessary for my template and PID is necessary for my datavalueField
it shows me error for Status is undefined
<script>
$(document).ready(function () {
$("#prog").kendoDropDownList({
dataTextField: "Name",
dataValueField: "PID",
optionLabel: "...select programme...",
headerTemplate: '<div class="dropdown-header">' +
'<span class="k-widget k-header">status</span>' +
'<span class="k-widget k-header">Name</span>' +
'</div>',
valueTemplate: '<span class="selected-value">#: Name#</span>',
template: '<span class="k-state-default">#: Status#</span>' +
'<span class="k-state-default"><h3>#: Name#</h3></span>',
dataSource: {
transport: {
read: {
dataType: "json",
url: "#Url.Action("GetProgrammesInfo", "Programme", new { ECID = ViewBag.ECID as int? })"
}
}
},
change: function (e) {
var value = this.value();
alert(value);
}
});
var dropdownlist = $("#prog").data("kendoDropDownList");
});
</script>
I think to reference a property inside the template that isn't the textfield or valuefield, you will need to use data.Status.
If I switch to that, it seems to work. Also if I switch the dataTextField to Status, I get the error on the Name, and if I change the Name to data.Name, it works again.
Somewhat working sample... http://jsbin.com/xemef/1/edit
On my Kendo grid, I have "editable=true" and datasource with "autoSync=true". As I click on a cell, it becomes editable and when leaving the cell it executes the transport's update event.
That's all fine.
In the update event I have access to the row of the dataset model containing all the values of the modified row (although with editable=true and autosync, only one column value would have been modified).
I need to know which column / field was modified?
Ideally I thought that info would be in the arguments (options) supplied to the update event.
dataSource = new kendo.data.DataSource({
autoSync: true,
transport: {
update: function (options) {
// options does not tell me which model field was updated?
...
But since it isn't there, I suppose I need to bind to the model's set event, but I cannot get that to work.
Any ideas?
Never mind this answer! I read the question all wrong.
I think I got it working the way you like.
Is this jsFiddle what you're looking for?
var ds = new kendo.data.DataSource({
autoSync: true,
transport: {
read: function(options) {
options.success([
{ Id: 1, A: 'Hello', B: 'World' },
{ Id: 2, A: '1', B: '2' },
{ Id: 3, A: 'fdasf', B: '4523' }
]);
},
update: function (options) {
var uid = ds.data().find(function(p) {return p.Id == options.data.Id;}).uid;
var tr = $('#grid .k-grid-content tr[data-uid="'+uid+'"]');
console.log(tr);
//Do something with tr
}
},
schema: {
model: { id: "Id" }
}
});
$('#grid').kendoGrid({
dataSource: ds,
editable: true,
});
Can someone tell me how to bind kendo grid based on previous control selection?
Ex: I've placed one dropdown list and grid in a page. Now I wanted to populate data in grid based on dropdown selected value.
Can someone help me to do this. I'm working with MVC.
try this :
$("#dept").kendoComboBox({
filter: "contains",
index: 0,
dataTextField: "Name",
dataValueField: "ID",
dataSource: data,
select: onSelect
});
//Dropdown change event
function onSelect(e) {
var dataItem = this.dataItem(e.item.index());
UpdateUPGridSource(dataItem.value);
}
//Refresh Datasource by Role wise
function UpdateGridSource(DropdownValue) {
var grd = $("#users").data("kendoGrid");
//Set url property of the grid data source
grd.dataSource.transport.options.read.url = '/Controller/JSONMethodName?ParameterName='+ RoleID;
//Read data source to update
grd.dataSource.read();
}
Maybe you can do this as follow:
$("#dept").kendoComboBox({
filter: "contains",
suggest: true,
index: 0,
dataTextField: "Name",
dataValueField: "ID",
dataSource: data,
change: function(e){
grid.data("kendoGrid").dataSource.filter({
field: "someField",
operator: "eq|etc.",
value: this.value()
});
}
});
grid is the object you defined by kendoGrid() method.
hope it will help you.