Kendo UI grid not calling the webservice - kendo-ui

I am new to Kendo UI, I have a grid which I populate from a asmx webservice in JSON format, it displays the data fine. I have enabled editing, but when I click the update button nothing happens, I am not getting to the breakpoint in my webmethod.
Here is the relevant code snipets.
Thanks
//references
<link href="Content/kendo/2012.3.1114/kendo.common.min.css" rel="Stylesheet" />
<link href="Content/kendo/2012.3.1114/kendo.default.min.css" rel="Stylesheet" />
<script src="Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script src="Scripts/kendo/2012.3.1114/kendo.web.min.js" type="text/javascript"></script>
//Data Model Classes
[Serializable]
public class Make
{
public int PhoneMakeID { get; set; }
public string PhoneMakeDesc { get; set; }
public string BillingDesc { get; set; }
}
//Web Methods
//Phone Make Get that populates the grid, works 100%
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Make> GetPhoneMakes(string Active, string OfferTypeID, string TrackMonthID, string NetworkID)
{
DataTable dtPhoneMakes = new DataTable();
Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("PricingConnection");
DbCommand dbCommand = null;
dbCommand = db.GetStoredProcCommand("uspPS_PhoneMake_Get");
db.AddInParameter(dbCommand, "#OfferTypeID", DbType.String, OfferTypeID);
db.AddInParameter(dbCommand, "#TrackMonthID", DbType.String, TrackMonthID);
db.AddInParameter(dbCommand, "#NetworkID", DbType.String, NetworkID);
db.AddInParameter(dbCommand, "#Active", DbType.String, Active);
dtPhoneMakes = db.ExecuteDataSet(dbCommand).Tables[0];
List<Make> ml = new List<Make>();
Make m;
foreach (DataRow dr in dtPhoneMakes.Rows)
{
m = new Make();
m.PhoneMakeID = Convert.ToInt32(dr["PhoneMakeID"].ToString());
m.PhoneMakeDesc = dr["PhoneMakeDesc"].ToString();
m.BillingDesc = dr["BillingDesc"].ToString();
ml.Add(m);
}
return ml;
}
//Phone Make Update, not getting to here
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void UpdatePhoneMakes(string PhoneMakeID, string PhoneMakeDesc, string BillingDesc)
{
Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("PricingConnection");
DbCommand dbCommand = null;
dbCommand = db.GetStoredProcCommand("uspPS_PhoneMake_Update");
db.AddInParameter(dbCommand, "#PhoneMakeID", DbType.String, PhoneMakeID);
db.AddInParameter(dbCommand, "#PhoneMakeDesc", DbType.String, PhoneMakeDesc);
db.AddInParameter(dbCommand, "#BillingDesc", DbType.String, BillingDesc);
db.ExecuteNonQuery(dbCommand);
}
//Transports
var transpPhoneMake = {
read: function (options) {
$.ajax({
type: "POST",
url: "webmethods/phones.asmx/GetPhoneMakes",
contentType: "application/json; charset=utf-8",
data: jdPhoneMake,
dataType: 'json',
success: function (msg) {
options.success(msg);
}
});
},
update: function (options) {
url: "webmethods/phones.asmx/UpdatePhoneMakes/";
contentType: "application/json; charset=utf-8";
type: "POST";
},
parameterMap: function (data, operation) {
if (operation != "read") {
return JSON.stringify({ products: data.models })
}
else {
data = $.extend({ sort: null, filter: null }, data);
return JSON.stringify(data);
}
}
};
//DataSources
var dsPhoneMake = new kendo.data.DataSource({
transport: transpPhoneMake,
schema:
{
data: "d",
total: "d.length",
model:
{
id: "PhoneMakeID",
fields:
{
PhoneMakeID: { type: "string", editable: false, nullable: true },
PhoneMakeDesc: { type: "string", editable: true },
BillingDesc: { type: "string", editable: true }
}
}
},
pageSize: 10,
sort:
{
field: "PhoneMakeDesc",
dir: "asc"
}
});
//Grids
var gridPhoneMakes = $("#gridPhoneMakes").kendoGrid({
dataSource: dsPhoneMake,
columns:
[
{ command: ["edit", "destroy"], title: " ", width: "175px" },
{
field: "PhoneMakeID",
title: "Make ID",
filterable: false,
editable: false,
width: 75
},
{
field: "PhoneMakeDesc",
title: "Make Description",
editable: true
},
{
field: "BillingDesc",
title: "Make Code",
editable: true
}
],
toolbar: kendo.template($("#tmplPhoneMakeToolbar").html()),
selectable: "row",
sortable: true,
groupable: true,
pageable: true,
filterable: true,
autoBind: false,
editable: "inline",
batch: false
});

