I have a table form mysql in my html page, and each row has a checkbox, if I checked some checkboxes , how could I output the rows I checked.
now I konw how to output all the rows, but I don't konw how to selected the rows I checked
give all the check boxes the same name and assign each one with a different value, say the rows primary key.
Use request.getParameterValues() in the servlet and you will get an array which will have only the checked values
Have more you can get checked table rows in javascript. Examples:
var allRows = $('#yourTableId tr');
for(var i=0; i<allRows.length; i++) {
if(allRows[i].find('input:checkbox').checked) {
//selected row allRows[i]. And you can get value checkbox allRows[i].find('input:checkbox').value
}
}
or use each loop jquery:
$('#yourTableId tr').each(function(index)) {
if($(this).find('input:checkbox').checked) {
//selected row $(this). And you can get value checkbox $(this).find('input:checkbox').value
}
}
And have more answers in stackoverflow for such issues:
Using jquery to get all checked checkboxes with a certain class name
Jquery find a checkbox inside a table row
How to check whether a checkbox is checked in jQuery?
Related
I want a report with a few selected columns from a sheet.
The selection will be as per the user input like "1,4,7,12" for including column 1,4,7 and 12 in the report.
Or the input can be "2,3" to include column 2 and 3 in the report.
//user input is saved in variable input "1,4,7,8" or "2,4" or "3,2,5"
//
var jobvals = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
var flds=input.split(",");
var tstr="";
for (i=1;i<jobvals.length;i++){
for (s=0;s<flds.length;s++){
//tstr+=jobvals[i][flds[s]]+","; //I can make a comma separated string for each row. But, I want an array for further processing
//getting stuck here - how to make an array
}
tstr+="\n";
}
I want to make a new array with the selected columns. The number of columns in the result array will vary. And, the column selection will also vary.
Appreciate some ideas how to achieve this.
I got help from
Best method to extract selected columns from 2d array in apps script
Google Script GetRange with Filter based on values in a column AND select only certain columns
Solution
function extract_some_columns{
var jobvals = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
var new_array = getCols(jobvals,'['+input+']');
//new_array has only column 1,3,5 (as specified in the variable input = "1,3,5")
}
function getCols(arr,cols) {
return arr.map(row =>
row.filter((_,i) => cols.includes(++i)))
}
You can declare an empty array and add each element one by one by using push() function on an array. Your code should look like this:
var jobvals = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
var flds=input.split(",");
var tstr=[];
for (i=1;i<jobvals.length;i++){
for (s=0;s<flds.length;s++){
tstr.push(jobvals[i][flds[s]]);
}
}
I've given up on trying to manage my Gmail by Categories as they don't filter well in third-party email tools on my phone, so I've resorted to filtering all of my newsletters, blog posts, coupons, etc. into labels and sub-labels.
I have a GScript function to delete labels after a certain amount of days, but I can't figure out how to have the code include the parent/child label as well.
For example, I have a label called "-Newsletters" and a bunch of sub-labels for the type of newsletter (i.e., -Newsletters/Blogs, -Newsletters/Coupon, -Newsletter/News, etc.). The code I have shown below works great when I'm searching for the exact label, but I'm trying to modify it so that it will run the code on all parent/child labels I may set up in the future.
Is there a way to do this with the GmailApp.search function? or do I just need to repeat the code for each separate search string?
The code below deletes all emails with a label "-Newsletters" older than 3 days, but not
"-Newsletters/Blogs" or "-Newsletters/News". I'm not sure how I would setup a variable to capture the child label name or how to place that variable inside the GmailApp.search function.
Thanks in advance.
function cleanUpNewsletterLabel() {
var delayDays = 3 // Enter # of days before messages are moved to trash
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var threads = GmailApp.search('label:-Newsletters -is:starred'); // filter all emails with -Newsletters label not starred
for (var i = 0; i < threads.length; i++) {
if (threads[i].getLastMessageDate()<maxDate)
{
threads[i].moveToTrash();
}
}
}
I don't think there is a search operator that you can use to get all child labels (only exact match seems to be supported).
What you can do instead, is retrieve all the child labels the following way:
Retrieve all labels with getUserLabels().
Filter this labels with the condition that their names start with the parent label name, since child label names have this format: {parentLabelName}/{childLabelName}/.... Here, you could use filter() and startsWith().
For each filtered label, search the corresponding threads.
It could be something like this:
const parentLabelName = "-Newsletters";
const labels = GmailApp.getUserLabels();
const filteredLabels = labels.filter(label => label.getName().startsWith(parentLabelName));
filteredLabels.forEach(filteredLabel => {
const threads = GmailApp.search(`label:${filteredLabel.getName()} -is:starred`);
// Delete threads (your code)
});
I am using checkboxes in a sheet (and their functionality in themselves are exactly what I need).
The problems I am having is that the users who are able to edit the sheet, sometime (by mistake) delete the checkbox instead of simply checking/un-checking the checkbox.
Hence, I want the users to use the checkboxes, but not be able to delete them.
Is this possible somehow?
FYI: it is not possible to use Google Forms in the case.
Here you have an example on how to achive it using onOpen triggers
function onOpen(e)
{
var range = SpreadsheetApp.getActive().getSheets()[0].getRange(1,4,5);
var values = range.getValues();
for ( var val in values ) {
if( values[val] != true && values[val] != false ) {
values[val]= false;
}
}
range.insertCheckboxes(values);
}
You have to specify where are the checkboxes placed, in the code example provided there are 5 of them, starting from row 1 to row 5, and they are placed on the 4th column, so called D.
This is specified on the getRange(1,4,5) function:
getRange(InitialRow, Initial Column, Number of Rows)
for more documentation around the use of getRange() use the following reference.
This onOpen function will be executed every time that the spreadsheet is opened, and if on the specified fields there aren't false or true statements, it means that we lost the checkbox somehow, so it will introduce them again.
I have a model containing a list and I'm using Razor to display the list items onscreen in a standard html table. A user can edit some parts of these items and save them, or they can add extra lines to the table.
For example, a master record may have 2 child records, the user can edit the 2nd record, then add a 3rd, then save and all the data is bound to the model as expected and is saved back to the database. This all works fine
The issue occurs when the user tries to delete a child record which is not the last record in the list. I'm using the following code to delete a child record, where ctrl is a delete icon which appears in each row.
deleteExtraLine: function (ctrl) {
//get the table to which the ctrl belongs
var table = $(ctrl).closest("table");
//remove the row from the table
$(ctrl).closest('tr').remove();
}
The issue I'm having is all rows after the one which was removed from the table are no longer bound to the model on the postback when saving.
ie. there are 5 rows, row 3 is deleted, but 4 and 5 are also deleted as they are missing from the model.
Is there some way of removing a row from the table which isn't going to affect the model in this way?
Ok, so I worked it out not long after I posted this.
The numbering of the name attribute needed to be contiguous so as soon as there was a gap it lost everything subsequent to that. So I had to write a function to make sure the number on the name attribute was in sequence
updateRowIndex: function(rows, index) {
//index will start at the first row to be updated
$(rows).each(function() {
//get all the inputs and set the appropriate attribute values (this doesn't select the hidden ones but they don't matter anyway)
var inputs = $(this).find("input");
$(inputs).each(function () {
//change the value for id
var oldId = $(this).attr("id");
var newId = oldId.replace(/\[[0-9]?[0-9]\]/ig, "[" + index + "]"); //eg replace all occurrences of [2] with [1];
$(this).attr("id", newId);
//change the value for the name attribute
var oldName = $(this).attr("name");
var newName = oldName.replace(/\[[0-9]?[0-9]\]/ig, "[" + index + "]"); //eg replace all occurrences of [2] with [1];
$(this).attr("name", newName);
});
index++;
});
}
I have already implemented full row update, but before updating the rows I need to get which columns have been edited and the respective data present in the columns. In order to perform some validations on the data for displaying an error message before updating the row, I am using (rowValueChanged)="onRowValueChanged($event)" method.
I believe to accomplish this, you would need to listen on both the rowValueChanged, cellValueChanged events
and add a flag with the edited value
onRowValueChanged(event) {
console.log(`Changed Values = ${event.node.changedValues.join(',')}`);
// do validations
}
onCellValueChanged(event) {
if (event.newValue !== event.oldValue) {
if (!event.node.changedValues)
event.node.changedValues = [];
event.node.changedValues.push(event['column']['colId']);
}
}