How to bind a model json to a comboBox in controller? - json

I want to bind a model json with a comboBox inside the controller (not in a xml view).
I made a model Json called types.json and I passed to the controller, but, when I bind the values inside the created model to the combobox, didn't show nothing.
Can you please help me. I can't find the problem here.
That is parts of my code, below, who I think is important to this question.
my MODEL JSON - name: types.json
"types": [
{
"text": "Férias",
"key": "01"
},
{
"text": "something",
"key": "02"
},
]
my CONTROLLER - name: Page.controller.js
handleAppointmentCreate: function (oEvent) {
var oStartDate = oEvent.getParameter("startDate"),
oEndDate = oEvent.getParameter("endDate"),
oCalendarRow = oEvent.getParameter("calendarRow"),
oEmpId = oCalendarRow.getKey(),
_oYearStartDate = oStartDate.getFullYear(),
_oMonthStartDate = oStartDate.getMonth() + 1,
_oDateStartDate = oStartDate.getDate(),
_oYearEndDate = oEndDate.getFullYear(),
_oMonthEndDate = oEndDate.getMonth() + 1,
_oDateEndDate = oEndDate.getDate(),
_HourStart = oStartDate.getHours(),
_oMinStart = oStartDate.getMinutes(),
_oSecStart = oStartDate.getSeconds(),
_oHourEnd = oEndDate.getHours(),
_oMinEnd = oEndDate.getMinutes(),
_oSecEnd = oEndDate.getSeconds(),
sStartDate = _oYearStartDate + "-" + _oMonthStartDate + "-" + _oDateStartDate,
sEndDate = _oYearEndDate + "-" + _oMonthEndDate + "-" + _oDateEndDate,
sStartHour = _HourStart + ":" + _oMinStart + ":" + _oSecStart,
sEndHour = _oHourEnd + ":" + _oMinEnd + ":" + _oSecEnd,
sIdEmp = oEmpId;
var dataModel = this.getOwnerComponent().getModel("Model");
this.getView().setModel(dataModel, "DataModel");
if (!this.oConfirmDialog) {
this.oConfirmDialog = new Dialog({
type: DialogType.Message,
title: "Novo agendamento",
content: [
new HorizontalLayout({
content: [
new VerticalLayout({
width: "120px",
content: [
new Text({ text: "Id de funcionário: " }),
new Text({ text: "Data de inicio: " }),
new Text({ text: "Data de término: " }),
new Text({ text: "Hora de inicio: " }),
new Text({ text: "Hora de término: " })
]
}),
new VerticalLayout({
content: [
new Text({text: sIdEmp }),
new Text({ text: sStartDate }),
new Text({ text: sEndDate }),
new Text({ text: sStartHour }),
new Text({ text: sEndHour })
]
})
]
}),
new TextArea("confirmationTitle", {
width: "100%",
placeholder: "Adicione o titulo do agendamento"
//required:"true" - não pode ter
}),
new TextArea("confirmationDetails", {
width: "100%",
placeholder: "Adicione detalhes do agendamento"
//required:"true" - não pode ter
}),
new sap.m.ComboBox({
items: {
path: "DataModel>/types",
template: {
Type: "sap.ui.core.ListItem",
text: "{DataModel>Key}",
enabled: true
}
}
})
],
beginButton: new Button({
type: ButtonType.Emphasized,
text: "Submeter",
press: function () {
var sIdEmp1 = sIdEmp,
sStartDate1 = sStartDate,
sEndDate1 = sEndDate,
sStartHour1 = sStartHour,
sEndHour1 = sEndHour,
sTitle = Core.byId("confirmationTitle").getValue(),
sDetails = Core.byId("confirmationDetails").getValue();
this.addAppointment(sIdEmp1, sStartDate1, sEndDate1, sStartHour1, sEndHour1, sTitle, sDetails);
this.oConfirmDialog.close();
}.bind(this)
}),
endButton: new Button({
text: "Cancelar",
press: function () {
this.oConfirmDialog.close();
}.bind(this)
})
});
}
this.oConfirmDialog.open();
},

First of all, there's a typo. In your model you have the string "key", but when creating the ComboBox you used "Key", with an uppercase K. Be sure that you put the same on both places.
If this don't solves your problem, try using sap.ui.core.Item instead of sap.ui.core.ListItem, as it is the control that the aggregation items accepts:
new sap.m.ComboBox({
items: {
path: "DataModel>/types",
template: new sap.ui.core.Item({
key: "{DataModel>key}",
text: "{DataModel>text}"
})
},
enabled: true
})
Also, remember that ComboBox is ony recomended when you will have between 13 and 200 results. For less than 13 options to show use sap.m.Select instead.

Related

Any search result returned due to a free-text search should show preview text under each row in the results tables

