I'm trying to create ListView which contains ASPxTextBox that the user can enter only numeric value. There are many rows and columns of data. I want to add new row and column that calculate total value from each row and column
For example, if I have data like this:
Student Name | Subject1 | Subject2 | Subject3
----------------------------------------------------------
John ----------- | ------- 23 | --------45 | --------37
Smith ---------- | ------- 41 | --------22 | --------11
Linda ---------- | ------- 35 | --------38 | --------23
I want to be like this
Student Name | Subject1 | Subject2 | Subject3 | Total
------------------------------------------------------------------
John ----------- | ------- 23 | --------45 | --------37 | 105
Smith ---------- | ------- 41 | --------22 | --------11 | --74
Linda ---------- | ------- 35 | --------38 | --------23 | --96
------------------------------------------------------------------
Total----------- | --------99 | -------105 | --------71 | 275
The number of columns (Subject) is fixed but the number of rows depends on data in database.
There are 2 main problems here:
1. I want the summation row and column to execute calculation in client side using javascript. So when one of textboxes is lost focus, it will recalculate the summation value in each row and column.
2. I can't access the value from ASPxTextBox using javascript. I tried using:
var test = document.getElementById('<%# Container.FindControl("txt_Subject1").ClientID %>')
var data = test.value;
but it doesn't seem to work.
The following code also cause error:
var test = document.getElementById('<%# Container.FindControl("txt_Subject1").ClientID %>')
var data = test.GetValue();
And when I tried ClientInstanceName, it always return the value of the last row only:
var data = txt_Subject1.GetValue(); //doesn't work?
Also, I don't want to change ASPxTextBox to asp TextBox because I want to use some of its feature.
You could dinamically assign ClientInstanceName values based on row number:
txt_Subject1_1
txt_Subject1_2
...
txt_Subject1_n
Then use javascript to calculate sum:
var sum = 0;
for (var i = 1; i <= rowCount; i++)
{
sum += parseInt(window['txt_Subject1_' + i].GetValue());
}
Related
I currently have to manually enter Sheet1 column B, but I'd like to auto increment it every time the script runs.
I am using a Google Sheets script.
This part is working.
How it works:
Sheet2 column A is a drop down list imported from Sheet1 column A
if Sheet2 column B is anniversary of todays date Sheet 2 column F is automated to be 'reset'
the working script then deletes the rows in Sheet 2 where F is 'reset' and adds new rows for each to the bottom
Currently using this script
function writeupReset() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet2');
var r = s.getRange('C:C');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--){
if(v[0,i]=='reset'){
s.insertRowsAfter(251,1);
s.deleteRow(i+1);
}
}
}
Example of the working bits using todays date - 6/2/20
Sheet1 Sheet2 (before script) Sheet2 (after script)
A B A B C A B C
|---------------| |-------------------------| |-------------------------|
1 | name | resets | 1 | name | date | delet | 1 | name | date | delet |
|---------------| |-------------------------| |-------------------------|
2 | bill | 1 | 2 | bill | 6/2/19 | reset | 2 | alex | 9/5/19 | |
|---------------| |-------------------------| |-------------------------|
3 | mark | 5 | 3 | alex | 9/5/19 | | 3 | adam | 11/6/19 | |
|---------------| |-------------------------| |-------------------------|
4 | holy | 2 | 4 | adam | 11/6/19 | | 4 | tony | 12/1/19 | |
|---------------| |-------------------------| |-------------------------|
5 | tony | 0 | 5 | mark | 6/2/19 | reset | 5 | | | |
|---------------| |-------------------------| |-------------------------|
6 | alex | 2 | 6 | tony | 12/1/19 | | 6 | | | |
|---------------| |-------------------------| |-------------------------|
7 | june | 1 | 7 | | | | 7 | | | |
|---------------| |-------------------------| |-------------------------|
8 | jack | 0 | to 252 rows (last two hidden) to 252 rows (last two hidden)
|---------------|
9 | adam | 2 |
|---------------|
So how can I edit the script to automatically increment Sheet1 column B when the script deletes those 'reset' rows in Sheet1?
I believe your goal as follows.
You want to increase the values of the column "B", that the name is the same , in "Sheet1" when the rows with reset in "Sheet2" are deleted by the script.
In your case, the same names are included in the column "A" in "Sheet2".
You want to achieve this by modifying your script in your question.
For this, how about this answer?
Modification points:
In this case, it is required to know the values of "Sheet1" and compare them with the values of "Sheet2". The flow is as follows.
Delete rows in "Sheet2".
At that time, an object including the information of the deleted rows is created.
Increase values of the column "B" in "Sheet1".
The values of the column "B" are updated using the object, and the updated values are put to the sheet.
When above points are reflected to your script, it becomes as follows.
Modified script:
function writeupReset() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// 1. Delete rows in Sheet2.
var s = ss.getSheetByName('Sheet2');
var r = s.getRange('A1:C' + s.getLastRow()); // Modified
var v = r.getValues();
var obj = {}; // Added
for(var i=v.length-1;i>=0;i--){
if(v[i][2] == 'reset') { // Modified
s.insertRowsAfter(251,1);
s.deleteRow(i+1);
var name = v[i][0] // Added
obj[name] = obj[name] ? obj[name] + 1 : 1; // Added
}
}
// I added below script.
// 2. Increase values of the column "B" in Sheet1.
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("A2:B" + sheet.getLastRow());
var values = range.getValues().map(([a, b]) => ([obj[a] ? b += obj[a] : b]));
range.offset(0, 1, values.length, 1).setValues(values);
}
Note:
In this modified script, it supposes that each value of the column "A" in "Sheet1" is the unique value in all values. From your question, I could understand like this.
If the sheet names of "Sheet1" and "Sheet2" are different from your actual situation, please modify them.
If your actual situation is different from the structure of your sample values in your question, this modified script might not work. So please be careful this.
References:
map()
setValues(values)
I'm working on aggregating my city's main big intersections with my city's collision data. What am trying to accomplish is to determine the number of accidents which happened in and around the intersection within a 20 meters radius.
Luckily, I'm already far off in the project and I have two tables in my database intersections and collisions
My intersections table:
--------------------------------------------
| id | city | Latitude | Longitude |
--------------------------------------------
| 1 | 1 | 34.44444 | 84.3434 |
--------------------------------------------
| 2 | 1 | 42.4666667 | 1.4666667 |
--------------------------------------------
| 3 | 1 | 32.534167 | 66.078056 |
--------------------------------------------
| 4 | 1 | 36.948889 | 66.328611 |
--------------------------------------------
| 5 | 1 | 35.088056 | 69.046389 |
--------------------------------------------
| 6 | 1 | 36.083056 | 69.0525 |
--------------------------------------------
| 7 | 1 | 31.015833 | 61.860278 |
--------------------------------------------
MY collisions table:
--------------------------------------------
| id | cause | Latitude | Longitude |
--------------------------------------------
| 1 | 1 | 44.44444 | 81.3434 |
--------------------------------------------
| 2 | 1 | 32.4666667 | 1.4666667 |
--------------------------------------------
| 3 | 1 | 42.534167 | 63.078056 |
--------------------------------------------
| 4 | 1 | 46.948889 | 62.328611 |
--------------------------------------------
| 5 | 1 | 45.088056 | 61.046389 |
--------------------------------------------
| 6 | 1 | 46.083056 | 63.0525 |
--------------------------------------------
| 7 | 1 | 41.015833 | 69.860278 |
--------------------------------------------
Note: Some fields were omitted for simplicity sake
As you can see, both tables posses latitude and longitude fields. Now to determine if intersection and collision coordinates are close, I thought of using a MySQL query that takes an id of an intersection which then queries the collisions table to get all collisions within 20 meters of the intersection.
What is this query (in MySQL or in Sequelize)?
Here's what I have now (it's in Sequelize using a MySQL database)
// api/collisions/{intersection_id}/count
exports.intersection_accident_count = (req, res) => {
intersection.findOne({where: {equipement: req.params.intersection_id}}).then(intersection => {
let intersectionLongitude = intersection.longitude;
let intersectionLatitude = intersection.latitude;
collision.count({where: {
longitude: {
$gt: intersectionLongitude
},
latitude: {
$gt: intersectionLatitude
},
}}).then(count => {
res.status(200).json({'number_of_accidents': count});
});
});
};
You should start with straightforward way:
SELECT
i.*
FROM
intersections AS i
INNER JOIN collisions AS c ON (
ST_Distance_Sphere(POINT(i.Longitude, i.Latitude), POINT(c.Longitude, c.Latitude)) < #dist
)
This should give you results, however the query will not use any indexes at all and if you have many records it would be very slow.
You should think of adding geometry data types in your table:
ALTER TABLE `collisions` ADD COLUMN `Location` POINT;
UPDATE `collisions` SET Location = POINT(Longitude, Latitude);
ALTER TABLE `intersections` ADD COLUMN `Location` POINT;
UPDATE `intersections` SET Location = POINT(Longitude, Latitude);
You can add trigger so the Location will be populated automatically when you updating Longitude/Latitude.
Then:
SELECT
i.*
FROM
intersections AS i
INNER JOIN collisions AS c ON (
ST_Distance_Sphere(i.Location, c.Location) < #dist
)
This would be a bit faster.
If you'd like to make it even faster, you can add another geometry poligon Area column into your intersection table and 'pre-build' areas (can be round for accuracy or square for speed).
After that you will be able to do something like this:
SELECT
i.*
FROM
intersections AS i
INNER JOIN collisions AS c ON (
ST_WITHIN(c.Location, i.Area)
)
I have a table which looks like the example i have provided below with this data
Table
T som da pap
------------------------------
A | soma | label | nami |
----------------------------
B | a:b | acha | wen:nda |
-----------------------------
C | d | k | nd:ke |
-----------------------------
Using the data in javascript/jquery i would like to
create a table like this based on count of pap
T som da pap
------------------------------
A | soma | label | nami |
----------------------------
B | a | acha | wen |
-----------------------------
B | b | acha | nda |
-----------------------------
C | d | k | nd |
-----------------------------
C | d | k | ke |
-----------------------------
Suggestions, I have tried but so far the double data i can only put in td instead of tr
Assuming there are always the same number of items in both cells.
$("#tableID tr").each(function() {
var som = $(this).find("td:eq(1)").text();
var som_array = som.split(':');
if (som_array.length > 1) {
var pap_array = $(this).find("td:eq(3)").text().split(':');
$.each(som_array, function(i, som_el) {
var pap_el = pap_array[i];
var new_row = $(this).clone();
new_row.eq(1).text(som_el);
new_row.eq(3).text(pap_el);
$(this).after(new_row);
}
$(this).remove();
}
});
This loops over all the rows in the table, and checks whether the som column contains multiple items. If it does, it split it up, and also splits up the pap column. Then it loops over these values, makes a copy of the original row, and replaces those columns with the elements of the split strings. Finally it removes the original row.
I have a tables filled with data using Jquery Datatables, what i want is to be able to sum all data from the distinc values of a column but when a dropdownlist value is changed and save the sum value into a new Html Input with Javascript Example:
I got my table filled with data like this:
|Platillo | PDV | Precio | Can | Date |
------------------------------------------------------------------
| Poz | REST | 40 | 2 | 01/02/2016|
------------------------------------------------------------------
| Tor | REST | 50 | 2 | 02/02/2016|
------------------------------------------------------------------
| Zes | REST | 100 | 2 | 01/02/2016|
------------------------------------------------------------------
| Poz | FUEM | 60 | 2 | 01/02/2016|
------------------------------------------------------------------
| Tor | FUEM | 70 | 2 | 01/02/2016|
------------------------------------------------------------------
| Zes | FUEM | 120 | 2 | 01/02/2016|
------------------------------------------------------------------
| Poz | VTSI | 45 | 2 | 02/02/2016|
------------------------------------------------------------------
| Tor | VTSI | 57 | 2 | 01/02/2016|
------------------------------------------------------------------
| Zes | VTSI | 10 | 2 | 02/02/2016|
------------------------------------------------------------------
i want is to be able to sum all the values from the column 'Precio' into a 'Html Input Field' but i want to sum the totals of each distinct value from column 'PDV', and triggered by the value of a dropdown list,
Ex: if user select's Date '01/02/2016' i shoul fil my html Inputs with the sum of the data of all distinct value of column 'PDV' but only when they match the Date selected by the user '01/02/2016' looking for this output:
<option value="">Todos</option>
<option value="01/02/2016">#item</option> // USER SELECTS THIS VALUE
<option value="02/02/2016">#item</option>
My input field are like follow:
<input value="//TOTAL SUM FROM REST WHERE DATE IS EQUAL AS UsER SELECT '01/02/2016'" />
<input value="//TOTAL SUM FROM REST WHERE DATE IS EQUAL AS UsER SELECT '01/02/2016'" />
<input value="//TOTAL SUM FROM REST WHERE DATE IS EQUAL AS UsER SELECT '01/02/2016'" />
Im new to javascript and right now i have only be able to filter my Datatable based on the selected date but i need to sum all distincs values of my 'PDV' column each time user selects a date value how can i do it?
UPDATED:
i have the code of my dropdown filter y just need to know how to sum columns based on unique column items on my search javascript
This is my search code:
$('#sel0').on('change', function () {
dataTable.columns('.fechas').search(this.value).draw();
var datafecha = $(this).val();
});
You need to write a custom filter. Or take a look at this below to get an idea :
var val = $.fn.dataTable.util.escapeRegex($('dropdown').val());
column.search( val ? '^'+val+'$' : '', true, false ).draw();
var sum = column.data().unique().sum();
alert(sum);
I have Table (Employees) A:
+----+---------+
| ID | NAME |
+----+---------+
| 1 | ROBERT |
| 2 | JAMES |
| 3 | RICHARD |
| 4 | KANYE |
| 5 | DYLAN |
| 6 | JOHN |
| 7 | JEAN |
| 8 | LOKI |
| 9 | ADAM |
+----+---------+
And Table (Employees) B:
+----+---------+
| ID | NAME |
+----+---------+
| 1 | ROBERT |
| 2 | JAMES |
| 3 | RICHARD |
| 4 | KANYE |
| 5 | DYLAN |
+----+---------+
Those people are my "employees". The names from table B are all the employees on my database and the names from table A are all the employees that I've assigned to project n°1.
I would like to HIDE all names in Table B that already appear in Table A because I don't want to re-assign the same employee to project n°1.
How can I resolve this using jQuery?
a clientside check would be:
function compareLists(listA, listB){
var resultList = [];
listA.forEach(function(itemA){
listB.forEach(function(itemB){
if (itemB.name !== itemA.name)resultList.push(itemB);
}
}
return resultList;
}
or at the serverside (database):
select * from table_b where name not in (select name from table_a)
greetings
Normally you should do that on the server side using SQL as already answered. However, since you ask about javascript. This is the way to filter it at client side:
//collect list of names in A
var table = document.getElementById("TableA");
var names = []
for (var i = 0, row; row = table.rows[i]; i++) {
names.push(row.celss[1]);
}
//check names in B
table = document.getElementById("TableB");
for (var i = 0, row; row = table.rows[i]; i++) {
if(jQuery.inArray(row.cells[1], names) !== -1){
//show row
}else{
//hide row
}
}
I dont have that much knowlwdge in Jquery..
But this query increases perfomance in a large DB.
SELECT
FROM TableB
LEFT JOIN TableA ON TableB.name=TableA.name // id will be better than name
WHERE TableB.name IS NULL
Hope this helps