فرمت کردن اطلاعات نمایش داده شده به کمک jqGrid در ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۴/۱۲ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace jqGrid02.Models
{
public class Product
{
public int Id { set; get; }
public string Name { set; get; }
public decimal Price { set; get; }
public Supplier Supplier { set; get; }
}
public class Supplier
{
public int Id { set; get; }
public string CompanyName { set; get; }
public string Address { set; get; }
public string PostalCode { set; get; }
public string City { set; get; }
public string Country { set; get; }
public string Phone { set; get; }
public string HomePage { set; get; }
}
} public ActionResult GetGetSupplierData(int id)
{
var list = ProductDataSource.LatestProducts;
var product = list.FirstOrDefault(x => x.Id == id);
if (product == null)
return Json(null, JsonRequestBehavior.AllowGet);
return Json(new
{
product.Supplier.CompanyName,
product.Supplier.Address,
product.Supplier.PostalCode,
product.Supplier.City,
product.Supplier.Country,
product.Supplier.Phone,
product.Supplier.HomePage
}, JsonRequestBehavior.AllowGet);
} <div dir="rtl" id="supplierDialog">
<span id="CompanyName"></span><br /><br />
<span id="Address"></span><br />
<span id="PostalCode"></span>, <span id="City"></span><br />
<span id="Country"></span><br /><br />
<span id="Phone"></span><br />
<span id="HomePage"></span>
</div> <script type="text/javascript">
function showSupplierDialog(linkElement, supplierId) {
//request json data
$.getJSON('@Url.Action("GetGetSupplierData","Home")', { id: supplierId }, function (data) {
//set values in dialog
for (var property in data) {
if (data.hasOwnProperty(property)) {
$('#' + property).text(data[property]);
}
}
//get link position
var linkPosition = $(linkElement).offset();
$('#supplierDialog').dialog('option', 'position', [linkPosition.left, linkPosition.top]);
//open dialog
$('#supplierDialog').dialog('open');
});
}
$(document).ready(function () {
$('#supplierDialog').dialog({
autoOpen: false, bgiframe: true, resizable: false, title: 'تولید کننده'
});
$('#list').jqGrid({
// .... مانند قبل
colNames: ['شماره', 'نام محصول', 'قیمت'],
//columns model
colModel: [
{
name: 'Id', index: 'Id', align: 'right', width: 20,
formatter: function (cellvalue, options, rowObject) {
var cellValueInt = parseInt(cellvalue);
if (cellValueInt == 5) {
return "<span style='background: brown; color: yellow'>" + cellvalue + "</span>";
}
return cellvalue;
}
},
{
name: 'Name', index: 'Name', align: 'right', width: 300,
formatter: function (cellvalue, options, rowObject) {
return "<a href='#' onclick='showSupplierDialog(this, " + rowObject[0] + ");'>" + cellvalue + "</a>";
}
},
{
name: 'Price', index: 'Price', align: 'center', width: 50,
formatter: 'currency',
formatoptions:
{
decimalSeparator: '.', thousandsSeparator: ',', decimalPlaces: 2, prefix: '$'
}
}
],
// .... مانند قبل
});
});
</script>