I am applying filter on columns in the datatable as well as on the attributes that are not in the datatable but are in present in the database of the object. The filters are working fine and I am getting the output wanted. However on the text search I want to preview the text found that is not part of the attributes in the datatable under the row (refer to the image below for better understanding of the scenario).
I have tried dynamically adding a row but it just reloads the page and shows me the same data i got from the ajax request.
var counter = 1;
$('#addRow').on('click', function () {
datatable.row.add([
counter + '.1',
counter + '.2',
counter + '.3',
counter + '.4',
counter + '.5'
]).draw();
counter++;
});
here's the code of Datatable that handles the ajax request and renders the data
let datatable = companytable.DataTable({
lengthMenu: [[10, 20, 30, -1], [10, 20, 30, "All"]],
ajax: {
url: '/api/company/get?' + "&statusFilter=" + statusFilter + "&nameFilter=" + nameFilter
+ "&isrOfficeSelected=" + isrOfficeSelected + "&industrySelected=" + industrySelected
+ "&companyTypeSelected=" + companyTypeSelected
+ "&usOfficeSelected=" + usOfficeSelected
+ "&usOfficeStateSelected=" + usOfficeStateSelected
+ "&targetMarketSelected=" + targetMarketSelected
+ "&financingStageSelected=" + financingStageSelected
+ "&officeSizeSelected=" + officeSizeSelected
+ "&tamidReferralSelected=" + tamidReferralSelected
+ "&ratingLow=" + ratingLow + "&ratingHigh=" + ratingHigh
+ "&paidInternFrom=" + paidInternFrom + "&paidInternTo=" + paidInternTo
+ "&unpaidInternFrom=" + unpaidInternFrom + "&unpaidInternTo=" + unpaidInternTo
+ "&activeInternFrom=" + activeInternFrom + "&activeInternTo=" + activeInternTo
,
type: "GET",
},
processing: true,
serverSide: true,
language: {
processing: "Loading...",
paginate: {
previous: "<",
next: ">",
first: "<<",
last: ">>"
},
lengthMenu: "_MENU_"
},
columns: [
{
data: "CompanyName",
name: "CompanyName",
render: function (data, type, row) {
if (row.CompanyName !== '') {
return $('<span />')
.append($('<a />')
.prop('href', '/Consulting/Admin/Company?id=' + row.ID).text(row.CompanyName)).html();
}
return "";
}
},
{
data: 'Department',
name: "Department",
render: function (data, type, row) {
return $("<span/>").text(row.Department).html();
}
},
{
data: 'ContactName',
name: "ContactName",
render: function (data, type, row) {
return $("<span/>").text(row.FirstName + " " + row.LastName).html();
}
},
{
data: 'Email',
name: "Email",
render: function (data, type, row) {
return $("<span/>").text(row.Email).html();
}
},
{
data: "CurrentRating",
name: "CurrentRating",
render: function (data, type, row) {
let rating = $('<span />');
rating.append($('<span class="hidden" />').text(row.CurrentRating));
for (var i = 0; i < 5; i++) {
rating.append($('<i ></i>').addClass(i < parseInt(row.CurrentRating) ? 'fa fa-star text-primary' : 'fa fa-star-o text-muted'))
}
return rating.html();
}
},
],
dom: '<"top">rt<"bottom"pl><"clear">',
pagingType: "full_numbers",
});
Image Reference of the desired output

Ajax Response Capitalizations Rules

