I am trying to do the following but I am not sure how to do it. I have a grid (like excel) where the user will enter information. The grid has columns like "empno", "paycode", "payrate", "hours" etc. The user will enter a separate record in the grid for each paycode for each employee in the system. so the grid will look like the following
empno paycode payrate hours
1 R 25.00 40.00
2 R 12.00 40.00
3 R 15.00 18.00
1 v 25.00 12.00
2 PTO 25.00 18.00
Below this grid I will have two grids. One that displays "Employee totals" and one that displays "Totals by paycode". so as the user enters the data in the grid, I am planning to add / update the values into a two dimensional array. Then when the user clicks on a particular record on the grid, I will read the empno from that record and display the employee totals by binding the array to the html table. As you can see the data entered by the user might not be in the order of empno. The user might first enter the data for paycode "R", then paycode "V" etc.
Please let me know your ideas.
Why don't you just store it in a JavaScript object that's keyed off of your employee numbers. For instance...
var emps = {};
emps['1'] = [
{
empno : '1',
paycode : 'R',
payrate : '25.00',
hours : '40.00'
},
{
empno : '1',
paycode : 'G',
payrate : '30.00',
hours : '10.00'
}
];
function getEmp( empno ) {
if ( emps[empno] && typeof emps[empno] === "object" ) {
return emps[empno];
} else {
return false;
}
}
function addRecord( record ) {
if ( !emps[record.empno] || typeof emps[record.empno] !== "object" ) {
emps[record.empno] = [];
}
emps[record.empno].push( record );
}
It sounds like you want to loop through the input fields, and update the Employee total fields.
If the number of employee number records is unknown, I would use an id for each input similar to this: id=empno_x where x is a unique ID for each staff member. You can then use jquery to get the values using:
//Loop through all employee number input fields
$(":input[id^='empno_']").each function(){
//Do whatever you need to do with the data (add it to an array etc)
});
So you would do that for the empno, paycode, payrate, and hours fields.
Once you have captured the data being input, it's just a matter of manipulating the HTML using Jquery.
Hope this helps!
Related
I have two sheets. Test Data has 3-4k entries of many columns of data and Order Changes has no data at all. I would like to search two specific columns on Test Data, a column of names and a column of yes or no. If column two of Test Data contains a 'yes' in the cell then the name of that person would be placed into a cell on order changes.
This is what I have so far:
function isThreshold(){
var data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test Data");
var cdata = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Order Changes");
var lc = data.getLastColumn();
var lookUp = data.getRange(1,6,3,2).getValues();
lookUp.forEach(var info in lookUp){
}
Logger.log(lookUp);
}
I probably shouldn't loop through that many entries but I don't know of any other way. Should I combine the forEach loop with an if loop to get the desired result or use some other method?
I believe your goal as follows.
You want to retrieve the values from the cells "F1:G" of sheet "Test Data".
You want to search yes from the column "G" and when the column "G" is yes, you want to put the value of the column "F" to the sheet "Order Changes".
Modification points:
SpreadsheetApp.getActiveSpreadsheet() can be declared one time.
In this case, you can retrieve the values from the range of "F1:G" + data.getLastRow() of "Test Data", and create the array for putting to the sheet "Order Changes", and put it.
When above points are reflected to your script, it becomes as follows.
Modified script:
function isThreshold(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data = ss.getSheetByName("Test Data");
var cdata = ss.getSheetByName("Order Changes");
var valuesOfTestData = data.getRange("F1:G" + data.getLastRow()).getValues();
var valuesForOrderChanges = valuesOfTestData.reduce((ar, [f, g]) => {
if (g.toLowerCase() == "yes") ar.push([f]);
return ar;
}, []);
if (valuesForOrderChanges.length > 0) {
cdata.getRange(1, 1, valuesForOrderChanges.length, valuesForOrderChanges[0].length).setValues(valuesForOrderChanges);
// or cdata.getRange(cdata.getLastRow() + 1, 1, valuesForOrderChanges.length, valuesForOrderChanges[0].length).setValues(valuesForOrderChanges);
}
}
In this modified script, from your question, it supposes that the columns "F" and "G" are the value of name and yes or no.
References:
getRange(a1Notation) of Class Sheet
reduce()
I want to make few tables from an Array using dropdown list. List will be Daily, weekly, Monthly, yearly.
I have written the code. Below is the sample array.
var allPoints = records.map(
(
{ id, sector_name, f_day_lot1, f_day_lot2, f_day_lot3, f_day_lot4 }
) => (
{ id, sector_name, f_day_lot1, f_day_lot2, f_day_lot3, f_day_lot4 }
)
);
The thing is I want to change the above day to weekly, monthly or yearly depends on the dropdown.
I can write same code for multiple times changing day to weekly, monthly or yearly. But then it will be alot of code. So, is there any other better way to do.
Or how to make day as a variable and change value depending upon menu.
I'm not sure what is the exact data structure of your records. But I think you can use a dynamic key name:
function getPoints(records, dropdownValue) {
return records.map((item) => {
let mapedItem = {};
for (const key in item) {
if (key.indexOf(`${dropdownValue}_lot`) !== -1) {
mapedItem[key] = item[key];
}
}
mapedItem["id"] = item["id"];
mapedItem["sector_name"] = item["sector_name"];
return mapedItem;
});
}
const data = [
{
id: 1,
sector_name: 2,
f_day_lot1: "f_day_lot1",
f_day_lot2: "f_day_lot2",
f_day_lot3: "f_day_lot3",
f_day_lot4: "f_day_lot4",
f_month_lot1: "f_month_lot1",
f_month_lot2: "f_month_lot2",
f_month_lot3: "f_month_lot3",
f_month_lot4: "f_month_lot4",
f_week_lot1: "f_week_lot1",
f_week_lot2: "f_week_lot2",
f_week_lot3: "f_week_lot3",
f_week_lot4: "f_week_lot4"
}
];
console.log(getPoints(data, "day"));
console.log(getPoints(data, "week"));
console.log(getPoints(data, "month"));
I would like to store product information in a key, value array, with the key being the unique product url. Then I would also like to store the visit frequency of each of these products. I will store these objects as window.localStorage items, but that's not very important.
The thing I had in mind was two key value arrays:
//product information
prods["url"] = ["name:product_x,type:category_x,price:50"]
//product visits frequency
freq["url"] = [6]
Then I would like to sort these prods based on the frequency.
Is that possible?
Hope you guys can help! Thanks a lot
Well you seem to have made several strange choices for your data format/structure. But assuming the format of the "prod" is beyond your control but you can choose your data structure, here's one way to do it.
Rather than two objects both using url as a key and having one value field each I've made a single object still keyed on url but with the product and frequency information from each in a field.
Objects don't have any inherent order so rather than sorting the table object I sort the keys, your "url"s ordered by ascending frequency.
To show that it's sorted that way I print it out (not in the same format).
For descending frequency, change data[a].freq - data[b].freq to data[b].freq - data[a].freq
var data = {
"url": {
prod: "name:product_x,type:category_x,price:50",
freq: 6
},
"url2": {
prod: "name:product_y,type:category_y,price:25",
freq: 3
}
};
var sorted = Object.keys(data).sort((a, b) => data[a].freq - data[b].freq);
console.log(sorted.map(k => [data[k].freq, k, data[k].prod]));
There's more than one way to format the data, which would change the shape of the code here.
maybe something like this:
var prods = [
{url:1, val:[{name:'a',type:'x',price:60}]},
{url:2, val:[{name:'b',type:'x',price:30}]},
{url:3, val:[{name:'c',type:'x',price:50}]},
{url:4, val:[{name:'c',type:'x',price:20}]},
{url:5, val:[{name:'c',type:'x',price:10}]},
{url:6, val:[{name:'c',type:'x',price:40}]}
];
var freq = [
{url:1, freq:6},
{url:2, freq:3},
{url:3, freq:5},
{url:4, freq:2},
{url:5, freq:1},
{url:6, freq:4}
];
prods.sort(function (a, b) {
var aU = freq.filter(function(obj) {
return obj.url === a.url;
});
var bU = freq.filter(function(obj) {
return obj.url === b.url;
});
if (aU[0].freq > bU[0].freq) {
return 1;
}
if (aU[0].freq < bU[0].freq) {
return -1;
}
return 0;
});
I have a jquery datatable that's initialized and populated using ajax.
I have a menu on the left with various "complex" sorting options.
My datatable contains a total price as well as the number of units sold.
I don't have and can't add a column "unit price" but i still would like my menu item "Sort by unit price" to work as intended which means that if i have the following table :
Name Units Price
----------------------
James 1 10
Eric 2 19
Greg 10 110
James 5 45
And i click on that button, i want it to be sorted like that :
Name Units Price
----------------------
James 5 45
Eric 2 19
James 1 10
Greg 10 110
I want to do this purely in javascript cause i don't want to mess with the controllers and models.
I managed to do this using the following:
$.fn.dataTableExt.afnSortData['unit-price'] = function ( oSettings, iColumn ) {
return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
var price = parseInt($('td:eq('+iColumn+')', tr).text());
if(isNaN(price)) return 0;
var units = parseInt($('td:eq(1)', tr).text());
if(isNaN(units)) return 0;
var result = Math.round(1000*price/units);
return result;
} );
};
For now i'm keeping the fact that the units column is at index 1 but one could use a class for instance to keep this dynamic.
And then initializing in the datatable with:
theTable.dataTable({
...
"aoColumns": [
null,
null,
null,
null,
null,
{ "sSortDataType": "unit-price" }
]
});
I can now sort it with an external button using:
theTable.dataTable().fnSort([[5,'asc']]);
I have a page that contains a table like the following (automatically sorted by "Name" column)
Name Open Num Total Num
-----------------------------------
Doe, John 0 0
Smith, Sarah 4 3
Tyler, Rose 7 8
The second tr would look like this:
<tr id="1"><td class="N">Smith, Sarah</td><td class="O">4</td><td class="T">3</td></tr>
Where the row ID is a counter (first row = 0, second = 1, third = 2, etc.) and the cell class is to grab the column using jQuery ($(".O") gives Open Num column)
I'm trying to get the table to sort based off of the numerical columns (Open Num and Total Num). So output would look like this (sorted by Open Num or Total Num):
Name Open Num Total Num
-----------------------------------
Tyler, Rose 7 8
Smith, Sarah 4 3
Doe, John 0 0
So far, I store the numbers into an array arrQuick and I store the row number in a different array rowCount. I then use the Quick Sort method to sort the data, at the same time sorting the second array, which works perfectly. So now I have the sorted data and the order that my rows should be in.
The Problem
I cannot figure out how to get the table rows to update correctly.
So far I have this.
for(var i=0;i<rowCount.length;i++){
var tmpHolder=$("#"+i).html();
$("#"+i).html($("#"+rowCount[rowCount.length-(i+1)]).html());
$("#"+rowCount[rowCount.length-(i+1)]).html(tmpHolder);
}
When stepping through I can see that initially the updating is working. However, eventually it gets to some point rows are getting updated to places they shouldn't be and I'm not sure why.
You can sort the rows based on the values of table cells. The following method accepts a className of the cells and sorts the rows based on the text contents of that cells.
$.fn.sortTable = function(cls) {
this.find('tr').sort(function(a, b){
return $(a).find('td.'+cls).text() > $(b).find('td.' + cls).text();
}).appendTo(this.find('tbody'));
}
$('table').sortTable('O');
Updated method for supporting ascending and descending orders.
$.fn.sortTable = function (opt) {
var sort = typeof opt.sortType !== 'undefined' ? opt.sortType : 'asc',
cls = opt.cls;
if (!cls) return;
var $tr = this.find('tbody').children().sort(function(a, b){
return $(a).find('.' + cls).text() > $(b).find('.' + cls).text();
});
if (sort === 'des') {
$tr = $tr.toArray().reverse();
}
this.find('tbody').append($tr);
}
$('table').sortTable({
cls: 'N', // className of the cells
sortType: 'des' // order 'asc' || 'des'
});
http://jsfiddle.net/Af7mG/