کار با Kendo UI DataSource
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۸/۱۵ ۲۲:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
<script type="text/javascript">
$(function () {
var cars = [
{ "Year": 2000, "Make": "Hyundai", "Model": "Elantra" },
{ "Year": 2001, "Make": "Hyundai", "Model": "Sonata" },
{ "Year": 2002, "Make": "Toyota", "Model": "Corolla" },
{ "Year": 2003, "Make": "Toyota", "Model": "Yaris" },
{ "Year": 2004, "Make": "Honda", "Model": "CRV" },
{ "Year": 2005, "Make": "Honda", "Model": "Accord" },
{ "Year": 2000, "Make": "Honda", "Model": "Accord" },
{ "Year": 2002, "Make": "Kia", "Model": "Sedona" },
{ "Year": 2004, "Make": "Fiat", "Model": "One" },
{ "Year": 2005, "Make": "BMW", "Model": "M3" },
{ "Year": 2008, "Make": "BMW", "Model": "X5" }
];
var carsDataSource = new kendo.data.DataSource({
data: cars
});
carsDataSource.read();
alert(carsDataSource.total());
});
</script> <script type="text/javascript">
$(function () {
var twitterDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "http://search.twitter.com/search.json",
dataType: "jsonp",
contentType: 'application/json; charset=utf-8',
type: 'GET',
data: { q: "#kendoui" }
},
schema: { data: "results" }
},
error: function (e) {
alert(e.errorThrown.stack);
}
});
});
</script> <script type="text/javascript">
var moviesDataSource = new kendo.data.DataSource({
type: "odata",
transport: {
read: "http://demos.kendoui.com/service/Northwind.svc/Orders"
},
error: function (e) {
alert(e.errorThrown.stack);
}
});
});
</script> using System.Collections.Generic;
using System.Web.Http;
namespace KendoUI02
{
public class Product
{
public int Id { set; get; }
public string Name { set; get; }
}
public class ProductsController : ApiController
{
public IEnumerable<Product> Get()
{
var products = new List<Product>();
for (var i = 1; i <= 100; i++)
{
products.Add(new Product { Id = i, Name = "Product " + i });
}
return products;
}
}
} using System;
using System.Web.Http;
using System.Web.Routing;
namespace KendoUI02
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
} <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Kendo UI: Implemeting the Grid</title>
<link href="styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
<link href="styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/kendo.all.min.js" type="text/javascript"></script>
</head>
<body>
<div id="report-grid"></div>
<script type="text/javascript">
$(function () {
var productsDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "api/products",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET'
}
},
error: function (e) {
alert(e.errorThrown.stack);
},
pageSize: 5,
sort: { field: "Id", dir: "desc" }
});
$("#report-grid").kendoGrid({
dataSource: productsDataSource,
autoBind: true,
scrollable: false,
pageable: true,
sortable: true,
columns: [
{ field: "Id", title: "#" },
{ field: "Name", title: "Product" }
]
});
});
</script>
</body>
</html>
@Url.Action("method_name", "Home") '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "products" })' routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); <script type="text/javascript">
$(function () {
// var r = "12";
var productsDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "@Url.Action("GetProducts", "Home")",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET',
data: { param1: "dfvdf", param2: "val2" } // ارسال اطلاعات اضافی و سفارشی به سرور در حین درخواست
},
create: {
url: "@Url.Action("PostProduct","Home")",
contentType: 'application/json; charset=utf-8',
type: "POST"
},
update: {
url:// function (product) {
"@Url.Action("UpdateProduct","Home")",//, +product.Id;
//},
contentType: 'application/json; charset=utf-8',
type: "PUT"
},
destroy: {
url: function (p) {
return "@Url.Action("DeleteProduct","Home")/" + p.Id;
},
contentType: 'application/json; charset=utf-8',
type: "DELETE"
},
parameterMap: function (options) {
return kendo.stringify(options);
}
},
schema: {
parse: function (data) {
return data;
},
data: "Data",
total: "Total",
model: {
id: "Id", // define the model of the data source. Required for validation and property types.
fields: {
"Id": { type: "number", editable: false }, //تعیین نوع فیلد برای جستجوی پویا مهم است
"Name": { type: "string", validation: { required: true }, editable: true },
"Discription": { type: "string", },
"Title": { type: "string", editable: false },
"GroupName": { type: "string", },
"Link": { type: "string" }
}
},
batch: false,
},
error: function (e) {
alert(e.errorThrown.stack);
},
pageSize: 5,
sort: { field: "Id", dir: "desc" }
});
$("#report-grid").kendoGrid({
dataSource: productsDataSource,
autoBind: true,
scrollable: false,
pageable: true,
sortable: true,
columns: [
{ field: "Id", title: "#" },
{ field: "Name", title: "Product" }
]
});
});
</script> {"param1":"val1","param2":"val2","take":10,"skip":0,"page":1,"pageSize":10,"sort":[{"field":"Id","dir":"desc"}]} // با ارث بری، خواص اضافی و سفارشی را به کلاس پایه اضافه میکنیم
public class CustomDataSourceRequest : DataSourceRequest
{
public string Param1 { set; get; }
public string Param2 { set; get; }
} var request = JsonConvert.DeserializeObject<CustomDataSourceRequest>(queryString);
$("#report-grid").kendoGrid $(...).kendoGrid is not a function
DatabaseContext db = new DatabaseContext();
[HttpGet]
public IEnumerable<News> Get()
{
db.Configuration.ProxyCreationEnabled = false;
var lst = db.Newses.ToList();
return lst;
} DatabaseContext db = new DatabaseContext();
[HttpGet]
public IEnumerable<News> Get()
{
db.Configuration.ProxyCreationEnabled = false;
var lst = db.Newses.Include("Types").ToList();
return lst;
} "address":{
"street":"test 59",
"city":"City test",
"post_number":"25050"
}, columns : [
{ field: "address.street", title: "Street" },
{ field: "address.city", title: "City" },
{ field: "address.post_number", title: "Post#" }
]
[DisplayName("تاریخ درج")]
public DateTime CreateDate { get; set; }
[DisplayName("تاریخ انتشار")]
public DateTime PublishDate { get; set; } [DisplayName("نوع مطلب")]
public string BlogType { get; set; } <script type="text/javascript">
$(function () {
var blogDataSource = new kendo.data.DataSource({
transport: {
read: {
url: "@Url.Action("GetLastBlogs", "Admin")",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET'
},
parameterMap: function (options) {
return kendo.stringify(options);
}
},
schema: {
data: "data",
total: "total",
model: {
fields: {
"id": { type: "number" }, //تعیین نوع فیلد برای جستجوی پویا مهم است
"title": { type: "string" },
"content": { type: "string" },
"imagepath": { type: "string" },
"attachmentid": { type: "number" },
"createdate": { type: "string" },
"publishdate": { type: "date" },
"blogtype": { type: "string" },
"lastmodified": { type: "date" },
"visitcount": { type: "number" },
"isarchived": { type: "boolean" },
"ispublished": { type: "boolean" },
"isdeleted": { type: "boolean" },
"tags": { type: "string" }
}
}
},
error: function (e) {
alert(e.errorThrown);
},
pageSize: 10,
sort: { field: "id", dir: "desc" },
serverPaging: true,
serverFiltering: true,
serverSorting: true
});
$("#report-grid").kendoGrid({
dataSource: blogDataSource,
autoBind: true,
scrollable: false,
pageable: true,
sortable: true,
filterable: true,
reorderable: true,
columnMenu: true,
columns: [
{ field: "id", title: "شماره", width: "130px" },
{ field: "title", title: "عنوان مطلب" },
{ field: "createdate", title: "تاریخ درج" },
{ field: "publishdate", title: "تاریخ انتشار" },
{ field: "content", title: "خلاصه مطلب" },
{ field: "blogtype", title: "نوع" },
{ field: "lastmodified", title: "آخرین ویرایش" },
{ field: "visitcount", title: "تعداد بازدید" },
{
field: "isarchived", title: "آرشیو شده",
template: '<input type="checkbox" #= isarchived ? checked="checked" : "" # disabled="disabled" ></input>'
},
{
field: "ispublished", title: "منتشر شده",
template: '<input type="checkbox" #= ispublished ? checked="checked" : "" # disabled="disabled" ></input>'
}
]
});
});
</script>