A was using DataTables plugin, and when displaying my columns I needed to put the first letter to LowerCase for it to recognize the property/object, e.g:
// Object name is actually: EngineeringChange.Responsible
{
title: "Responsible",
data: "engineeringChange.responsible",
className: "columnName columnHeader",
},
I just assumed the first letter would always be capitalized to LowerCase. I tried creating a new property inside EngineeringChange named ECRNumber, so I tried:
{
title: "ECR Number",
data: "engineeringChange.eCRNumber",
className: "columnName columnHeader",
},
Isn't recognized as a parameter... After a bit of searching I find out the Json response I get on AJAX it's called ecrNumber. So now I'm actually lost on which are the rules that are automatically applied to the Json Response. How does it turn ecr to LowerCase and Number to UpperCase (the N)??
Edit
Sry, can´t think of any easy way to exactly reproduce my problem on a demo
TableDesign/Creation
table = $('#tblEntries2').DataTable({
order: [[0, 'desc']],
deferRender: true,
ajax: {
url: '#Url.Action("GetEntries", "AlteracaoEngenharia")',
method: 'GET',
dataSrc: '',
beforeSend: function () {
onBegin();
$content.hide();
$loader.show();
},
complete: function (jsonResponse) {
console.log(jsonResponse);
onComplete();
$loader.hide();
$content.fadeIn();
$('#ExcelExport').show();
table.column(5).visible(false);
table.column(6).visible(false);
table.column(7).visible(false);
table.column(9).visible(false);
table.column(10).visible(false);
table.column(11).visible(false);
}
},
dom: "<'row'<'col-2'l><'col-7 text-center'B><'col-3'f>>" +
"<'row'<'col-12'tr>>" +
"<'row'<'col-5'i><'col-7'p>>",
lengthMenu: [
[10, 25, 50, 100, -1],
['10', '25', '50', '100', 'Todos'],
],
columns: [
{
title: "Id (yr-id)",
data: "creationYear",
className: "columnNumber columnHeader",
},
{
title: "ECR Number",
data: "engineeringChange.ecrNumber",
className: "columnNumber columnHeader",
},
{
title: "Criador Alteração de Engenharia",
data: "engineeringChange.responsible",
className: "columnName columnHeader",
},
...
Handler
public IActionResult GetEntries()
{
GetDataEntries();
int count = 0;
int currentYear = 0;
foreach (var entry in EntriesList)
{
EngineeringChangesListViewModel h = new EngineeringChangesListViewModel
{
EngineeringChange = entry
};
if (currentYear != entry.DataCriacao.Year)
{
count = 1;
currentYear = entry.DataCriacao.Year;
}
else
{
count++;
}
h.DeadLine = entry.FinishedGood.Week + " - " + entry.DataCriacao.Year.ToString();
if (entry.OldPart == null)
{
h.EngineeringChange.OldPart = new Part();
}
if (entry.NewPart == null)
{
h.EngineeringChange.NewPart = new Part();
}
if (entry.FinishedGood == null)
{
h.EngineeringChange.FinishedGood = new FinishedGood();
}
if (entry.OldPart != null && entry.OldPart.CDP.HasValue)
h.OldPartValue = entry.OldPart.CDP * entry.OldPart.Stock;
if (entry.NewPart != null && entry.NewPart.CDP.HasValue)
h.NewPartValue = entry.NewPart.CDP * entry.NewPart.Stock;
h.EngineeringChange.ECRNumber = entry.ECRNumber;
//toString("D4") padds the number to always be 4 digits
h.CreationYear = entry.DataCriacao.Year.ToString() + "_" + count.ToString("D4");
h.IdYear = count;
EntriesListaViewModel.Add(h);
}
var errorsJson = JsonConvert.SerializeObject(EntriesListaViewModel);
Console.WriteLine(errorsJson);
return new JsonResult(EntriesListaViewModel);
}
HANDLER OUTPUT
[{"CreationYear":"2021_0001","IdYear":1,"OldPartValue":null,"NewPartValue":2061.09155,"DeadLine":"15 - 2021","EngineeringChange":{"Id":8,"DataCriacao":"2021-03-11T16:15:24.6630956","Responsible":"José António","ECRNumber":"X1232","State":"Aberto","Comment":"Teste","UserId":1,"User":null,"Component":null,"FinishedGood":{"Id":31,"Week":15,"EngineeringChangeId":8},"OldPart":{"Id":5,"Reference":"FS12848AC","Stock":null,"CDP":1.43584776,"LastExpired":null},"NewPart":{"Id":6,"Reference":"FS12848AD","Stock":1650,"CDP":1.24914646,"LastExpired":"2021-03-11T00:00:00"},"Transformation":{"Id":188,"Possible":true,"Cost":1090.0,"WillBeTransformed":true,"TransformationDate":"2021-03-13T08:48:00","Responsible":"Miguel","EngineeringChangeId":8}}},]
PAGE/AJAX OUTPUT outputs are created by console.log() and Console.WriteLine() displayed above.
ECRNumber gets named to ecrNumber...

JQGgrid- How to generate a pdf / excel file from the grid data

I have the following JQGrid table, which is being generated from JSON data. I want to be able to generate/export or create a pdf file based on the table data state that is view able. I am using JSON , Jqgrid and Javascript
How do I generate a pdf file from the data ?
Here My FIDDLE
JS CODE
$(document).ready(function() {
var jsonData = {
"Name": "Julie Brown",
"Account": "C0010",
"LoanApproved": "12/5/2015",
"LastActivity": "4/1/2016",
"PledgedPortfolio": "4012214.00875",
"MaxApprovedLoanAmt": "2050877.824375",
"LoanBalance": "1849000",
"AvailableCredit": "201877.824375",
"Aging": "3",
"Brokerage": "My Broker",
"Contact": "Robert L. Johnson",
"ContactPhone": "(212) 902-3614",
"RiskCategory": "Yellow",
"rows": [{
"ClientID": "C0010",
"Symbol": "WEC",
"Description": "Western Electric Co",
"ShareQuantity": "20638",
"SharePrice": "21.12",
"TotalValue": "435874.56",
"LTVCategory": "Equities",
"LTVRatio": "50%",
"MaxLoanAmt": "217937.28"
}, {
"ClientID": "C0010",
"Symbol": "BBB",
"Description": "Bins Breakers and Boxes",
"ShareQuantity": "9623",
"SharePrice": "74.29125",
"TotalValue": "714904.69875",
"LTVCategory": "Equities",
"LTVRatio": "50%",
"MaxLoanAmt": "357452.349375"
}, {
"ClientID": "C0010",
"Symbol": "GPSC",
"Description": "Great Plains Small Cap Stock",
"ShareQuantity": "49612",
"SharePrice": "14.24",
"TotalValue": "706474.88",
"LTVCategory": "Mutual Funds - Small Cap",
"LTVRatio": "40%",
"MaxLoanAmt": "282589.952"
}]
},
mmddyyyy = "";
/*********************************************************************/
$("#output").jqGrid({
url: "/echo/json/",
mtype: "POST",
datatype: "json",
postData: {
json: JSON.stringify(jsonData)
},
colModel: [
/** { name: 'ClientID', label:'ClientID',width: 80, key: true },****/
{
name: 'Symbol',
width: 65
}, {
name: 'Description',
width: 165
}, {
name: 'ShareQuantity',
align: 'right',
width: 85,
classes: "hidden-xs", labelClasses: "hidden-xs",
formatter: 'currency',
formatoptions: {
prefix: " ",
suffix: " "
}
}, {
name: 'SharePrice',
label: 'Share Price',
align: 'right',
width: 100,
classes: "hidden-xs", labelClasses: "hidden-xs",
template: "number",
formatoptions: {
prefix: " $",
decimalPlaces: 4
}
},
/*{ label: 'Value1',
name: 'Value1',
width: 80,
sorttype: 'number',
formatter: 'number',
align: 'right'
}, */
{
name: 'TotalValue',
label: 'Total Value',
width: 160,
sorttype: 'number',
align: "right",
search: false,
formatter: 'currency',
formatoptions: {
prefix: " $",
suffix: " "
}
}, {
name: 'LTVRatio',
label: 'LTV Ratio',
width: 70,
sorttype: 'number',
align: "right",
formatter: 'percentage',
formatoptions: {
prefix: " ",
suffix: " "
}
}, {
name: 'LTVCategory',
label: 'LTV Category',
classes: "hidden-xs", labelClasses: "hidden-xs",
width: 120,
width: 165
},
{
name: 'MaxLoanAmt',
label: 'MaxLoanAmount',
width: 165,
sorttype: 'number',
align: "right",
search: false,
formatter: 'currency',
formatoptions: {
prefix: " $",
suffix: " "
}
}
],
additionalProperties: ["Symbol", "Description"],
subGrid: true,
subGridRowExpanded: function (subgridDivId, rowid) {
var item = $(this).jqGrid("getLocalRow", rowid);
$("#" + $.jgrid.jqID(subgridDivId)).html("Symbol: <em>" + item.Symbol +
"</em><br/>Description: <em>" + item.Description + "</em>");
},
beforeProcessing: function (data) {
var symbolsMap = {}, symbolsValues = ":All", rows = data.rows, i, symbol;
for (i = 0; i < rows.length; i++) {
symbol = rows[i].Symbol;
if (!symbolsMap.hasOwnProperty(symbol)) {
symbolsMap[symbol] = 1;
symbolsValues += ";" + symbol + ":" + symbol;
}
}
$(this).jqGrid("setColProp", 'Symbol', {
stype: "select",
searchoptions: {
value: symbolsValues
}
}).jqGrid('destroyFilterToolbar')
.jqGrid('filterToolbar', {
stringResult: true,
searchOnEnter: false,
defaultSearch : "cn"
});
},
/*beforeProcessing: function (data) {
var item, i, n = data.length;
for (i = 0; i < n; i++) {
item = data[i];
item.Quantity = parseFloat($.trim(item.Quantity).replace(",", ""));
item.LTVRatio = parseFloat($.trim(item.LTVRatio *10000).replace(",", ""));
item.Value = parseFloat($.trim(item.Value).replace(",", ""));
item.Num1 = parseInt($.trim(item.Num1).replace(",", ""), 10);
item.Num2 = parseInt($.trim(item.Num2).replace(",", ""), 10);
}
}, */
iconSet: "fontAwesome",
loadonce: true,
rownumbers: true,
cmTemplate: {
autoResizable: true,
editable: true
},
autoResizing: {
compact: true
},
autowidth: true,
height: 'auto',
forceClientSorting: true,
sortname: "Symbol",
footerrow: true,
caption: "<b>Collateral Value</b> <span class='pull-right' style='margin-right:20px;'>Valuation as of: " + mmddyyyy + "</span>",
loadComplete: function() {
var $self = $(this),
sum = $self.jqGrid("getCol", "Price", false, "sum"),
sum1 = $self.jqGrid("getCol", "MaxLoanAmt", false, "sum");
//ltvratio = $self.jqGrid("getCol","LTVRatio:addas", "Aved Loan Amount");
$self.jqGrid("footerData", "set", {
LTVCategory: "Max Approved Loan Amount:",
Price: sum,
MaxLoanAmt: sum1
});
}
});
$("#output").jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "cn"});
$(window).on("resize", function () {
var newWidth = $("#output").closest(".ui-jqgrid").parent().width();
$("#output").jqGrid("setGridWidth", newWidth, true);
}).triggerHandle("resize");
});
There is no build in method to export jqgrid data to pdf. You have to use third party tools( I personally like iTextSharp which can be downloaded from Here . You have to create action method for printing the grid data in your controller.
Here is also another one example one guy made.
Here is also another example in trirand. If you see the source code they are using iTextHsarp.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JQGridMVCExamples.Models;
using Trirand.Web.Mvc;
using System.IO;
//
// For PDF export we are using the free open-source iTextSharp library.
//
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Data;
namespace JQGridMVCExamples.Controllers.Grid
{
public partial class GridController : Controller
{
// This is the default action for the View. Use it to setup your grid Model.
public ActionResult ExportPDF()
{
// Get the model (setup) of the grid defined in the /Models folder.
var gridModel = new OrdersJqGridModel();
var ordersGrid = gridModel.OrdersGrid;
// Setting the DataUrl to an action (method) in the controller is required.
// This action will return the data needed by the grid
ordersGrid.DataUrl = Url.Action("PDFGrid_DataRequested");
// customize the default Orders grid model with custom settings
// NOTE: you need to call this method in the action that fetches the data as well,
// so that the models match
SetPDFExportGrid(ordersGrid);
// Pass the custmomized grid model to the View
return View(gridModel);
}
// This method is called when the grid requests data
public JsonResult PDFGrid_DataRequested()
{
// Get both the grid Model and the data Model
// The data model in our case is an autogenerated linq2sql database based on Northwind.
var gridModel = new OrdersJqGridModel();
var northWindModel = new NorthwindDataContext();
// customize the default Orders grid model with our custom settings
SetPDFExportGrid(gridModel.OrdersGrid);
// Save the current grid state in Session
// We will later need it for PDF Export
JQGridState gridState = gridModel.OrdersGrid.GetState();
Session["gridState"] = gridState;
// return the result of the DataBind method, passing the datasource as a parameter
// jqGrid for ASP.NET MVC automatically takes care of paging, sorting, filtering/searching, etc
return gridModel.OrdersGrid.DataBind(northWindModel.Orders);
}
public JsonResult PDFExport_AutoCompleteShipName(string term)
{
var northWindModel = new NorthwindDataContext();
JQAutoComplete autoComplete = new JQAutoComplete();
autoComplete.DataField = "ShipName";
autoComplete.AutoCompleteMode = AutoCompleteMode.BeginsWith;
autoComplete.DataSource = from o in northWindModel.Orders
select o;
return autoComplete.DataBind();
}
private void SetPDFExportGrid(JQGrid ordersGrid)
{
// show the search toolbar
ordersGrid.ToolBarSettings.ShowSearchToolBar = true;
ordersGrid.ToolBarSettings.ShowSearchButton = true;
var orderDateColumn = ordersGrid.Columns.Find(c => c.DataField == "OrderDate");
orderDateColumn.DataFormatString = "{0:yyyy/MM/dd}";
orderDateColumn.SearchType = SearchType.DatePicker;
orderDateColumn.DataType = typeof(DateTime);
orderDateColumn.SearchControlID = "DatePicker";
orderDateColumn.SearchToolBarOperation = SearchOperation.IsEqualTo;
var shipNameColumn = ordersGrid.Columns.Find(c => c.DataField == "ShipName");
shipNameColumn.SearchType = SearchType.AutoComplete;
shipNameColumn.DataType = typeof(string);
shipNameColumn.SearchControlID = "AutoComplete";
shipNameColumn.SearchToolBarOperation = SearchOperation.Contains;
var orderIDColumns = ordersGrid.Columns.Find(c => c.DataField == "OrderID");
orderIDColumns.Searchable = true;
orderIDColumns.DataType = typeof(int);
orderIDColumns.SearchToolBarOperation = SearchOperation.IsEqualTo;
SetPDFCustomerIDSearchDropDown(ordersGrid);
SetPDFFreightSearchDropDown(ordersGrid);
}
private void SetPDFCustomerIDSearchDropDown(JQGrid ordersGrid)
{
// setup the grid search criteria for the columns
JQGridColumn customersColumn = ordersGrid.Columns.Find(c => c.DataField == "CustomerID");
customersColumn.Searchable = true;
// DataType must be set in order to use searching
customersColumn.DataType = typeof(string);
customersColumn.SearchToolBarOperation = SearchOperation.IsEqualTo;
customersColumn.SearchType = SearchType.DropDown;
// Populate the search dropdown only on initial request, in order to optimize performance
if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
{
var northWindModel = new NorthwindDataContext();
var searchList = from customers in northWindModel.Customers
select new SelectListItem
{
Text = customers.CustomerID,
Value = customers.CustomerID
};
customersColumn.SearchList = searchList.ToList<SelectListItem>();
customersColumn.SearchList.Insert(0, new SelectListItem { Text = "All", Value = "" });
}
}
private void SetPDFFreightSearchDropDown(JQGrid ordersGrid)
{
// setup the grid search criteria for the columns
JQGridColumn freightColumn = ordersGrid.Columns.Find(c => c.DataField == "Freight");
freightColumn.Searchable = true;
// DataType must be set in order to use searching
freightColumn.DataType = typeof(decimal);
freightColumn.SearchToolBarOperation = SearchOperation.IsGreaterOrEqualTo;
freightColumn.SearchType = SearchType.DropDown;
// Populate the search dropdown only on initial request, in order to optimize performance
if (ordersGrid.AjaxCallBackMode == AjaxCallBackMode.RequestData)
{
List<SelectListItem> searchList = new List<SelectListItem>();
searchList.Add(new SelectListItem { Text = "> 10", Value = "10" });
searchList.Add(new SelectListItem { Text = "> 30", Value = "30" });
searchList.Add(new SelectListItem { Text = "> 50", Value = "50" });
searchList.Add(new SelectListItem { Text = "> 100", Value = "100" });
freightColumn.SearchList = searchList.ToList<SelectListItem>();
freightColumn.SearchList.Insert(0, new SelectListItem { Text = "All", Value = "" });
}
}
public ActionResult ExportToPDF(string exportType)
{
var gridModel = new OrdersJqGridModel();
var northWindModel = new NorthwindDataContext();
var grid = gridModel.OrdersGrid;
// Get the last grid state the we saved before in Session in the DataRequested action
JQGridState gridState = Session["GridState"] as JQGridState;
// Need to set grid options again
SetExportGrid(grid);
if (String.IsNullOrEmpty(exportType))
exportType = "1";
DataTable exportData;
switch (exportType)
{
case "1":
grid.ExportSettings.ExportDataRange = ExportDataRange.All;
exportData = grid.GetExportData(northWindModel.Orders);
ExportToPDF(exportData);
break;
case "2":
grid.ExportSettings.ExportDataRange = ExportDataRange.Filtered;
exportData = grid.GetExportData(northWindModel.Orders, gridState);
ExportToPDF(exportData);
break;
case "3":
grid.ExportSettings.ExportDataRange = ExportDataRange.FilteredAndPaged;
exportData = grid.GetExportData(northWindModel.Orders, gridState);
ExportToPDF(exportData);
break;
}
return View();
}
private void ExportToPDF(DataTable dt)
{
//
// For PDF export we are using the free open-source iTextSharp library.
//
Document pdfDoc = new Document();
MemoryStream pdfStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, pdfStream);
pdfDoc.Open();//Open Document to write
pdfDoc.NewPage();
Font font8 = FontFactory.GetFont("ARIAL", 7);
PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
PdfPCell PdfPCell = null;
//Add Header of the pdf table
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].Caption, font8)));
PdfTable.AddCell(PdfPCell);
}
//How add the data from datatable to pdf table
for (int rows = 0; rows < dt.Rows.Count; rows++)
{
for (int column = 0; column < dt.Columns.Count; column++)
{
PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
PdfTable.AddCell(PdfPCell);
}
}
PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table
pdfDoc.Add(PdfTable); // add pdf table to the document
pdfDoc.Close();
pdfWriter.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=gridexport.pdf");
Response.BinaryWrite(pdfStream.ToArray());
Response.End();
}
}
}

