Kendo Autocomplete is not displaying json dataset results - proxy

I am using kendoautocomplete using json method, where my json method returns DataSet results. below is code.
$(document).ready(function () {
$("#autocomplete").kendoAutoComplete({
minLength: 3,
dataValueField: "Id",
dataTextField: 'Text',
dataSource: {
transport: {
read: {
url: "/Home/getPostcodeData",
serverPaging: true,
serverFiltering: true,
pageSize: 20,
contentType: 'application/json; charset=utf-8',
type: 'GET',
dataType: 'json'
}
}
}
})
return Json(dataSet, JsonRequestBehavior.AllowGet);

Why don't you use combobox instead of autocomplete since you have dataValueField?
Here is my code example and everything works fine.
HTML
<input id="combobox" name="combobox" type="text"/>
Javascript
$(document).ready(function() {
$("#combobox").kendoComboBox({
placeholder: "Select Value",
dataValueField: 'Id',
dataTextField: 'Text',
filter: "startswith",
dataSource: {
transport: {
read: {
url: "#Url.Action("Test","Home")",
serverPaging: true,
serverFiltering: true,
pageSize: 20,
contentType: 'application/json; charset=utf-8',
type: 'GET',
dataType: 'json'
}
}
}
})
});
C#
public JsonResult Test()
{
var test = new List<TestModel>
{
new TestModel(){ Id = 1, Text = "Text 1" },
new TestModel(){ Id = 2, Text = "Text 2" },
new TestModel(){ Id = 3, Text = "Text 3" },
new TestModel(){ Id = 4, Text = "Text 4" },
new TestModel(){ Id = 5, Text = "Text 5" },
};
return Json(test, JsonRequestBehavior.AllowGet);
}

Related

Kendo-UI autocomplete fails to load

I am having an issue with the data source not being accessed. The webservice executes it's query and firebug shows the return string but I don't get the features of the autocomplete list.
$("#txtCriteria").kendoAutoComplete({
minLength: 1,
suggest: true,
filter: "startswith",
dataTextField: "ACName",
select: function (e) {
var dataItem = this.dataItem(e.item.index());
//output selected dataItem
document.getElementsByName("hdfldSelect")[0].value = dataItem.ACCode;
$("#txtCriteria").kendoAutoComplete();
var autocomplete = $("#txtCriteria").data("kendoAutoComplete");
autocomplete.destroy();
},
dataSource: new kendo.data.DataSource({
serverFiltering: true,
transport: {
read: {
url: "../DAL/Reports/wsReports.asmx/AutoComplete",
dataType: "json",
type: "GET",
},
parameterMap: function (data, action) {
var newParams = {
Type: Type,
filter: data.filter.filters[0].value
};//var
return newParams;
},//parameter
}//trans2
})//data
});
Thank you for any assistance
Going along the fact that your endpoint returns the expected data set, you could try adding a 'schema' to your kendo-datasource.
dataSource: new kendo.data.DataSource({
schema: {
data: function (e) {
return e.Results
},
model: {
fields: {
Id: { type: "number" },
Name: { type: "string" }
}
}
},
serverFiltering: true,
transport: {
read: {
url: "../DAL/Reports/wsReports.asmx/AutoComplete",
dataType: "json",
type: "GET",
},
parameterMap: function (data, action) {
var newParams = {
Type: Type,
filter: data.filter.filters[0].value
};//var
return newParams;
},//parameter
}//trans2
})//data

Kendo UI call custom service for delete

How can I call custom service for deleting a row in Kendo UI grid.This custon service should have a confirmation dialog option that can be customized.
Any thoughts on this is highly appreciative
You should customize the kendo.data.DataSource 'destroy' attribute to specify the URL and update the kendoGrid 'delete' command code for the custom confirmation.
Example code
$(document).ready(function () {
var windowTemplate = kendo.template($("#windowTemplate").html());
var crudServiceBaseUrl = "http://demos.kendoui.com/service",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "/Products",
dataType: "jsonp"
},
update: {
url: crudServiceBaseUrl + "/Products/Update",
dataType: "jsonp"
},
destroy: {
url: crudServiceBaseUrl + "/Products/Destroy",
dataType: "jsonp"
},
create: {
url: crudServiceBaseUrl + "/Products/Create",
dataType: "jsonp"
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
id: "ProductID",
fields: {
ProductID: { editable: false, nullable: true },
ProductName: { validation: { required: true } },
UnitPrice: { type: "number", validation: { required: true, min: 1} },
Discontinued: { type: "boolean" },
UnitsInStock: { type: "number", validation: { min: 0, required: true } }
}
}
}
});
var window = $("#window").kendoWindow({
title: "Are you sure you want to delete this record?",
visible: false, //the window will not appear before its .open method is called
width: "400px",
height: "200px",
}).data("kendoWindow");
var grid = $("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 430,
toolbar: ["create"],
columns: [
"ProductName",
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}"},
{ field: "UnitsInStock", title:"Units In Stock"},
{ field: "Discontinued"},
{ command: [
{name: "edit"},
{name: "Delete",
click: function(e){ //add a click event listener on the delete button
var tr = $(e.target).closest("tr"); //get the row for deletion
var data = this.dataItem(tr); //get the row data so it can be referred later
window.content(windowTemplate(data)); //send the row data object to the template and render it
window.open().center();
$("#yesButton").click(function(){
grid.dataSource.remove(data) //prepare a "destroy" request
grid.dataSource.sync() //actually send the request (might be ommited if the autoSync option is enabled in the dataSource)
window.close();
})
$("#noButton").click(function(){
window.close();
})
}
}
]}],
editable: {
mode: "inline"
}
}).data("kendoGrid");
});
Taken from:
http://docs.telerik.com/kendo-ui/web/grid/how-to/Editing/custom-delete-confirmation-dialog
You can set the delete option in your datasource to a function and do anything you want from that point. Without a sample of your service, I can't help you getting started but I can link you to the documentation.