Your transport definition says:
update: function (options) {
url: "webmethods/phones.asmx/UpdatePhoneMakes/";
contentType: "application/json; charset=utf-8";
type: "POST";
},
When you are actually not defining a function. It should be something like:
update: {
url: "webmethods/phones.asmx/UpdatePhoneMakes/",
contentType: "application/json; charset=utf-8",
type: "POST",
},
But all transport definitions must be consistent (Excerpt from KendoUI documentation)
Note: transport methods should be consistent - create, update and
destroy should be specified as functions too
(Thanks #LindsySimon for the remark)
You should do as you did for read:
read: function (options) {
$.ajax({
url: "webmethods/phones.asmx/UpdatePhoneMakes/",
contentType: "application/json; charset=utf-8",
type: "POST",
success: function (result) {
...
}
});
}

Related

Kendo UI posting null values to paramter in web api controller method

I am using the following code in cshtml,
Controller display null value in parameter,
I require values to be populated in controller method parameter,
Please help
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var crudServiceBaseUrl = "http://localhost:49885/api",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "/Suppliers",
dataType: "json",
type: "Get"
},
update: {
url: function(data) {
debugger;
//return crudServiceBaseUrl + "/Suppliers/" + JSON.stringify(data.models[0]);
return crudServiceBaseUrl + "/Suppliers";
},
dataType: "json",
type: "Put",
contentType: "application/json"
},
destroy: {
url: function(data) {
debugger;
return crudServiceBaseUrl + "/Suppliers/" + JSON.stringify(data.SupplierId);
//return crudServiceBaseUrl + "/Suppliers";
},
//url: crudServiceBaseUrl + "/Suppliers",
dataType: "json",
type: "Delete",
contentType: "application/json"
},
create: {
url: function (data) {
debugger;
//return crudServiceBaseUrl + "/Suppliers/" + JSON.stringify(data);
return crudServiceBaseUrl + "/Suppliers";
},
//url: crudServiceBaseUrl + "/Suppliers",
dataType: "json",
type: "Post",
contentType: "application/json",
//data: JSON.stringify([data.SupplierId, data.CommId])
},
parameterMap: function(options, operation) {
debugger;
if (operation !== "read" && options) {
return { models: JSON.stringify(options) };
}
}
},
batch: false,
pageSize: 20,
schema: {
data: function(data) { //specify the array that contains the data
return data || [];
},
errors: function(response) {
return response.error;
},
model: {
id: "SupplierId",
fields: {
SupplierId: { validation: { required: true } },
CommId: { validation: { required: true } },
EmailId: { validation: { required: true } },
FullName: { validation: { required: true } },
FirstName: { validation: { required: true } },
Description: { validation: { required: true } },
LastName: { validation: { required: true } },
StateId: { validation: { required: true } }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
navigatable: true,
pageable: true,
sortable: true,
height: 550,
toolbar: ["create", "save", "cancel"],
columns: [
"SupplierId",
{ field: "SupplierId", title: "SupplierId", width: 120 },
{ field: "CommId", title: "CommId", width: 120 },
{ field: "EmailId", title: "EmailId", width: 120 },
{ field: "FullName", width: 120 },
{ field: "FirstName", width: 120 },
{ field: "Description", width: 120 },
{ field: "LastName", width: 120 },
{ field: "StateId", width: 120 },
{ command: "destroy", title: " ", width: 150 }
],
editable: true
});
});
</script>
<div id="example">
<div id="grid"></div>
</div>

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

Why can't I update or delete the new import of excel data in kendo grid?