Kendo UI - JSON Response for - Grid using Remote Data Source W/Server Grouping & Server Aggregates

I have a project in which I'm using the KendoUI Grid using the server to get the data instead of locally.
I'm not sure what the JSON response should be from my server to get grouping to work however. My goal is when the user drags a column to the grouping header that I know what kind of JSON response to give back so the GRID groups by that column and any other columns that might be added to that header.
Given the image above, how do I create a JSON response to fulfill it (so its showing what its supposed to grouped)? I get I have to do this on my own on the server but not sure how JSON needs to be formated. Furthermore if I want to show a 'count' field next to the groups when they are created so I know how many items are in each group (which I believe is the aggregate?)
My current grid codes looks like the following:
<div id="grid" style="height:100%;"></div>
<script>
$(window).on("resize", function() {
kendo.resize($("#grid"));
});
var crudServiceBaseUrl = "/api",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "/companies",
dataType: "json",
type: "POST"
},
update: {
url: crudServiceBaseUrl + "/companies/update",
dataType: "json",
type: "POST"
},
destroy: {
url: crudServiceBaseUrl + "/companies/destroy",
dataType: "json",
type: "POST"
},
create: {
url: crudServiceBaseUrl + "/companies/create",
dataType: "json",
type: "POST"
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
error: function (e) {
/* the e event argument will represent the following object:
{
errorThrown: "custom error",
errors: ["foo", "bar"]
sender: {... the Kendo UI DataSource instance ...}
status: "customerror"
xhr: null
}
*/
//alert("Status: " + e.status + "; Error message: " + e.errorThrown);
console.log("Status: " + e.status + "; Error message: " + e.errorThrown);
console.log("Errors: " + e.errors.join("; "));
},
autoSync: false,
serverPaging: true,
serverFiltering: true,
serverSorting: true,
serverGrouping: true,
serverAggregates: true,
pageSize: 20,
columnResizeHandleWidth: 6,
schema: {
total: "itemCount",
data: "items",
groups: "groups",
aggregates: "aggregates",
group: {
field: "phone", aggregates: [{ field: "phone", aggregate: "count" }]
},
model: {
id: "id",
fields: {
id: { editable: false, nullable: true },
name: { validation: { required: true } },
phone: {
type: "string",
validation: {
required: true,
phonerule: function(e){
if (e.is("[data-phonerule-msg]"))
{
var input = e.data('kendoMaskedTextBox');
//If we reached the end of input then it will return -1 which means true, validation passed
//Otherwise it won't === -1 and return false meaning all the characters were not entered.
return input.value().indexOf(input.options.promptChar) === -1;
}
return true; //return true for anything else that is not data-phonerule-msg
}
}
},
email: { type: "string", validation: { required: true, email:true } }
}
}
}
});
$("#grid").kendoGrid({
dataSource: dataSource,
groupable: true,
sortable: {
mode: "multiple",
allowUnsort: true
},
selectable: "multiple cell",
allowCopy:true,
toolbar: ["create","excel"],
excel: {
fileName: "Kendo UI Grid Export.xlsx",
//Below is only used as fallback for old browsers without support
proxyURL: "//demos.telerik.com/kendo-ui/service/export",
filterable: true
},
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
reorderable: true,
resizable: true,
columnMenu: true,
filterable: true,
editable: "popup",
mobile: true,
columns: [
{
field: "name",
title: "Company Name",
aggregates: ["count"],
groupFooterTemplate: "Count: #=count#"
},
{
field: "phone",
title: "Phone",
editor: function(container, options){
//pattern="[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}"
var input = $('<input type="tel" data-phonerule-msg="Invalid Phone Number!" class="k-textbox" required />');
input.attr("name", options.field);
input.kendoMaskedTextBox({
mask: "(999) 000-0000"
});
input.appendTo(container);
},
aggregates: ["count"],
groupFooterTemplate: "Count: #=count#"
},
{
field: "email",
title: "Email",
editor: function(container, options){
var input = $('<input type="email" data-email-msg="Invalid email!" class="k-textbox" required/>');
input.attr("name", options.field);
input.appendTo(container);
},
aggregates: ["count"],
groupFooterTemplate: "Count: #=count#"
},
{
command: ["edit", "destroy"],
title: "Operations",
width: "240px"
}
],
});
</script>
My current code that generates the demo javascript via Symfony 3.0 is below in..
DefaultController.php
/**
* #Route("/api/companies", name="api_companies_read")
*/
public function apiCompaniesReadAction(Request $request)
{
$data["itemCount"] = "7";
// $tdata["field"] = "";
// $tdata["value"] = "";
// $tdata["items"] = "hey";
// $data["groups"][] = $tdata;
$tdata["id"] = "1";
$tdata["name"] = "Joe";
$tdata["phone"] = "(714)475-8651";
$tdata["email"] = "Joe#whatever.com";
$data["items"][] = $tdata;
$tdata["id"] = "2";
$tdata["name"] = "Rachel";
$tdata["phone"] = "(563)812-4184";
$tdata["email"] = "rachel#yahoo.com";
$data["items"][] = $tdata;
$tdata["id"] = "3";
$tdata["name"] = "John";
$tdata["phone"] = "(563)812-4184";
$tdata["email"] = "John#yahoo.com";
$data["items"][] = $tdata;
$tdata["id"] = "4";
$tdata["name"] = "Richard";
$tdata["phone"] = "(563)812-4184";
$tdata["email"] = "John#yahoo.com";
$data["items"][] = $tdata;
$tdata["id"] = "5";
$tdata["name"] = "Sister";
$tdata["phone"] = "(563)812-4184";
$tdata["email"] = "John#yahoo.com";
$data["items"][] = $tdata;
$tdata["id"] = "6";
$tdata["name"] = "Brother";
$tdata["phone"] = "(563)812-4184";
$tdata["email"] = "Brother#yahoo.com";
$data["items"][] = $tdata;
$tdata["id"] = "7";
$tdata["name"] = "Sibling";
$tdata["phone"] = "(563)812-4184";
$tdata["email"] = "Sibling#yahoo.com";
$data["items"][] = $tdata;
// schema: {
// total: "total",
// model: {
// id: "CompanyID",
// fields: {
// CompanyID: { editable: false, nullable: true },
// Name: { validation: { required: true } },
// Phone: { type: "string" },
// Email: { type: "string" }
// }
// }
// }
// replace this example code with whatever you need
return new JsonResponse($data);
}
The current JSON it creates looks like this.
JSON
{"itemCount":"7","items":[{"id":"1","name":"Joe","phone":"(714)475-8651","email":"Joe#whatever.com"},{"id":"2","name":"Rachel","phone":"(563)812-4184","email":"rachel#yahoo.com"},{"id":"3","name":"John","phone":"(563)812-4184","email":"John#yahoo.com"},{"id":"4","name":"Richard","phone":"(563)812-4184","email":"Richard#yahoo.com"},{"id":"5","name":"Sister","phone":"(563)812-4184","email":"Sister#yahoo.com"},{"id":"6","name":"Brother","phone":"(563)812-4184","email":"Brother#yahoo.com"},{"id":"7","name":"Sibling","phone":"(563)812-4184","email":"Sibling#yahoo.com"}]}
I found an article here that looks close to what I want just hard to understand. This might also help.
I created JSFiddle that can be played around with, just needs to be supplied valid data.
Update
I got server paging to work! The first major change is on the transport for the datasource you have to change the parameterMap to send json so you can access what its trying to tell your server to change.
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "/companies",
dataType: "json",
type: "POST"
},
update: {
url: crudServiceBaseUrl + "/companies/update",
dataType: "json",
type: "POST"
},
destroy: {
url: crudServiceBaseUrl + "/companies/destroy",
dataType: "json",
type: "POST"
},
create: {
url: crudServiceBaseUrl + "/companies/create",
dataType: "json",
type: "POST"
},
parameterMap: function(options, operation) {
return kendo.stringify(options);
}
},
My datasource looks like above, but you could adjust to your needs. Next my php file below.
PHP
/companies
/**
* #Route("/api/companies", name="api_companies_read")
*/
public function apiCompaniesReadAction(Request $request)
{
$request_body = file_get_contents('php://input');
$json = json_decode($request_body);
//Based on the JSON Payload response adjust the search in the database
if($json){
$page = $json->page;
$pageSize = $json->pageSize;
$skip = $json->skip;
$take = $json->take;
}else{
$page = 1;
$pageSize = 20;
$skip = 1;
$take = 1;
}
$repository = $this->getDoctrine()->getRepository('AppBundle:Company');
/*
findBy(
array $criteria,
array $orderBy = null,
integer|null $limit = null,
integer|null $offset = null
)
*/
$company_total = $repository->findAll();
$company_records = $repository->findBy(array(),array(),$pageSize,($page-1)*$pageSize);
$data["total"] = count($company_total);
foreach($company_records as $company){
$temp["id"] = $company->getId();
$temp["name"] = $company->getName();
$temp["phone"] = $company->getPhone();
$temp["email"] = $company->getEmail();
$data["data"][] = $temp;
}
//converts data to JSON
return new JsonResponse($data);
}
Please remember the above is a Symfony 3.0 php implementation using annotations to set the route. The important part in that code is.
$request_body = file_get_contents('php://input');
$json = json_decode($request_body);
//Based on the JSON Payload response adjust the search in the database
if($json){
$page = $json->page;
$pageSize = $json->pageSize;
$skip = $json->skip;
$take = $json->take;
}else{
$page = 1;
$pageSize = 20;
$skip = 1;
$take = 1;
}
The file_get_contents('php://input'); gets the javascript object that the KendoUI control sends back.
UPDATE # 3 Got Server Sorting Working!
Below is an example implementaiton using Symfony 3.0 and Doctrine to do Server Sorting!
PHP
/**
* #Route("/api/companies", name="api_companies_read")
*/
public function apiCompaniesReadAction(Request $request)
{
$request_body = file_get_contents('php://input');
$json = json_decode($request_body);
//print_r($json);
//default parameters in case of error.
$page = 1;
$pageSize = 20;
$skip = 1;
$take = 1;
$sort = array();
//Based on the JSON Payload response adjust the search in the database
if(isset($json)){
$page = $json->page;
$pageSize = $json->pageSize;
$skip = $json->skip;
$take = $json->take;
if(isset($json->sort)){
//"sort":[{"field":"name","dir":"asc"}]}:
foreach($json->sort as $sortObj){
$sort[$sortObj->field] = $sortObj->dir;
}
}
}
$repository = $this->getDoctrine()->getRepository('AppBundle:Company');
/*
findBy(
array $criteria,
array $orderBy = null,
integer|null $limit = null,
integer|null $offset = null
)
*/
$company_total = $repository->findAll();
$company_records = $repository->findBy(array(),$sort,$pageSize,($page-1)*$pageSize);
$data["total"] = count($company_total);
foreach($company_records as $company){
$temp["id"] = $company->getId();
$temp["name"] = $company->getName();
$temp["phone"] = $company->getPhone();
$temp["email"] = $company->getEmail();
$data["data"][] = $temp;
}
//converts data to JSON
return new JsonResponse($data);
}
That first link is spot on. I had to export data from a grid to pdf and excel adhering to the grid grouping. I finally figured out the semantics. The grid has columns and data objects which you can get through grid calls. The data object holds the grouping information. Here is the processing operation.
In a nutshell, you have to recursively iterate the data object tpo determine the grouping. For each element in the data you need to inspect if the data[x].value exists. If it exists then it is a group object with child data. If it does not exists then you use the normal data[row][column].field to get to the child data. The trick here is to call a recurisive function for each point where dat[0].value has data and finally process the data[row][column] on the unwind.
Below is a js function you can use to inspect the Grid's json dta. I would add a group or two to your grid and see the difference between the data with/without grouping applied and that should be what you need to add to your data....I think you pretty much add data as data={[[],[]]}unless it is a group, then it is data={[value='groupName',{[],[]}]}.
function showData(gridName) {
var grid = $('#' + gridName).data('kendoGrid');
var data = grid.dataSource.data().toJSON();
console.log(data);
}

