Issue with expanding and selecting the first node of kendo tree view - kendo-ui

I have a kendo tree view which is populated by a hierarchical data source. I want to set the parent node selected and expanded on load of the tree view. I have written the following expand and select to do so. But it is not working .Could anyone provide any solution?
var treeData = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: NsMenuMaster.urls.getMenuTreeUrl,
data: data,
datatype: "json",
type: "POST"
}
},
schema: {
model: {
id: "MenuConfigUid",
children: "Child"
}
}
});
$("#MenuTreelist").kendoTreeView({
dataSource: treeData,
dataTextField: ["MenuText"],
dataValueField: ["MenuConfigUid"],
height: 1000,
template: kendo.template($("#treeview-template").html()),
checkboxes: {
template: "<input type='checkbox' name='StudentClassID' value='#= item.id #' />",
checkChildren: true
},
select: NsMenuMaster.onSelect
});
$("#MenuTreelist").getKendoTreeView().select(".k-first");
$("#MenuTreelist").getKendoTreeView().expand(".k-first");

The place where you have the .select() and .expand() code is in the wrong spot. At the point at which you are currently calling them, there are not yet any nodes in the tree as the remote dataSource has not yet been populated.
You have to move your select and expand code to after the remote dataSource is read and the .k-first node exists.
You can do this by binding to the first dataBound event of TreeView, i.e:
$("#MenuTreelist").kendoTreeView({
....
});
var treeView = $("#MenuTreelist").getKendoTreeView();
treeView.one("dataBound", function () {
treeView.select(".k-first");
treeView.expand(".k-first");
});
Example: http://dojo.telerik.com/#Stephen/iYIBo

Related

Creating a new item in Grid ComboBox editor template

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?

Setting dataTextField value in Kendo UI kendoComboBox object