I import the Excel file to server and the server return the JSON data to client. but after the new JSON data binding to the kendo grid dataSource, I can't update or delete this or these datas? What can I do to resolve this problem?
This is the form submit js code:
var form = $('#idForm');
var grid = $('#demo2').data('kendoGrid');
form.submit(function (e) {
var formData = new FormData($(this)[0]);
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: formData,
cache: false,
processData: false,
contentType: false,
dataType: 'json',
//async: false,
success: function (response) {
if (response.result) {
***grid.dataSource.data(response.data);***
$('#moneyin').text(response.context.moneyin);
$('#moneyout').text(response.context.moneyout);
$('#moneyleft').text(response.context.moneyleft);
} else {
alert(response.message);
}
},
error: function (response) {
alert("error");
}
});
//e.preventDefault(); // avoid to execute the actual submit of the form.
return false;
});
var url = site_url + 'inline_edit/';
var dataSource = new kendo.data.DataSource({
transport: {
read: {
url: site_url + 'initial/',
type: 'GET',
dataType: "json"
},
update: {
url: url,
type: 'POST',
dataType: 'json'
},
create: {
url: url,
type: 'POST',
dataType: 'json'
},
destroy: {
url: url,
type: 'POST',
dataType: 'json'
},
parameterMap: function (options, operation) {
//if (operation == "read") {
// return {
// page: options.page,
// pageSize: options.pageSize,
// start: $('#start').val(),
// end: $('#end').val()
// }
//}
if (operation !== "read" && options.models) {
for (var k = 0; k < options.models.length; k++) {
var date_str = date2str(options.models[k].date, 'yyyy-MM-dd');
var recor_str = date2str(new Date(), 'yyyy-MM-dd');
options.models[k].date = date_str;
options.models[k].recordate = recor_str;
}
return {
'operation': operation,
'models': kendo.stringify(options.models)
};
}
}
},
requestEnd: function (e) {
if (e.response && e.response.context) {
$('#moneyin').text(e.response.context.moneyin);
$('#moneyout').text(e.response.context.moneyout);
$('#moneyleft').text(e.response.context.moneyleft);
}
},
//serverPaging: true,
batch: true,
pageSize: 7,
schema: {
model: {
id: 'id',
//data type of the field {Number|String|Boolean|Date|datetime} default is String
fields: {
id: {
type: "number",
editable: false,
nullable: false
},
date: {
type: "date",
defaultValue: new Date(),
validation: {
required: true
}
},
fundbefore: {
type: "number",
editable: false,
nullable: true
},
moneyin: {
type: "number",
validation: {
min: 0,
required: true
}
},
moneyout: {
type: "number",
validation: {
min: 0,
required: true
}
},
fundafter: {
type: "number",
editable: false,
nullable: true
},
charge: {
type: "string",
validation: {
required: true
}
},
reason: {
type: 'textarea',
validation: {
required: true
}
},
recordate: {
type: 'date',
editable: false,
defaultValue: new Date()
},
modify: {
editable: false,
defaultValue: user_name
}
}
},
data: function (response) {
if (response.result) {
return response.data;
} else {
return alert(response.message);
}
},
total: function (response) {
return response.total;
}
}

How I write a dynamic URL in kendo UI DataSource like "update/{id}"

I have a web API. In that I wrote a update method. But it need to id of the table row to update to the row.
I use a grid to show data and use a toolbar to edit the row.
My question is how I pass that id to the update.
Can someone guide me ??
update: function(options) {
$.ajax( {
url: function(data) { return "updateUsuarios/"+data.Id,
dataType: "json",
.....
Well i suggest you, explain more your question, but i think this examples could help , if you have a toolbar as a template like this:
<script type="text/x-kendo-template" id="template">
<div class="toolbar">
<button type="button" id="update">Update</button>
</div>
</script>
You "grid" need the attr "toolbar"
$("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 550,
filterable:true,
toolbar: kendo.template($("#template").html()),
columns: [
{ field:"username", title: "Username" , width: "120px" },
{ field: "nombre", title:"Nombre", width: "120px" },
{ field: "apellido", title:"Apellido", width: "120px" },
{ field: "ci", title:"Documento de Identidad", width: "120px" },
{ field: "email", title:"Direccion de Correo", width: "120px" },
{ field: "activo",title:"Estatus", width: "120px" },
{ field: "fecha_caducidad",title:"Fin Demo", width: "120px",template: "#= kendo.toString(kendo.parseDate(fecha_caducidad, 'yyyy-MM-dd'), 'MM/dd/yyyy') #" },
{ field: "licencia_status",title:" ", width: "40px",template:'<img src="assets/images/#:data.licencia_status#.png"/>' },
{ command: ["edit"], title: " ", width: "120px" }],
editable: "popup",
dataBound: function () {
var rows = this.items();
$(rows).each(function () {
var index = $(this).index() + 1;
var rowLabel = $(this).find(".row-number");
$(rowLabel).html(index);
});
},
selectable: true
});
So,you can configure a kendo button and add functionality in the event click:
$("#update").kendoButton({
click: function(){
//Here you will have the selected row
var self=$('#grid').data('kendoGrid')
var index = self.items().index(self.select());
var rowActual= self.dataSource.data()[index];
rowActual=self.dataItem(self.select());
if(rowActual==undefined || rowActual ==null) {
alert("No row selected");
}else{
$.ajax({
type: "POST",
url: "update",
data: {
id:rowActual.id
},
dataType: 'json',
success: function (data) {
},
error: function(){
}
});
}
}
});
and send in the ajax the row id, but if you are update the row with the inline edition you could try with a datasource like this
dataSource = new kendo.data.DataSource({
transport: {
read: function(options) {
$.ajax( {
url: "readUsuarios",
dataType: "json",
success: function(result) {
options.success(result);
}
});
},
update: function(options) {
$.ajax( {
url: "updateUsuarios",
dataType: "json",
data: {
models: kendo.stringify(options.data.models)
},
success: function(data) {
// response server;
},
error: function(result) {
// notify the data source that the request failed
options.error(result);
}
});
}
},
batch: true,
pageSize: 20,
schema: {
model: {
id: "id",
fields: {
username: { editable: false, nullable: true },
nombre: { validation: { required: true } },
apellido: { type: "string", validation: { required: true} },
ci: { type: "string", validation: { required: true} },
email: { type: "string", validation: { required: true } },
activo: { type: "boolean",editable: false },
fecha_caducidad: { type: "date" },
licencia_status:{editable: false}
}
}
}
});
I hope this help!

Kendo Grid error on update - no database issue

I am getting this error every time i update any row.
The update itself is successful in the database, but when i trigger the success method for kendo it seems to be bothered, at the beginning i thought it could be that the object i return from the database is different, but its exactly the same one as the one i submit.
Thanks in advance
Uncaught SyntaxError: Unexpected identifier
yt.setter
lt.extend._set
Ut.extend.accept
lt.extend._accept
(anonymous function)
(anonymous function)
j
k.fireWith
e.(anonymous function)
o.(anonymous
function).call.X.success
$.ajax.success
j
k.fireWith
x
b
Here is the code:
dataSource = new kendo.data.DataSource({
transport: {
read: function(options) {
$.ajax( {
url: crudServiceBaseUrl + "?serviceType=students&serviceFunction=getStudentLists&type=2",
dataType: "json",
success: function(result) {
var data = _.flatten(result);
options.success(data);
}
});
},
update: function(options) {
$.ajax( {
type: 'POST',
url: "/tools.php?id=ajaxstudentlist",
dataType: "json",
data: {data:options.data.models},
success: function(result) {
var arr = [];
arr.push(result);
var data = _.flatten(arr);
options.success();
}
});
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
batch: true,
pageSize: 70,
schema: {
model: {
id: "studystatusid",
fields: {
studystatusid : { type: 'number', editable: false, nullable: true },
studentid : { type: 'string', editable: false, nullable: false},
lastname : { type: 'string', editable: false, },
firstname : { type: 'string', editable: false, },
spnumber : { type: 'string', editable: false, },
status : { type: 'string', editable: false, },
begin_date: { type: "date", validation: { required: true } },
end_date: { type: "date" }
}
},
parse : function(data) {
for (var i = data.length - 1; i >= 0; i--) {
data[i].studentid = addZeros(data[i].studentid);
if(data[i].begin_date !== '0'){
data[i].begin_date = new Date(parseInt(data[i].begin_date+ '000'));
}
if(data[i].end_date){
data[i].end_date = new Date(parseInt(data[i].end_date+ '000'));
}
}
return data;
}
}
});
Your Datasource Update
update: function(options) {
$.ajax( {
type: 'POST',
url: "/tools.php?id=ajaxstudentlist",
dataType: "json",
data: {data:options.data.models},
success: function(result) {
var arr = [];
arr.push(result);
var data = _.flatten(arr);
options.success();
}
});
},
the above ajax call is expecting a JSON result back , you should return a JSON result back as a success , Its Better to return the updated row back.
why are are you calling ajax inside Kendo?
Should be like the following code. Try this and update the status I will modify the code if it not get succeeded.
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "?serviceType=students&serviceFunction=getStudentLists&type=2",
dataType: "json",
success: function (data) {
console.log(data);
}
},
update: {
url: "/tools.php?id=ajaxstudentlist",
dataType: "json",
data:{inputParam:inputvalue},
success: function(result) {
console.log(data);
}
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
id: "studystatusid",
fields: {
studystatusid : { type: 'number', editable: false, nullable: true },
studentid : { type: 'string', editable: false, nullable: false},
lastname : { type: 'string', editable: false, },
firstname : { type: 'string', editable: false, },
spnumber : { type: 'string', editable: false, },
status : { type: 'string', editable: false, },
begin_date: { type: "date", validation: { required: true } },
end_date: { type: "date" }
}
}
}
});

Resources