Why is Kendo dropDownList undefined

I want to populate a Kendo dropdownlist with data from a database.
That's what I call the "frwMantenimientoEmpresas.aspx / SelectProvincias" method.
My problem is that the value "undefined" after making the call, in the dropdownlist appears ...
dataSource1 = new kendo.data.DataSource({
serverFiltering: true,
transport: {
read: {
beforeSend: function (req) {
//alert("");
},
async: false,
url: "frwMantenimientoEmpresas.aspx/SelectProvincias",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8"
}
},
schema: {
model: {
fields: {
UIDProvincia: { type: "int" },
Nombre: { type: "string" }
}
}
}
});
$("#dropProv").kendoDropDownList({
dataSource:dataSource1,
dataTextField: "Nombre",
dataValueField: "UIDProvincia",
autoBind: true,
});
The read method calls the server SelectProvincias method correctly..
At this point dropDownList = undefined??

Kendo Listview datasource issue

I am working on kendo ui listvie with paging. In this everytime I move to next page it will call datasource. In my scenario if I select 4th page it is calling datasource 4times times.
below is the code
function InitiateContactList() {
var RouteDataSource = null;
RouteDataSource = new kendo.data.DataSource({
serverPaging: true,
type: "aspnetmvc-ajax",
create: {
contentType: "application/json"
},
transport: {
read: {
url: '#Url.Content("~/Partner/GetPartnerContacts")',
data: { lPartnerId: $("#hdnPartnerId").val() },
dataType: "json",
type: "get",
}
},
serverFiltering: true,
pageSize: 2,
schema: {
data: "Data",
total: "Total"
}
});
$("#copyRoutelistdata").kendoListView({
autoBind: false,
dataSource: RouteDataSource,
template: kendo.template($("#Contactstemplate").html()),
//selectable: "single",
//pageable: true,
change: function (e) {
var index = this.select().index();
dataItem = this.dataSource.view()[index];
if (dataItem != null && dataItem.RouteId != null) {
//CopyRoute(dataItem.RouteId);
//HideWindow('MdCopyRouteSearch');
}
},
dataBound: function (e) {
$("#RoutelistPager").kendoPager({
autoBind:false,
dataSource: RouteDataSource
});
}
});
RouteDataSource.read();
}
Thanks in advance.

JSON to Kendo Grid

I want to feed some json data to my Kendo Grid. I'm taking the html route.
Here's what I have:
Given the following:
<div id="#grid"></div>
JSON:
"{\"Columns\":{\"Column\":[{\"#Name\":\"key1\",\"#DataType\":\"Boolean\",\"#text\":\"True\"},{\"#Name\":\"key2\",\"#DataType\":\"String\",\"#text\":\"Hello
World\"},{\"#Name\":\"key3\",\"#DataType\":\"Integer\",\"#text\":\"999\"}]}}"
xml version of the JSON:
<Columns>
<Column Name=""key1"" DataType=""Boolean"">True</Column>
<Column Name=""key2"" DataType=""String"">Hello World</Column>
<Column Name=""key3"" DataType=""Integer"">999</Column>
</Columns>
My failed JavaScript attempt:
$("#grid").kendoGrid({
sortable: true,
groupable: true,
scrollable: true,
height: "300px",
pageable: {
pageSizes: 9
},
columns:
[
{ field: "Name" },
],
dataSource:
{
transport:
{
read:
{
url: "/controller/action?param=" + myParam,
dataType: "jsonp"
}
}
},
schema:
{
data: "Column"
}
});
As you are expecting json data so you must use dataType:"json" instead of dataType:"jsonp"
now to read data in appropriate format you may use read function as below
transport: {
read: function(options) {
$.ajax( {
url: "/controller/action?param=" + myParam,
dataType: "json",
success: function(result) {
options.success(result.Columns.Column);
}
});
},
i hope this will help you
Abbas's answer is perfect; except, 'result' should be parsed as json:
var jResult = $.parseJSON(result);
Here's my test data:
public string ValidationResult()
{
var doc = new XmlDocument();
var xml = #"
<dataset>
<table>
<fname>a1</fname>
<age>aa1</age>
</table>
<table>
<fname>a2</fname>
<age>aa2</age>
</table>
<table>
<fname>a3</fname>
<age>aa3</age>
</table>
</dataset>";
;
doc.LoadXml(xml);
return JsonConvert.SerializeXmlNode(doc);
}
As per Abbas's answer here's the full version of binding the grid to the above xml->json resultset:
$("#grid").kendoGrid({
sortable: true,
groupable: true,
scrollable: true,
height: "600px",
pageable: {
pageSizes: 9
},
columns:
[
{ field: 'fname' },
{ field: 'age' }
],
dataSource:
{
transport:
{
read: function (options)
{
$.ajax(
{
url: "/Controller/Action?param=" + paramVal,
dataType: "json",
success: function (result)
{
var jResult = $.parseJSON(result);
options.success(jResult.dataset.table);
}
});
}
}
},
});

Resources