Advanced AngularJS paging and searching with WEB API2 - javascript

We create ASP.NET MVC5 project with AngularJS and WebApi 2. In some views we need paging for EntityList. For this purpose we write WebApi action as follows:
[HttpGet]
[Route("GetAll/{pageSize:int}/{pageNumber:int}/{caseType?}/{caseDate?}/{caseNumber?}/{entryDate?}/{entryNumber?}/{seenForm?}/{levelOfAccess?}/{tagWords?}/{senderOrganization?}/{senderPerson?}")]
[AgPermission(System.Security.Permissions.SecurityAction.Demand)]
//[Authorize(Roles = "Katiblik")]
public ObjectWrapperWithNameOfSet GetAll(int pageSize, int pageNumber,int? caseType = null,string caseDate = null,string caseNumber = null,string entryDate = null,string entryNumber = null,SeenForm? seenForm = null,LevelOfAccess? levelOfAccess = null,string tagWords = null,int? senderOrganization = null, int? senderPerson = null)
{
var caseQuery = service.List().List;
if (caseType != null)
{
caseQuery = caseQuery.Where(#case => #case.CaseType.Id == caseType).ToList();
}
if (caseDate != "null")
{
caseQuery = caseQuery.Where(#case => #case.CaseDate == DateTime.ParseExact(caseDate, "ddMMyyyy", null)).ToList();
}
if (caseNumber != "null")
{
caseQuery = caseQuery.Where(#case => #case.CaseNumber == caseNumber).ToList();
}
if (entryDate != "null")
{
caseQuery = caseQuery.Where(#case => #case.EntryDate == DateTime.ParseExact(entryDate, "ddMMyyyy", null)).ToList();
}
if (entryNumber != "null")
{
caseQuery = caseQuery.Where(#case => #case.EntryNumber == entryNumber).ToList();
}
if (seenForm != null)
{
caseQuery = caseQuery.Where(#case => #case.SeenForm == seenForm).ToList();
}
if (levelOfAccess != null)
{
caseQuery = caseQuery.Where(#case => #case.LevelOfAccess == levelOfAccess).ToList();
}
if (tagWords != "null")
{
caseQuery = caseQuery.Where(#case => #case.TagedWords.Contains(tagWords)).ToList();
}
if (senderOrganization != null)
{
caseQuery = caseQuery.Where(#case => #case.SenderOrganization.Id == senderOrganization).ToList();
}
if (senderPerson != null)
{
caseQuery = caseQuery.Where(#case => #case.SenderPerson.Id == senderPerson).ToList();
}
var totalCount = caseQuery.Count();
var totalPages = Math.Ceiling((double)totalCount / pageSize);
var cases = caseQuery.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
var result = new
{
TotalCount = totalCount,
totalPages = totalPages,
Cases = cases
};
return new ObjectWrapperWithNameOfSet("list", result);
}
But it seemed not advanced and not clearly understanted method for Paging in WebApi2. We have many data for the case entity and we must create searching for every column of table.
So my question is
How to create Advanced AngularJS paging with WEB API2?

Related

What is a set analogue from squel.js in knex.js?

How can I rewrite it using Knex.js?
type === 'insert' ?
sql = squel.insert()
.set('id', userId)
.set('type', 'open')
.set('server', server)
.set('created_at', timestamp)
.set('updated_at', timestamp)
.into('saved_users') :
sql = squel.update()
.table('saved_users')
.set('updated_at', timestamp)
.where(`id = "${userId}"`)
for (const param in player) {
if (param === 'full_response') continue;
const value = player[param];
if (param === 'kill') {
sql.set(`\`${param}\``, value ?? 0)
} else {
sql.set(param, value ?? 0)
}
}
I'm using .set(..) to append the insert or update query. How to do it using Knex?
Something like that =/ Maybe it can better
const data = {}
const query = knex("saved_users")
for (const param in player) {
if (param === 'full_response') continue;
const value = player[param];
if (param === 'kill') {
data[param] = value ?? 0
} else {
data[param] = value ?? 0
}
}
if (type === "insert") {
data["id"] = userId;
data["type"] = "open";
data["server"] = server;
data["created_at"] = timestamp;
data["updated_at"] = timestamp;
await query.insert(data)
} else {
data["updated_at"] = timestamp
await query.update(data).where({
id: userId
})
}

Vaadin-grid export to CSV with lazy loading

I have to create a button that exports a Vaadin-grid as a CSV file.
But if the table has too many entries it starts to do lazy loading / paging and it only exports the loaded entries.
How do i export the entire table in those cases?
I'm pretty new to javascript and all i could find where java solutions that disable lazy loading entirely (which is not optimal since most tables are very big)
here is the exporter class:
export class TableCSVExporter {
constructor(table, content, includeHeaders = true) {
this.table = table;
this.content = Array.from(content.querySelectorAll('vaadin-grid-cell-content'));
this.rows = Array.from(table.querySelectorAll('tr'));
if (!includeHeaders && this.rows[0].querySelectorAll('th').length) {
this.rows.shift();
}
}
convertToCSV() {
const lines = [];
const numCols = this._findLongestRowLength();
console.log(this.content);
let l = 0;
let rows = this.content.length / numCols;
// const row of this.rows
for (let y = 0; y < rows; y++) {
let line = '';
for (let i = 0; i < numCols; i++) {
if (y == 0) {
line += TableCSVExporter.parseHeaderCell(this.content[l]);
} else {
line += TableCSVExporter.parseCell(this.content[l]);
}
l++;
line += (i !== (numCols - 1)) ? ',' : '';
}
lines.push(line);
}
return lines.join('\n');
}
_findLongestRowLength() {
return this.rows.reduce((l, row) => row.childElementCount > l ? row.childElementCount : l, 0);
}
static parseCell(tableCell) {
let parsedValue = tableCell.textContent;
return this.formatCellContent(parsedValue);
}
static parseHeaderCell(tableCell) {
let parsedValue = "";
let input = tableCell.querySelector('vaadin-text-field');
if(input != null) {
parsedValue = input.placeholder;
}
return this.formatCellContent(parsedValue);
}
static formatCellContent(parsedValue) {
// Replace all double quotes with two double quotes
parsedValue = parsedValue.replace(/\n/g, '');
parsedValue = parsedValue.replace(/"/g, `""`);
parsedValue = parsedValue.trim();
// If value contains comma, new-line or double-quote, enclose in double quotes
parsedValue = /[",\n]/.test(parsedValue) ? `"${parsedValue}"` : parsedValue;
return parsedValue;
}
}
and this is the table class:
import {
LitElement, html,
} from 'lit-element';
import { render } from 'lit-html';
import '#vaadin/vaadin-grid/theme/material/vaadin-grid';
import '#vaadin/vaadin-grid/theme/lumo/vaadin-grid';
import '#vaadin/vaadin-grid/vaadin-grid-filter';
import '#vaadin/vaadin-grid/vaadin-grid-sorter';
import './theme/vaadin-grid';
import '#vaadin/vaadin-context-menu/vaadin-context-menu';
import { createGridOutput } from '../../../js/LdGlobal';
import { LdGridStyles } from './styles';
import '../modules/assets/assets-charts/availability/swimlane';
import '#polymer/paper-button/paper-button';
import '#polymer/paper-fab/paper-fab';
import { TableCSVExporter } from './TableCSVExporter';
/**
* Displays a <vaadin-grid> element with given items
* #extends LitElement
*/
class LdGrid extends LitElement {
// --------------------------------------------------------------------------------------
static get properties() {
return {
gridItems: { type: Object, hasChanged: () => true },
gridTitle: { type: String, hasChanged: () => true },
gridColumns: { type: Object, hasChanged: () => true },
multiSort: { type: Boolean, value: false, hasChanged: () => true },
loading: { type: Boolean, value: false, hasChanged: () => true },
userInformation: { type: Object, hasChanged: () => true },
_height: { type: String, hasChanged: () => true },
};
}
// --------------------------------------------------------------------------------------
static get styles() {
return [LdGridStyles];
}
// --------------------------------------------------------------------------------------
constructor() {
super();
this.addEventListener('keyup', () => this.calculateHeight());
}
// --------------------------------------------------------------------------------------
updated(changedProperties) {
if (changedProperties.has('gridColumns')) {
if (changedProperties.get('gridColumns') === this.gridColumns) {
this.calculateHeight();
}
}
}
// --------------------------------------------------------------------------------------
render() {
const gridColumns = [];
this.gridColumns.forEach(item => {
if (!item.hidden) {
let _contextMenuOptions = '';
let _contextMenuPrefix = '';
let _contextMenuField = '';
let _contextMenuSuffix = '';
const _column = {};
let _contextMenuOptionsLength;
let _contextMenuLinksLength;
if (item.icon) {
_column.style = typeof item.icon.style !== 'undefined' ? item.icon.style : '';
_column.src = typeof item.icon.src !== 'undefined' ? item.icon.src : '';
_column.title = typeof item.icon.title !== 'undefined' ? item.icon.title : '';
}
if (item.link) {
_column.link = {};
_column.link.prefix = typeof item.link.prefix !== 'undefined' ? item.link.prefix : '';
_column.link.suffix = typeof item.link.suffix !== 'undefined' ? item.link.suffix : '';
_column.link.field = typeof item.link.field !== 'undefined' ? item.link.field : '';
}
_column.subkey = typeof item.subKey !== 'undefined' ? item.subKey : '';
_column.chart = typeof item.charts !== 'undefined' ? item.charts : '';
_column.assetId = typeof item.assetId !== 'undefined' ? item.assetId : '';
_column.dateRange = typeof item.timestamp !== 'undefined' ? item.timestamp : '';
if (!item.visible_for_user_group) {
item.visible_for_user_group = [this.userInformation.main_group];
}
const _columnWidth = (typeof item.column_width !== 'undefined') ? item.column_width : '100px';
const _disableTitle = typeof item.disable_title !== 'undefined' && item.disable_title;
const _linkPrefix = (typeof _column.link !== 'undefined' && typeof _column.link.prefix !== 'undefined') ? _column.link.prefix : null;
const _linkField = (typeof _column.link !== 'undefined' && typeof _column.link.field !== 'undefined') ? _column.link.field : null;
const _linkSuffix = (typeof _column.link !== 'undefined' && typeof _column.link.suffix !== 'undefined') ? _column.link.suffix : null;
const _iotOnlineTime = (typeof item.iot !== 'undefined' && typeof item.iot.onlineTime !== 'undefined') ? item.iot.onlineTime : null;
const _iotColor = (typeof item.iot !== 'undefined' && typeof item.iot.color !== 'undefined') ? item.iot.color : null;
if (item.contextMenuOptions) {
item.contextMenuOptions.inputOptions.forEach((option, index) => {
_contextMenuOptions += (typeof option !== 'undefined') ? `${option},` : '';
});
item.contextMenuOptions.link.forEach((option, index) => {
_contextMenuPrefix += (typeof option.prefix !== 'undefined') ? `${option.prefix},` : '';
_contextMenuField += (typeof option.field !== 'undefined') ? `${option.field},` : '';
_contextMenuSuffix += (typeof option.suffix !== 'undefined') ? `${option.suffix},` : '';
});
_contextMenuLinksLength = item.contextMenuOptions.link.length;
}
if (item.visible_for_user_group[0].match(this.userInformation.main_group)) {
gridColumns.push(html`
<vaadin-grid-column
path='${item.id}'
subKey='${_column.subkey}'
initialSort='${item.initialSort}'
header='${item.title}'
sort='${item.sort}'
sortPath='${item.sortPath}'
filter='${item.filter}'
iconSrc='${_column.src}'
iconTitle='${_column.title}'
iconStyle='${_column.style}'
linkPrefix='${_linkPrefix}'
linkField='${_linkField}'
linkSuffix='${_linkSuffix}'
iotColor='${_iotColor}'
iotOnlineTime='${_iotOnlineTime}'
chart='${_column.chart}'
assetId='${_column.assetId}'
dateRange='${_column.dateRange}'
contextMenuPrefix='${_contextMenuPrefix}'
contextMenuField='${_contextMenuField}'
contextMenuSuffix='${_contextMenuSuffix}'
contextMenuOptionsLength='${_contextMenuOptionsLength}'
contextMenuLinksLength='${_contextMenuLinksLength}'
contextMenuOptions='${_contextMenuOptions}'
width='${_columnWidth}'
columnAlign='${item.column_align}'
disableTitle='${_disableTitle}'
.headerRenderer='${this.__searchAndFilterHeaderRenderer}'
.renderer='${this._columnRenderer}'
>
</vaadin-grid-column>`);
}
}
});
return html`
<vaadin-grid visible-rows='1' theme='row-stripes wrap-cell-content no-border' ?loading='${this.loading}' aria-label='${this.gridTitle}' .items='${this.gridItems}' ?multi-sort='${this.multiSort}'>
${gridColumns}
</vaadin-grid>
<paper-fab class='fab-item-left'
#click='${e => this._onDownloadCsvClick(e)}'
icon='add'
title='Export to CSV'></paper-fab>
`;
}
// --------------------------------------------------------------------------------------
__searchAndFilterHeaderRenderer(root, column) {
const _path = column.getAttribute('path');
const _title = column.getAttribute('header');
const _initialSort = column.getAttribute('initialSort');
const _filter = column.getAttribute('filter');
const _sort = column.getAttribute('sort');
const _sortPath = column.getAttribute('sortPath');
const _disableTitle = column.getAttribute('disableTitle');
if (_disableTitle) {
render(html``, root);
}
let _vaadinGridSorter;
let _vaadinGridFilter;
if (_sort !== 'undefined' && _sort) {
if (_initialSort !== 'undefined') {
_vaadinGridSorter = html`
<vaadin-grid-sorter id='sorter_${_path}' path='${_sortPath !== 'undefined' ? _sortPath : _path}'
direction='${_initialSort}'></vaadin-grid-sorter>
`;
} else {
_vaadinGridSorter = html`
<vaadin-grid-sorter path='${_sortPath !== 'undefined' ? _sortPath : _path}'></vaadin-grid-sorter>`;
}
}
if (_filter !== 'undefined' && _filter) {
_vaadinGridFilter = html`
<vaadin-grid-filter path='${_path}'>
<vaadin-text-field
id='filter_${_path}'
slot='filter'
focus-target
theme='small'
placeholder='${_title}'
#value-changed='${e => e.target.parentNode.value = e.detail.value}'>
</
>
</vaadin-grid-filter>`;
} else {
_vaadinGridFilter = html`
<vaadin-grid-column
path='${_path}'
class='header'>${_title}
</vaadin-grid-column>
`;
}
render(html`
${_vaadinGridSorter}
${_vaadinGridFilter}
`, root);
}
// --------------------------------------------------------------------------------------
_columnRenderer(root, column, rowData) {
const _path = column.getAttribute('path');
const _subKey = column.getAttribute('subKey');
const _iconSrc = column.getAttribute('iconSrc');
const _iconTitle = column.getAttribute('iconTitle');
const _linkPrefix = column.getAttribute('linkPrefix');
const _linkField = column.getAttribute('linkField');
const _linkSuffix = column.getAttribute('linkSuffix');
const _chart = column.getAttribute('chart');
const _assetId = column.getAttribute('assetId');
const _dateRange = column.getAttribute('dateRange');
const _iotColor = column.getAttribute('iotColor');
let _contextMenuPrefix = column.getAttribute('contextMenuPrefix');
let _contextMenuField = column.getAttribute('contextMenuField');
let _contextMenuSuffix = column.getAttribute('contextMenuSuffix');
const _contextMenuLinksLength = column.getAttribute('contextMenuLinksLength');
let _contextMenuOptions = column.getAttribute('contextMenuOptions');
const _iotOnlineTime = column.getAttribute('iotOnlineTime');
const _columnAlign = column.getAttribute('columnAlign');
let _link = null;
let _inputOptions = null;
const _vaadinItem = [];
let _output = createGridOutput(_path, rowData);
const _title = createGridOutput(_path, rowData);
if (_subKey !== '') {
_output = html`<b>${_output}</b><br />${rowData.item[_subKey]}`;
}
// TODO: testing against empty string?
if (_linkField !== 'null') {
_link = `${_linkPrefix}${rowData.item[_linkField]}${_linkSuffix}`;
}
let _vaadinGrid;
/**
* Create Icon and/or Link
*/
if (_contextMenuField !== '' && _contextMenuOptions !== '') {
for (let i = 0; i < Number(_contextMenuLinksLength); i++) {
// get the string from Prefix Field / Suffix until ,
_link = `${_contextMenuPrefix.substr(0, _contextMenuPrefix.indexOf(','))}${_contextMenuField.substr(0, _contextMenuField.indexOf(','))}/${rowData.item[_contextMenuSuffix.substr(0, _contextMenuSuffix.indexOf(','))]}`;
// get the string for the label until ,
_inputOptions = _contextMenuOptions.substr(0, _contextMenuOptions.indexOf(','));
// create the vaadin-item
_vaadinItem.push(html`
<vaadin-item theme='custom'>
<a href='${_link}'>${_inputOptions}</a>
</vaadin-item>
`);
// delete the used string
_contextMenuPrefix = _contextMenuPrefix.substr(_contextMenuPrefix.indexOf(',') + 1);
_contextMenuSuffix = _contextMenuSuffix.substr(_contextMenuSuffix.indexOf(',') + 1);
_contextMenuField = _contextMenuField.substr(_contextMenuField.indexOf(',') + 1);
_contextMenuOptions = _contextMenuOptions.substr(_contextMenuOptions.indexOf(',') + 1);
}
_vaadinGrid = html`
<vaadin-context-menu open-on='click'>
<div style='text-align:${_columnAlign}'>
<iron-icon icon='${_iconSrc}' style='cursor: pointer;'></iron-icon>
</div>
<template>
<vaadin-list-box>
${_vaadinItem}
</vaadin-list-box>
</template>
</vaadin-context-menu>
`;
} else if (_path === 'icon' && _link !== null) {
_vaadinGrid = html`
<a title='${_iconTitle}' id='${_title}' class='title' href='${_link}'>
<div style='text-align:${_columnAlign}'>
<iron-icon icon='${_iconSrc}'></iron-icon>
</div>
</a>`;
} else if (_path === 'icon' && _link === null) {
_vaadinGrid = html`
<div title='${_iconTitle}' id='${_title}' style='text-align:${_columnAlign}'>
<iron-icon icon='${_iconSrc}' style='cursor: pointer;'>
</iron-icon>
</div>`;
} else if (_link !== null) {
_vaadinGrid = html`
<a title='${_title}' id='${_title}' href='${_link}'>${_output}</a>`;
} else if (_path === 'iotLabel') {
_vaadinGrid = html`
<span title='${rowData.item[_iotOnlineTime]}' id='${rowData.item[_iotOnlineTime]}'
class='circle ${rowData.item[_iotColor]}'></span>
<span class='suffix'>${_output}</span>`;
} else if (_path === 'trend') {
_vaadinGrid = html`
<span>${_output}</span>`;
} else {
_vaadinGrid = html`
<span title='${_title}' id='${_title}'>${_output}</span>`;
}
/**
* create charts in an grid
*/
if (_chart !== '') {
// dot at machineId is needed in edge. Without machineId is null
_vaadinGrid = html`
<ld-availability-swimlane-chart .machineId='${rowData.item[_assetId]}'
.dateRange='${rowData.item[_dateRange]}'></ld-availability-swimlane-chart>`;
}
render(html`
${_vaadinGrid}
`, root);
}
// --------------------------------------------------------------------------------------
calculateHeight() {
setTimeout(() => {
const vaddinGridTable = this.shadowRoot.querySelector('vaadin-grid');
if (vaddinGridTable !== null) {
this._height = `${vaddinGridTable.shadowRoot.querySelector('#table').clientHeight}px`;
vaddinGridTable.setAttribute('style', `height:${this._height}`);
}
}, 500);
}
_onDownloadCsvClick(e) {
let dataTable = null;
const vaddinGridContent = this.shadowRoot.querySelector('vaadin-grid');
if (vaddinGridContent !== null) {
dataTable = vaddinGridContent.shadowRoot.querySelector('#table');
}
// console.log(dataTable);
const exporter = new TableCSVExporter(dataTable, vaddinGridContent);
const csvOutput = exporter.convertToCSV();
const csvBlob = new Blob([csvOutput], { type: 'text/csv' });
const blobUrl = URL.createObjectURL(csvBlob);
const anchorElement = document.createElement('a');
anchorElement.href = blobUrl;
anchorElement.download = 'table-export.csv';
anchorElement.click();
}
}
customElements.define('ld-grid', LdGrid);

Assigning a viewbag to a variable in a JQuery function

I'm using MVC and am trying to check if the item has enough stock in inventory. I do this in my controller by
[HttpPost]
[ValidateAntiForgeryToken]
[Audit]
public void AddUnits(int so_id, int site_id, int[] addItem_id, int[] addItem_qty)
{
// Loop however many times is necessary to iterate through the largest array
for (int i = 0; i < Math.Max(Math.Max(addItem_id.Length, addComp_id.Length), addPart_id.Length); i++)
{
foreach (SODetails sod in db.SalesOrders.Find(so_id).SalesDetails)
{
if (i < addItem_id.Length && addItem_qty[i] != 0 && sod.ItemID == addItem_id[i] && addItem_id[i] != 365 && addItem_id[i] != 410)
{
sod.item_qty += addItem_qty[i];
sod.item_discount = addItem_disc[i];
addItem_id[i] = 0;
addItem_qty[i] = 0;
addItem_disc[i] = 0;
}
}
db.SaveChanges();
if(i < addItem_qty.Length && addItem_qty[i] != 0)
{
SODetails sODetails = new SODetails
{
SalesOrderID = so_id,
SiteID = site_id
};
// Only add a unit to the SODetails object if it's not null and has an id and quanitity specified
if(i < addItem_id.Length && addItem_id[i] != 0 && addItem_qty[i] != 0)
{
sODetails.ItemID = addItem_id[i];
sODetails.item_qty = addItem_qty[i];
sODetails.item_discount = addItem_disc[i];
}
SalesOrder SO = db.SalesOrders.Find(sODetails.SalesOrderID);
SODetails salesOrderDetails = db.SODetails.Add(sODetails);
salesOrderDetails.SalesOrder = SO;
Item SO_Item = db.Items.Find(sODetails.ItemID);
if (SO_Item != null)
{
ViewBag.itemOnHand = SO_Item.On_Hand;
sODetails.item_qty = sODetails.item_qty == null ? 0 : sODetails.item_qty;
int qtyOrdered = sODetails.item_qty == null ? 0 : (int)sODetails.item_qty;
salesOrderDetails.dynamicItem_qty = qtyOrdered;
if (SO_Item.SalesOrderMessage != null)
TempData["SalesOrderMessage"] = SO_Item.SalesOrderMessage;
}
}
}
}
db.SaveChanges();
}
Currently I'm trying to pass the inventory count of that item into the viewbag by doing
ViewBag.itemOnHand = SO_Item.On_Hand;
Then my view Jquery function looks like this
// Get all item ids and quantities and store them in arrays
var itemssel = document.getElementsByClassName("Item-select");
var itemsqtysel = document.getElementsByClassName("Item-qty");
var itemsdiscsel = document.getElementsByClassName("Item-disc");
var itemOnHand = #ViewBag.itemOnHand;
for (i = 0; i < itemssel.length; i++) {
items[i] = itemssel[i].options[itemssel[i].selectedIndex].value;
itemsqty[i] = itemsqtysel[i].value;
itemsdisc[i] = itemsdiscsel[i].value;
if (itemsqty[i] < 0) {
alert("Quantities can't be negative!");
return;
}
if (itemsqty[i] < itemOnHand) {
alert("Not enough inventory in site!");
return;
}
// The add units method is then called here
// Send all the values to the AJAX function and respond with success or error
$.ajax({
type: "POST",
url: "#IGT.baseUrl/SODetailsAjax/AddUnits",
traditional: true,
data: {
__RequestVerificationToken: token,
so_id: #Int32.Parse(Request["orderId"]),
site_id: site,
addItem_id: items,
addItem_qty: itemsqty,
addItem_disc: itemsdisc,
addComp_id: comps,
addComp_qty: compsqty,
addComp_disc: compsdisc,
addPart_id: parts,
addPart_qty: partsqty,
addPart_disc: partsdisc
},
success: function () {
location.href = "../SalesOrders/Details?id=#so.ID";
},
error: function (jqXHR, status, error) {
alert("Error: " + error);
}
});
But it doesn't work correctly as it is right now. Why is this?
As requested, here is my GET method for the page before it posts
// GET: SODetails/Create
[RestrictAccess(restriction = AccessRestrictions.ModifySalesOrder)]
public ActionResult Create(int orderId)
{
var SOID = (from i in db.SalesOrders.Where(x => x.ID == orderId).Where(x => x.deleted == false)
select new
{
SO_id = i.ID,
status = i.ID + " (Status: " + i.SalesOrderStatus +")",
}).OrderByDescending(x => x.SO_id).ToList();
var item = (from i in db.Items.Where(x => x.deleted == false)
select new
{
itemID = i.ID,
itemName = i.Product_Number + " : " + i.ItemID + " : " + i.Name
}).OrderBy(x => x.itemName).ToList();
var component = (from c in db.Components.Where(x => x.deleted == false)
select new
{
compID = c.ID,
compName = c.Product_Number + " : " + c.ComponentID + " : " + c.Name
}).OrderBy(x => x.compName).ToList();
var part = (from c in db.Parts.Where(x => x.deleted == false)
select new
{
partID = c.ID,
partName = c.Product_Number + " : " + c.PartID + " : " + c.Name
}).OrderBy(x => x.partName).ToList();
var sites = (from s in db.Sites
select new
{
siteID = s.ID,
siteName = s.Name
}).OrderBy(x => x.siteName).ToList();
sites.Insert(0, new { siteID = 0, siteName = "Main Inventory" });
var masters = db.Items.Where(x => x.Product_Number.Contains("106101") || x.ItemID.Contains("106101"));
List<int> ids = new List<int>();
foreach(Item i in masters.Where(x => x.Product_Number.Contains("BMU") || x.Product_Number.Contains("BMDU") || x.Product_Number.Contains("BMS")))
{
ids.Add(i.ID);
}
ViewBag.BMUMasters = ids.ToArray();
ids = new List<int>();
foreach (Item i in masters.Where(x => x.Product_Number.Contains("GMU")))
{
ids.Add(i.ID);
}
ViewBag.GMUMasters = ids.ToArray();
ViewBag.ItemID = new SelectList(item, "itemID", "itemName");
ViewBag.ComponentID = new SelectList(component, "compID", "compName");
ViewBag.PartID = new SelectList(part, "partID", "partName");
ViewBag.SalesOrderID = new SelectList(SOID, "SO_id", "status");
ViewBag.SiteID = new SelectList(sites, "siteID", "siteName");
ViewBag.invSiteID = new SelectList(sites, "siteID", "siteName");
return View();
}
Try to add null coalescing, this will return 0 if the ViewBag.itemOnHand is empty;
var itemOnHand = #(ViewBag.itemOnHand ?? 0);
Then if you're going to use it for some number operations try to force it to become an integer;
var itemOnHand = Number(#(ViewBag.itemOnHand ?? 0));
Since you already have an if statement or null checks prior to ViewBag assignment, you could simply return the view and add a ViewBag.Error;
if (SO_Item != null)
{
// .. if not null
}else{
// if null return view
ViewBag.Error = "your error here";
return View();
}
Then somewhere in your view add this;
#if(ViewBag.Error != null){
<div class="row error">
<div class="col-md-12">
Error occured: <strong>#ViewBag.Error</strong>
</div>
</div>
}

Acumatica error after upgrade from version 18.210 to version 18.217 ( Unable to cast object of type ....)

I recently upgraded my acumatica account from version 18.210 to version 18.217, When i try to do an create a sales order.
when i go to sales order tab and try to create sales order i get the error message
The error reads
Unable to cast object of type 'PX.Data.PXResult`2[PX.Objects.FS.FSServiceOrder,PX.Objects.AR.Customer]'
to type 'PX.Objects.FS.FSServiceOrder'.
Tace error...
using Autofac;
using PX.Data;
using PX.Data.DependencyInjection;
using PX.LicensePolicy;
using PX.Objects.AP;
using PX.Objects.AR;
using PX.Objects.CM.Extensions;
using PX.Objects.CR;
using PX.Objects.CS;
using PX.Objects.CT;
using PX.Objects.Extensions.MultiCurrency;
using PX.Objects.Extensions.SalesTax;
using PX.Objects.GL;
using PX.Objects.FS.ParallelProcessing;
using PX.Objects.IN;
using PX.Objects.IN.Overrides.INDocumentRelease;
using PX.Objects.PO;
using PX.Objects.SO;
using PX.Objects.TX;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Compilation;
using PX.Objects;
using PX.Objects.FS;
namespace PX.Objects.FS
{
public class ServiceOrderEntry_Extension : PXGraphExtension<ServiceOrderEntry>
{
#region Event Handlers
protected void FSServiceOrder_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected InvokeBaseHandler)
{
if(InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
FSServiceOrder row = this.Base.ServiceOrderRecords.Current;
FSServiceOrderExt orderExt = PXCache<FSServiceOrder>.GetExtension<FSServiceOrderExt>(row);
if (row != null)
{
foreach (FSAppointment apps in SelectAppointments(row.RefNbr, row.SrvOrdType))
{
if (apps != null)
{
if (apps.Status == "X" || apps.Status=="Z")
{
cache.SetValue<FSServiceOrderExt.usrOpenAppointments>(row, false);
}
else
{
cache.SetValue<FSServiceOrderExt.usrOpenAppointments>(row, true);
}
}
}
}
}
private IEnumerable SelectAppointments(object refnbr, object ordertype)
{
if (refnbr == null) return new FSAppointment[0];
return PXSelect<FSAppointment, Where< FSAppointment.soRefNbr, Equal<Required<FSAppointment.soRefNbr>>,
And<FSAppointment.srvOrdType, Equal<Required<FSAppointment.srvOrdType>>>>>.Select(this.Base, refnbr, ordertype).RowCast<FSAppointment>();
}
public string doctype = "SM";
public string nbr = "";
public int chkLines = 0;
protected void FSSODetPart_RowSelected(PXCache cache, PXRowSelectedEventArgs e, PXRowSelected InvokeBaseHandler)
{
if(InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
FSSODetPart row = (FSSODetPart)e.Row;
if(row != null)
{
FSSODetExt partsExt = PXCache<FSSODet>.GetExtension<FSSODetExt>(row);
//disable the SO fields
PXUIFieldAttribute.SetEnabled<FSSODetExt.usrSONumber>(cache, row, false);
PXUIFieldAttribute.SetEnabled<FSSODetExt.usrSOStatus>(cache, row, false);
//disable action if nothing is selected
FSServiceOrder fsServiceORderRow = this.Base.ServiceOrderRecords.Current;
foreach (FSSODetPart parts in SelectItems(fsServiceORderRow.RefNbr))
{
if (parts != null)
{
if (partsExt.UsrMarkSO == true)
{
chkLines++;
}
}
}
if (chkLines == 0)
{
this.Base.actionsMenu.SetEnabled("createSalesOrder", false);
}
//disable chkbox if sonumber exists
if (!string.IsNullOrEmpty(partsExt.UsrSONumber))
{
PXUIFieldAttribute.SetEnabled<FSSODetExt.usrMarkSO>(cache, row, false);
}
//Check the SOStatus
string strStatus = partsExt.UsrSOStatus;
if (strStatus == "C")
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, "Completed");
}
else if (strStatus == "S")
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, "Shipping");
}
else if (strStatus == "O")
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, "Open");
}
else if (strStatus == "H")
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, "On-Hold");
}
else if (strStatus == "P")
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, "Pending Approval");
}
else if (strStatus == "N")
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, "Open-Approved");
}
else
{
cache.SetValueExt<FSSODetExt.usrSOStatus>(row, strStatus);
}
}
}
public override void Initialize()
{
this.Base.actionsMenu.AddMenuAction(CreateSalesOrder);
}
public PXAction<FSServiceOrder> CreateSalesOrder;
[PXUIField(DisplayName = "Create Sales Order", Enabled = true, MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXProcessButton]
public virtual IEnumerable createSalesOrder(PXAdapter adapter)
{
foreach (FSServiceOrder fso in adapter.Get())
{
Customer cust = (Customer)PXSelect<Customer, Where<Customer.bAccountID, Equal<Current<FSServiceOrder.customerID>>>>.Select(this.Base);
if (cust == null)
{
string exc = "Invalid customer ID";
throw new PXException(exc);
}
yield return fso;
}
SOOrderEntry docgraph = PXGraph.CreateInstance<SOOrderEntry>();
PXLongOperation.StartOperation(Base, delegate ()
{
Customer customer = (Customer)PXSelect<Customer, Where<Customer.bAccountID, Equal<Current<FSServiceOrder.customerID>>>>.Select(this.Base);
FSServiceOrder fsServiceORderRow = this.Base.ServiceOrderRecords.Current;
FSSODetPart rows = this.Base.ServiceOrderDetParts.Current;
CurrencyInfo info = PXSelect<CurrencyInfo, Where<CurrencyInfo.curyInfoID, Equal<Current<FSServiceOrder.curyInfoID>>>>.Select(this.Base);
info.CuryInfoID = null;
bool createso = false;
foreach (FSSODetPart parts in SelectItems(fsServiceORderRow.RefNbr))
{
if (parts == null)
{
createso = false;
}
else
{
createso = true;
}
}
if (createso == true)
{
SOOrder doc = new SOOrder();
doc.OrderType = "SM";
doc = docgraph.Document.Insert(doc);
doc.OrderDate = fsServiceORderRow.OrderDate;
doc.CustomerRefNbr = fsServiceORderRow.RefNbr;
doc.CustomerOrderNbr = fsServiceORderRow.CustPORefNbr;
nbr = fsServiceORderRow.RefNbr;
doc.OrderDesc = fsServiceORderRow.DocDesc;
doc.TermsID = customer.TermsID;
doc.CustomerID = fsServiceORderRow.CustomerID;
doc = docgraph.Document.Update(doc);
foreach (FSSODetPart parts in SelectItems(fsServiceORderRow.RefNbr))
{
if (parts == null) return;
SOLine tran = new SOLine();
tran = docgraph.Transactions.Insert(tran);
if (tran != null)
{
tran.InventoryID = parts.InventoryID;
tran.SubItemID = parts.SubItemID;
tran.OrderQty = parts.OrderQty;
tran.UOM = parts.UOM;
tran.CuryUnitPrice = parts.CuryUnitPrice;
tran.TaxCategoryID = parts.TaxCategoryID;
tran.SiteID = parts.SiteID;
tran.LocationID = parts.LocationID;
tran.IsFree = parts.IsFree;
tran.ProjectID = parts.ProjectID;
tran.TaskID = parts.TaskID;
tran.ManualPrice = parts.ManualPrice;
}
tran = docgraph.Transactions.Update(tran);
}
docgraph.Save.Press();
docgraph.Document.Current = docgraph.Document.Search<SOOrder.orderNbr, SOOrder.orderType>(doc.OrderNbr, doctype);
}
});
}
private IEnumerable SelectItems(object refnbr)
{
if (refnbr == null) return new FSSODetPart[0];
return PXSelect<FSSODetPart, Where2<Where<FSSODetPart.refNbr, Equal<Required<FSSODetPart.refNbr>>,
And<FSSODetExt.usrMarkSO, Equal<True>>>,
And<FSSODetExt.usrSONumber, IsNull>>>.Select(this.Base, refnbr).RowCast<FSSODetPart>();
}
private IEnumerable UpdateItems(object refnbr)
{
if (refnbr == null) return new FSSODetPart[0];
return PXSelect<FSSODetPart, Where2<Where<FSSODetPart.refNbr, Equal<Required<FSSODetPart.refNbr>>,
And<FSSODetExt.usrMarkSO, Equal<True>>>,
And<FSSODetExt.usrSONumber, IsNull>>>.Select(this.Base, refnbr).RowCast<FSSODetPart>();
}
#endregion
}
}

Instagram instafeed Image rotator/Slider

This is my first post on StackOverflow and might I say, what a great and helpful resource this is. I have been able to find many answers to my questions and hope to do the same with this one. On to the issue at hand... I am currently using the instafeed.js (http://instafeedjs.com/) script to pull in images from instagram and display them on my site.
What I'm trying to accomplish is to have 9 items display, then if I hit the "next" button, it would replace the current 9 images and load the next 9 images and, if the "previous" button is clicked it would go back and show the previous 9 images.
Currently it only renders one image thumb that scrolls through 9 images then stops. Swapping only the one image at a time. A sample of the current working code with only 1 image displaying can be found here - http://codepen.io/stevenschobert/pen/iHxfw
Here is my JavaScript for the instafeed call:
var count = 1;
var feed;
feed = new Instafeed({
clientId: '68be8b63013048ff81bb4ac8b02b606e',
limit: 9,
resolution: 'standard_resolution',
template: '<img src="{{image}}" /><div class="likes">♥ {{likes}}</div>',
mock: true,
after: function () {
var images = $("#instafeed").find('a');
$.each(images, function(index, image) {
var delay = (index * 75) + 'ms';
$(image).css('-webkit-animation-delay', delay);
$(image).css('-moz-animation-delay', delay);
$(image).css('-ms-animation-delay', delay);
$(image).css('-o-animation-delay', delay);
$(image).css('animation-delay', delay);
$(image).addClass('animated fadeInUp pic-'+count++);
});
},
custom: {
images: [],
currentImage: 0,
showImage: function () {
var result, image;
image = this.options.custom.images[this.options.custom.currentImage];
result = this._makeTemplate(this.options.template, {
model: image,
id: image.id,
link: image.link,
image: image.images[this.options.resolution].url,
caption: this._getObjectProperty(image, 'caption.text'),
likes: image.likes.count,
comments: image.comments.count,
location: this._getObjectProperty(image, 'location.name')
});
$("#instafeed").html(result);
}
},
success: function (data) {
this.options.custom.images = data.data;
this.options.custom.showImage.call(this);
}
});
feed.run();
$(".next").click(function () {
var length, current;
current = feed.options.custom.currentImage;
length = feed.options.custom.images.length;
if (current < length - 1) {
feed.options.custom.currentImage++;
feed.options.custom.showImage.call(feed);
}
});
$(".prev").click(function () {
var length, current;
current = feed.options.custom.currentImage;
length = feed.options.custom.images.length;
if (current > 0) {
feed.options.custom.currentImage--
feed.options.custom.showImage.call(feed);
}
});
My HTML:
<div id="instafeed"></div>
<div class="controls">
<div class="prev"><- prev</div>
<div class="next">next -></div>
</div>
The Instafeed.js code:
(function() { var Instafeed, root; Instafeed = (function() {
function Instafeed(params) {
var option, value;
this.options = {
target: 'instafeed',
get: 'popular',
resolution: 'thumbnail',
sortBy: 'most-recent',
links: true,
limit: 15,
mock: false
};
if (typeof params === 'object') {
for (option in params) {
value = params[option];
this.options[option] = value;
}
}
this.unique = this._genKey();
}
Instafeed.prototype.run = function() {
var header, instanceName, script;
if (typeof this.options.clientId !== 'string') {
if (typeof this.options.accessToken !== 'string') {
throw new Error("Missing clientId or accessToken.");
}
}
if (typeof this.options.accessToken !== 'string') {
if (typeof this.options.clientId !== 'string') {
throw new Error("Missing clientId or accessToken.");
}
}
if ((this.options.before != null) && typeof this.options.before === 'function') {
this.options.before.call(this);
}
if (typeof document !== "undefined" && document !== null) {
script = document.createElement('script');
script.id = 'instafeed-fetcher';
script.src = this._buildUrl();
header = document.getElementsByTagName('head');
header[0].appendChild(script);
instanceName = "instafeedCache" + this.unique;
window[instanceName] = new Instafeed(this.options);
window[instanceName].unique = this.unique;
}
return true;
};
Instafeed.prototype.parse = function(response) {
var anchor, fragment, header, htmlString, image, imageString, images, img, instanceName, reverse, sortSettings, _i, _j, _len, _len1;
if (typeof response !== 'object') {
if ((this.options.error != null) && typeof this.options.error === 'function') {
this.options.error.call(this, 'Invalid JSON data');
return false;
} else {
throw new Error('Invalid JSON response');
}
}
if (response.meta.code !== 200) {
if ((this.options.error != null) && typeof this.options.error === 'function') {
this.options.error.call(this, response.meta.error_message);
return false;
} else {
throw new Error("Error from Instagram: " + response.meta.error_message);
}
}
if (response.data.length === 0) {
if ((this.options.error != null) && typeof this.options.error === 'function') {
this.options.error.call(this, 'No images were returned from Instagram');
return false;
} else {
throw new Error('No images were returned from Instagram');
}
}
if ((this.options.success != null) && typeof this.options.success === 'function') {
this.options.success.call(this, response);
}
if (this.options.sortBy !== 'most-recent') {
if (this.options.sortBy === 'random') {
sortSettings = ['', 'random'];
} else {
sortSettings = this.options.sortBy.split('-');
}
reverse = sortSettings[0] === 'least' ? true : false;
switch (sortSettings[1]) {
case 'random':
response.data.sort(function() {
return 0.5 - Math.random();
});
break;
case 'recent':
response.data = this._sortBy(response.data, 'created_time', reverse);
break;
case 'liked':
response.data = this._sortBy(response.data, 'likes.count', reverse);
break;
case 'commented':
response.data = this._sortBy(response.data, 'comments.count', reverse);
break;
default:
throw new Error("Invalid option for sortBy: '" + this.options.sortBy + "'.");
}
}
if ((typeof document !== "undefined" && document !== null) && this.options.mock === false) {
document.getElementById(this.options.target).innerHTML = '';
images = response.data;
if (images.length > this.options.limit) {
images = images.slice(0, this.options.limit + 1 || 9e9);
}
if ((this.options.template != null) && typeof this.options.template === 'string') {
htmlString = '';
imageString = '';
for (_i = 0, _len = images.length; _i < _len; _i++) {
image = images[_i];
imageString = this._makeTemplate(this.options.template, {
model: image,
id: image.id,
link: image.link,
image: image.images[this.options.resolution].url,
caption: this._getObjectProperty(image, 'caption.text'),
likes: image.likes.count,
comments: image.comments.count,
location: this._getObjectProperty(image, 'location.name')
});
htmlString += imageString;
}
document.getElementById(this.options.target).innerHTML = htmlString;
} else {
fragment = document.createDocumentFragment();
for (_j = 0, _len1 = images.length; _j < _len1; _j++) {
image = images[_j];
img = document.createElement('img');
img.src = image.images[this.options.resolution].url;
if (this.options.links === true) {
anchor = document.createElement('a');
anchor.href = image.images['standard_resolution'].url;
anchor.rel = "lightbox";
anchor.appendChild(img);
fragment.appendChild(anchor);
} else {
fragment.appendChild(img);
}
}
document.getElementById(this.options.target).appendChild(fragment);
}
header = document.getElementsByTagName('head')[0];
header.removeChild(document.getElementById('instafeed-fetcher'));
instanceName = "instafeedCache" + this.unique;
delete window[instanceName];
}
if ((this.options.after != null) && typeof this.options.after === 'function') {
this.options.after.call(this);
}
return true;
};
Instafeed.prototype._buildUrl = function() {
var base, endpoint, final;
base = "https://api.instagram.com/v1";
switch (this.options.get) {
case "popular":
endpoint = "media/popular";
break;
case "tagged":
if (typeof this.options.tagName !== 'string') {
throw new Error("No tag name specified. Use the 'tagName' option.");
}
endpoint = "tags/" + this.options.tagName + "/media/recent";
break;
case "location":
if (typeof this.options.locationId !== 'number') {
throw new Error("No location specified. Use the 'locationId' option.");
}
endpoint = "locations/" + this.options.locationId + "/media/recent";
break;
case "user":
if (typeof this.options.userId !== 'number') {
throw new Error("No user specified. Use the 'userId' option.");
}
if (typeof this.options.accessToken !== 'string') {
throw new Error("No access token. Use the 'accessToken' option.");
}
endpoint = "users/" + this.options.userId + "/media/recent";
break;
default:
throw new Error("Invalid option for get: '" + this.options.get + "'.");
}
final = "" + base + "/" + endpoint;
if (this.options.accessToken != null) {
final += "?access_token=" + this.options.accessToken;
} else {
final += "?client_id=" + this.options.clientId;
}
final += "&count=" + this.options.limit;
final += "&callback=instafeedCache" + this.unique + ".parse";
return final;
};
Instafeed.prototype._genKey = function() {
var S4;
S4 = function() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return "" + (S4()) + (S4()) + (S4()) + (S4());
};
Instafeed.prototype._makeTemplate = function(template, data) {
var output, pattern, varName, varValue, _ref;
pattern = /(?:\{{2})([\w\[\]\.]+)(?:\}{2})/;
output = template;
while (pattern.test(output)) {
varName = output.match(pattern)[1];
varValue = (_ref = this._getObjectProperty(data, varName)) != null ? _ref : '';
output = output.replace(pattern, "" + varValue);
}
return output;
};
Instafeed.prototype._getObjectProperty = function(object, property) {
var piece, pieces;
property = property.replace(/\[(\w+)\]/g, '.$1');
pieces = property.split('.');
while (pieces.length) {
piece = pieces.shift();
if ((object != null) && piece in object) {
object = object[piece];
} else {
return null;
}
}
return object;
};
Instafeed.prototype._sortBy = function(data, property, reverse) {
var sorter;
sorter = function(a, b) {
var valueA, valueB;
valueA = this._getObjectProperty(a, property);
valueB = this._getObjectProperty(b, property);
if (reverse) {
if (valueA > valueB) {
return 1;
} else {
return -1;
}
}
if (valueA < valueB) {
return 1;
} else {
return -1;
}
};
data.sort(sorter.bind(this));
return data;
};
return Instafeed;})(); root = typeof exports !== "undefined" && exports !== null ? exports : window; root.Instafeed = Instafeed;}).call(this);
Any help with this issue would be GREATLY appreciated.
Thank You,
Jason

Categories

Resources