Add column values together in jqGrid and insert into label - html

I have a jqGrid inside of a div orderForm that can have a verifying number of rows added to it by a user.
What I would like to do is: whenever a user adds a row to the jqGrid, the totals in the TOTAL_LINE_AMOUNT column are added together and inserted into the label Subtotal
$('#orderForm).jqGrid({
data: details,
datatype: 'local',
colNames: ['ID', 'QUANTITY', 'MODEL_ORDER_NUM', 'DESCRIPTION', 'PRICE_EACH', 'TOTAL_LINE_AMOUNT'],
colModel: [
{ name: 'DETAIL_RECORD_ID', index: 'DETAIL_RECORD_ID', sorttype: 'string' },
{ name: 'QUANTITY', index: 'QUANTITY', sorttype: 'string' },
{ name: 'MODEL_ORDER_NUM', index: 'MODEL_ORDER_NUM', sorttype: 'string' },
{ name: 'DESCRIPTION', index: 'DESCRIPTION', sorttype: 'string' },
{ name: 'PRICE_EACH', index: 'PRICE_EACH', sorttype: 'string' },
{ name: 'TOTAL_LINE_AMOUNT', index: 'TOTAL_LINE_AMOUNT', sorttype: 'string' }
],
search: true,
onSelectRow: LoadInput,
loadonce: false,
jsonReader: { cell: '' },
sortname: 'DETAIL_RECORD_ID',
sortorder: 'asc',
sortable: true,
ignoreCase: true,
viewrecords: true,
height: 'auto',
width: 'auto',
shrinkToFit: false,
hiddengrid: false,
caption: 'Detail Records'
});
UPDATE: Here is how I load data into my grid
Form.Controller.orderForm = new function () {
var _detailRecordId = 1;
this.Create = function () {
var requestData = Controller.GetRequestData();
requestData.orderForm.push({
DETAIL_RECORD_ID: "T" + _detailRecordId++,
REQUEST_RECORD_ID: requestData.REQUEST_RECORD_ID,
QUANTITY: $('#orderForm_QUANTITY_INPUT').val(),
MODEL_ORDER_NUM: $('#orderForm_MODEL_ORDER_NUM_INPUT').val(),
DESCRIPTION: $('#orderForm_DESCRIPTION_INPUT').val(),
PRICE_EACH: $('#orderForm_PRICE_EACH_INPUT').val(),
TOTAL_LINE_AMOUNT: $('#orderForm_TOTAL_LINE_AMOUNT_INPUT').text()
});
Form.View.orderFrom.LoadGrid(requestData.orderForm);
};

Your code doesn't show which editing mechanism your are using with jqGrid so it is hard to say on which event you should subscribe (most porobably it should be either jqGridInlineAfterSaveRow or jqGridAddEditAfterComplete).
Getting the sum of column is very simple as jqGrid has a ready to use method for this:
var subTotal = $('#orderForm').jqGrid('getCol', 'TOTAL_LINE_AMOUNT', false, 'sum');
Depending of what kinf of HTML element your label is you should be able to set its text with one of following:
$('#Subtotal').text(subTotal);
or
$('#Subtotal').val(subTotal);

Related

jqGrid: JSON Returned by Web Service Won't Bind to Subgrid

I'm trying to add subgrids to an existing jqGrid.
When I expand a parent row, the URL is called and the JSON response looks good (to my eyes) but it is not being bound to the sub grid rows.
If I call the web service directly by pasting the URL in a browser, the response looks good.
This is the full jqGrid definition:
/* +++ Group Grid Definition +++ */
$("#<%= id %>_groupTable").jqGrid({
loadonce: true,
datatype: 'local',
//sortable: true,
gridview: true,
editurl: "clientArray",
autowidth: true,
rowNum: 12000,
height: 150,
multiselect: false,
hidegrid: false,
pgbuttons: false,
pginput: false,
sortname: "Date",
sortorder: "desc",
jsonReader: {
root: "inbox",
page: "page",
total: "total",
records: "records",
id: "id",
repeatitems: false,
subgrid: { repeatitems: true }
},
toolbar: [true, "bottom"],
colModel: [
{
name: 'userGroupId',
label: '{{VIEW_LABEL}}',
index: 'userGroupId',
width: 50,
fixed: true,
hidden: true,
//sortable: false,
align: 'center'
}, {
name: 'desc',
label: '{{NAME_LABEL}}',
index: 'desc',
width: 130,
align: 'center',
sorttype: 'text'
}],
hidegrid: false,
gridComplete: function () {
$('#<%= id %> .loading').remove();
$('#<%= id %>_groupTable').jqGrid('setSelection', 1);
},
loadComplete: styleGroupRows,
afterInsertRow: styleGroupRows,
subGrid: true,
subgridtype: 'json',
subGridUrl: '/ws/users/groupusers',
subGridModel: [
{
name: ['Name'],
width: [200],
params: ['userGroupId']
}]
}).jqGrid('bindKeys', {
'scrollingRows': true
});
This is the JSON response from the web service:
{'rows':['User 1312','User 1316','User 1286','User 1149']}
I'm expecting a single column of user names in the sub grids but only the column headers appear.
Your subGrid response does not match the rules.
Try with the following response:
{"rows":[{"id":"1","cell":["User 1312"]},{"id":"2","cell":["User 1316"]}]}

Populate Combo box with JSON file, ExtJs 4.x.x

Ext.define('Form', {
extend: 'Ext.data.Model',
fields: [
{name:'type', type:'String'}
]
});
var store = Ext.create('Ext.data.ArrayStore', {
model: 'Form',
autoLoad: true,
proxy: {
type: 'ajax',
url: '/test.json',
reader: {
type : 'json',
root: 'types'
}
}
});
Ext.define('DForms', {
extend: 'Ext.panel.Panel',
bodyPadding: 12,
layout : 'auto',
autoScroll : true,
items: [{
xtype:'selectcombo',
queryMode:'local',
emptyText: 'Select Condition',
store:store,
displayField: 'type',
valueField: 'type',
width : 200,
typeAhead : true
}],
});
When this loads, the selectcombo is empty, nothing gets loaded, i have searched through many sites, and can't find anything to help. Any suggestions would be great
The xtype you look for is combo, the store type is JsonStore because the ArrayStore will not interpret the json from the types root as you might expect. I can not simulate the ajax request here though.
Ext.onReady(function() {
Ext.define('Form', {
extend: 'Ext.data.Model',
fields: [{
name: 'type',
type: 'String'
}]
});
var store = Ext.create('Ext.data.Store', {
model: 'Form',
data: [{
type: 'Aaaaaaa'
}, {
type: 'Bbbbbb'
}, {
type: 'Ccccccccccc'
}, {
type: 'Ddddddd'
}]
});
Ext.define('DForms', {
extend: 'Ext.panel.Panel',
bodyPadding: 12,
layout: 'auto',
autoScroll: true,
items: [{
xtype: 'combo',
queryMode: 'local',
emptyText: 'Select Condition',
store: store,
displayField: 'type',
valueField: 'type',
width: 200,
typeAhead: true
}],
});
Ext.create('DForms', {
renderTo: Ext.getBody()
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/ext-all-debug.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/extjs/4.2.1/resources/css/ext-all.css" />
I think type in fields is case-sensitive. So try
fields :[{name : 'type', type : 'string'}]
If still not working, as Tejas1991 pointed out check the content of your json.

JQXGrid lists all json data under first column

// I am using a JQXGrid, and while populating the grid with valid JSON string (I checked) it forms the columns perfectly (headers, footers, pageing, etc) but all the data is listed under the first column. I have been trying different settings for the last while and possibly a second set of eyes can see the error.
// Configure data source for data grid,...
var LocationDataSource =
{
datatype: "json",
datafields: [
{ name: 'Date' },
{ name: 'ProductCode' },
{ name: 'StoreNum' },
{ name: 'ProductQty', type: 'int' }
],
localdata: LocationData
};
// Configure Data Adapter and apply JSON data to it,...
var LocationDataAdapter = new $.jqx.dataAdapter(LocationDataSource);
// Apply data source to grid,...
$("#jqxLocationGrid").jqxGrid(
{
width: 900,
source: LocationDataAdapter,
pageable: true,
rowsheight: 50,
autoheight: true,
sortable: true,
altrows: true,
enabletooltips: true,
selectionmode: 'multiplecellsadvanced',
columns: [
{ text: 'Date', datafield: 'Date', width: 250 },
{ text: 'Product Code', datafield: 'ProductCode', width: 250 },
{ text: 'Store Number', datafield: 'StoreNum', width: 250 },
{ text: 'Product Qty', datafield: 'ProductQty', width: 250 }
]
});
I had the same issue and i fixed it, I forgot to add jqx.base.css to the default.aspx.

JQgrid total amount row

iv seen an example by #Oleg for row total sum in jqgrid but i tried to apply it and it wont work i have the following grid i need to calculate the amount value for it.
colNames: ['ID', 'FTE', 'Workload'],
colModel: [
{ name: 'ID', index: 'ID', width: 200, align: 'left', hidden: true },
{ name: 'FTEValue', index: 'FTEValue', width: 200, align: 'left', formatter: 'number' },
{ name: 'Workload', index: 'Workload', width: 200, align: 'left' },
caption: "Activity FTE",
gridview: true,
rownumbers: true,
rownumWidth: 40,
scroll: 0,
rowNum: 100,
sortname: 'ID',
pager: '#pager',
sortorder: "asc",
viewrecords: true,
autowidth: true,
height: '100%',
footerrow: true,
jsonReader: { root: "GridData", page: "CurrentPage", total: "TotalPages", records: "TotalRecords", repeatitems: false, id: "0" }
};
DutyFTEGrid.prototype.SetupGrid = function (selector) {
jQuery(selector).html('<table id="grid"></table><div id="pager"></div>');
var grid = jQuery("#grid").jqGrid(this.gridConfiguration);
jQuery("#grid").jqGrid('navGrid', '#pager',
{ edit: false, add: false, search: false }, {}, {},
{ // Delete parameters
ajaxDelOptions: { contentType: "application/json" },
mtype: "DELETE",
serializeDelData: function () {
return "";
},
onclickSubmit: function (params, postdata) {
params.url = serviceURL + 'DutyFTE(' + encodeURIComponent(postdata) + ')/';
}
});
var grid = $("#grid");
var sum = grid.jqGrid('getCol', 'FTE', false, 'sum');
grid.jqGrid('footerData', 'set', { DriverEn: 'Total FTE:', FTEValue: sum });
};
Oleg your help please, i have tried your example but it didnt work for some reason.
If I understand you correct you want to place in the footer getCol and footerData methods:
var grid = $("#list"),
sum = grid.jqGrid('getCol', 'amount', false, 'sum');
grid.jqGrid('footerData','set', {ID: 'Total:', amount: sum});
The getCol can be used to calculate the sum of all numbers from the 'amount' column and with respect of footerData you can place at the bottom of the 'ID' column the text "Total:" and at the bottom of 'amount' column.
UPDATED: Probably you have problems because you place the code in the wrong place. The most safe place for the code is loadComplete event handler. Look at the demo.
Total of a price column:
//Count total for a price column
var total = 0;
$('#table tr').each(function(){
//cells that contains the price
var tdprice = $(this).find("td:eq(2)").html();
//Sum it up!
if (isNaN(tdprice)){ total += parseInt(tdprice); }
});
alert(total + "$");

ExtJS Grid doesn't load data

I'm trying to display data in a grid by using JsonStore.
The grid gets rendered, but it doesn't display the data.
The JSON data returned by API.ashx:
[{"Account":{"Username":"root","Password":null,"Enabled":true,"Id":1},"Text":"Hallo Welt!","Id":1},{"Account":{"Username":"root","Password":null,"Enabled":true,"Id":1},"Text":"hihihi","Id":3}]
My code:
Ext.onReady(function () {
var store = new Ext.data.JsonStore({
url: 'API.ashx?type=notes&action=getAll',
root: 'Note',
autoload: true,
fields: ['Text', { name: 'Id', type: 'int'}]
});
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{
id: 'Note',
header: 'Id',
width: 25,
sortable: true,
dataIndex: 'Id'
},
{
header: 'Text',
width: 160,
sortable: true,
dataIndex: 'Text'
}
],
stripeRows: true,
autoExpandColumn: 'Note',
height: 350,
width: 600,
title: 'Notes',
stateful: true,
stateId: 'grid'
});
store.load();
grid.render('grid-example');
});
I just fixed it myself. I had to remove the option "root" and now it works fine.