Adding links to jQgrid and open in new window

I have this jqgrid definition and I'm trying to open the selected document in a new Window.
My final urls should like these:
http://localhost/XPagesSortableSearchResults.nsf/xPerson.xsp?documentId=9D93E80306A7AA88802572580072717A&action=openDocument
and also I need to generate this type of url:
http://localhost/XPagesSortableSearchResults.nsf/$$OpenDominoDocument.xsp?documentId=9D93E80306A7AA88802572580072717&action=openDocument
$().ready(function(){
jQuery("#list2").jqGrid({
url:'./xGrid7.xsp/peoplejson',
datatype: "json",
colNames:['InternetAddress','#','Name','OfficeCountry'],
colModel:[
{name:'InternetAddress',index:'InternetAddress', width:200},
{name:'#position',index:'#position', width:50,sorttype:'int'},
{name:'$17',index:'$17', width:200},
{name:'OfficeCountry',
width:200,
formatter:editLinkFmatter
// formatter:'showlink',
// formatoptions:{ baseLinkUrl:'xPerson.xsp',addParam: '&action=openDocument', idName:'documentId'}
}
],
jsonReader: {
repeatitems: false,
id: '#unid',
root: function (obj) {
if ($.isArray(obj)) {
return obj;
}
if ($.isArray(obj.items)) {
return obj.items;
}
return [];
},
page: function () { return 1; },
total: function () { return 1; },
records: function (obj) {
if ($.isArray(obj)) {
return obj.length;
}
if ($.isArray(obj.items)) {
return obj.items.length;
}
return 0;
}
},
caption: "JSON Example",
height: 500,
gridview: true,
loadonce: true,
ignoreCase: true,
rowNum: 50,
rowList: [50, 100, 500, 1000],
pager: '#pager2'
}).jqGrid('filterToolbar', {stringResult: true, defaultSearch: 'cn', searchOnEnter: false});
Note my Json object looks like this and I'm not using the documentId I need on my url as part of my ColModel; the value I need is #unid
[
{
"#entryid":"1-B933790B1DC265ED8025725800728CC5",
"#unid":"B933790B1DC265ED8025725800728CC5",
"#noteid":"1E76E",
"#position":"1",
"#read":true,
"#siblings":40000,
"#form":"Person",
"$17":"Aaron, Adam",
"InternetAddress":"consurgo#compleo.net",
"OfficeCountry":"Namibia"
},
{
"#entryid":"2-9D93E80306A7AA88802572580072717A",
"#unid":"9D93E80306A7AA88802572580072717A",
"#noteid":"19376",
"#position":"2",
"#read":true,
"#siblings":40000,
"#form":"Person",
"$17":"Aaron, Dave",
"InternetAddress":"gratia#incito.co.uk",
"OfficeCountry":"Brazil"
},
{
"#entryid":"3-FAFA753960DB587A80257258007287CF",
"#unid":"FAFA753960DB587A80257258007287CF",
"#noteid":"1D842",
"#position":"3",
"#read":true,
"#siblings":40000,
"#form":"Person",
"$17":"Aaron, Donnie",
"InternetAddress":"vociferor#nequities.net",
"OfficeCountry":"Algeria"
}
]
So far I make it work using:
{name:'OfficeCountry',
width:200,
formatter:'showlink',
formatoptions:{ baseLinkUrl:'xPerson.xsp',addParam: '&action=openDocument', idName:'documentId'}
}
but I need to open it in a new window
I also tried with formatter:editLinkFmatter
function editLinkFmatter(cellvalue, options, rowObject) {
return "<a href='./" + rowObject[2] + "' class='requestlink'>" + cellvalue + "</a>";
//return "<a href='./documentId=" + rowObject.#unid + "' >Click here</a>";
//return "<a href='./documentId=" + options.idName + "&action=OpenDocument'>" + cellvalue + "</a>";
}
and I can't use rowObject.#unid because the node name
It seems to me that you should just use target="_blank"attribute in the <a> (see here and here). The standard 'showlink' formatter supports target attribute.
As the alternative to the custom formatter you can use 'dynamicLink' formatter (see the answer). You can download the last version of jQuery.jqGrid.dynamicLink.js from here.
UPDATED: To assess the property with the name #unid you can use syntax rowObject["#unid"]. So the editLinkFmatter can be like
function editLinkFmatter(cellvalue, options, rowObject) {
return "<a href='?documentId=" + rowObject["#unid"] +
"&action=OpenDocument' class='requestlink'>" + cellvalue + "</a>";
}
or better like
function editLinkFmatter(cellvalue, options, rowObject) {
return "<a href='?" +
$.param({
documentId: rowObject["#unid"],
action: 'OpenDocument'
}) + "' class='requestlink'>" + cellvalue + "</a>";
}