I have a Kendo UI combobox object something like this :
widget: "kendoComboBox",
options: {
dataTextField: "#:userFirstName#&nbsp#:userLastName#",
dataValueField: "userId",
template: "#:userFirstName#&nbsp#: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#&nbsp#: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/

KendoUI Combobox Configure for Complex Objects

I am binding the Combobox to a complex Object, the binding is such that ID field is available as a direct property on this object but the Text Property is coming from a child objects property.
I have been able to configure it show the values correctly, but running into problem to specify the optionLabel saying "select" not able to specify Parent.Childproperty getting runtime error (Uncaught TypeError: Cannot read property 'Childproperty' of undefined )
How can i specify Complex Objects in the Model defination and below for the empty selection ?
$('<input id="DegreeDDL" name="' + options.field + '"/>').appendTo(container).kendoDropDownList({
autoBind: true,
serverFiltering: true,
optionLabel: {
'Parent.Childproperty': "--- Select ---",
ABCD_PK: null
},
dataSource: {
transport: {
read: {
url: function (e) {
return "api/Org/XXXXXXXX?abcdPK=" + efgh;
},
dataType: "json" // <-- The default was "jsonp"
}
},
},
dataTextField: "Parent.ChildProperty",
dataValueField: "ABCD_PK"
});
Also running into similar propblem when defining model for the grid
var model = {
id: "ABCD_PK",
fields: {
Parent.Child.ChilProperty:
}
}
To answer your first question: use optionLabel as string if creating object here causes errors:
optionLabel: "--- Select ---",
Here is working JSFiddle: http://jsfiddle.net/a6Ek2/11/
To answer your second question just use dataSource.schema to parse your json for non complex object. More in this topic: How can I use nested Json to populate Kendo UI grid?. Grid official do not working with complex data objects. But if you wanna give a try you delclare only parent object in model eg:
fields: {
ABCD_PK: { editable: false },
Parent: { editable: true },
}
If you still got problem with this just update this JSFiddle and show exacly where this is. I'll try improve my answers then.

Nothing found in Kendo UI Combobox

Is there any way to notify user that were nothing found by his search query? Something like in JIRA comboboxes. >
http://i.stack.imgur.com/rKsGa.png
There is nothing integrated, but you can easily build this yourself.
See this jsFiddle for a demo.
Basically, what's happening is:
Return from your server. if there wasn't something found, a dummy entry with a special id.
Register the Select-Event on the ComboBox.
In the event, check to see if the selected item has your special id, and if yes, cancel the event with e.preventDefault()
Code:
$('input').kendoComboBox({
dataTextField: 'text',
dataValueField: 'id',
dataSource: {
transport: {
read: function(options) {
//instead, specify ajax call!
options.success([{ id: -1, text: 'No Matches...' }]);
}
}
},
placeholder: "Select...",
select: function(e) {
var dataItem = this.dataItem(e.item.index());
if(dataItem.id === -1) {
e.preventDefault();
}
}
});

kendo grid delete command not working

i have developed a web application using kendo ui tools and theres a kendo grid with batch edit mode..
but when i press the delete button for any record in kendo grid it will erase from the list in grid but actually not in the data source.when i reload the page or grid the deleted item will still exist..
here is the code of my grid
<div id="grid">
</div>
<script type="text/javascript">
$("#submitMarketUser").click(function () {
var grid = $("#grid").data("kendoGrid");
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: "WholeSaleTrade/GetTradeProductDetail",
dataType: "json",
data: {
test: $("#Names").val()
}
},
destroy: {
url: "WholeSaleTrade/DeletePro",
type: "POST",
dataType: "jsonp",
data: {
DAKy: $("#Names").val(),
DIKy: $("#btntxt").val()
}
},
create: {
url: "WholeSaleTrade/CreateProduct",
type: "POST",
dataType: "jsonp",
data: {
AKy: $("#Names").val(),
IKy: $("#btntxt").val()
}
}
},
pageSize: 5,
schema: {
model: {
id: "ProductKey",
fields: {
ProductKey: { editable: false, nullable: true },
ProductName: { validation: { required: true} }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
editable: true,
toolbar: ["create", "save"],
autobind: true,
pageable: true,
columns: [
{ field: "ProductName", title: "Product Name",
editor: function (container, options) {
var model = options.model;
$('<input id="btntxt" name="' + options.field + '"/>').appendTo(container).kendoComboBox({
dataSource: {
type: "POST",
transport: {
read: {
url: "MarketInformation/PopulateProducts",
success: function (data) {
var prod = data[0];
model.set("ProductName", prod.ItmNm);
model.set("ItmKy", prod.ItmKy);
model.set("UserKey", $("#Names").val());
}
}
}
},
dataValueField: "ItmKy",
dataTextField: "ItmNm"
});
}
},
{ command: ["destroy"], title: " " }
]
});
});
</script>
can not identify that where is the fault going and can somebody please help me to solve this matter.
There are three common reasons delete won't work:
1. Not setting editable of grid to inline or popup. The deleted items will be automatically processed through transport destroy only for "inline"/"popup" edit modes. Ex:
editable: {
mode: "inline",
}
//or
editable: "inline"
2. If on your datasource, you have the batch flag set to true, this means the datasource will make the call only after you tell it to, e.g calling sync(). Ex:
var dataSource = new kendo.data.DataSource({
batch: true,
//.....
});
//... in some where e.g in a save button click event call the following line:
dataSource.sync();
3. You should define id to your primary key of database field name inside model of datasource. Ex:
model: {
id: "ProductID",
fields: {
ProductID: { editable: false, nullable: true },
}
}
So the problem with your code is first one, i.e you did not set editable to inline or popup
If you choose not to include editable.mode in order to utilize the in-cell editing, you can set the toolbar of the grid to include the option save:
$("#grid").kendoGrid({
dataSource: {
transport: {
....
},
schema: {
....
}
},
toolbar: ["create", "save", "cancel"],
columns: [
....
],
editable: true
});
This will create a save button at the toolbar of the grid. After deleting any records by clicking the destroy command button, click on the save button to have the grid to make an Ajax call to the server to delete the record.
If you would rather delete the record automatically without including the save button, you could add a change event handler to the datasource of the grid:
$("#grid").kendoGrid({
dataSource: {
transport: {
....
},
schema: {
....
},
change: function(e) {
if (e.action === "remove") {
this.sync();
}
}
},
columns: [
....
],
editable: true
});
This will automatically sync the changes you made to the grid with the server when there's a data change.
Hmm try not including type: "POST", and see if it now works since as far as I can see that bit isn't included on the demo's and I don't think I included it when I last did inline edits/deletes.
I had put an arbitray name for an int on the server Delete Method.
[HttpPost]
public ActionResult DeleteRandomTest(Int32 randomTestId)
{
...
}
The default modelbinder was probably looking for a property called Id (same as the primary key of my type according to the configuration of the model).
.Model(config => config.Id(p => p.Id))
In fact, I proved this by changing the signature to the following:
[HttpPost]
public ActionResult DeleteRandomTest(Int32 Id)
{
...
}
My break point was hit after that.
Ultimately, I used the full type as the parameter as shown in the Kendo examples because I didn't want to have poorly named parameter names (not camel case) in the action. Shown as follows:
[HttpPost]
public ActionResult DeleteRandomTest([DataSourceRequest]
DataSourceRequest request, RandomDrugTest randomDrugTest)
{
...
}
This seems to the be the reason it wasn't working.
I had the same issue. My issue was caused by having a data property in the kendo model. Example:
{id: 1, data: ""}

Resources