Let us suppose that I have three checkboxes and each checkbox has certain items to be checked say Item 1 ,Item 2,Item 3 which has value 'a' ,'b','c'.So if we click on item1,item2 and mark them ,we get an array with values a and b .Now if we click item3,we would get c .Again if we go to another checkbox now and click on another item in another checkbox,we would get another array which holds the values for the items we marked in second checkbox .
I need to store all marked items in an array and display it .The problem is to get all the ticked items for all the different checkboxes.
will the following perhaps set you in the right direction?
var checkboxValues = [];
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
checkboxValues.push(checkboxes[i].value);
}
}
console.log('checkboxValues', checkboxValues);
Related
Suppose I have the following setting on a Google Sheet:
Column A containing a list of different items (Apple/Melon/Grapes), column B containing Data Validation drop-down menus with either option 1 or 2.
What I want is that if I select option 1 for any of the items, the value of the corresponding cell in column A is going to pasted in D2. If I select option 1 for another item, the value will be pasted in D3, and so forth, thus building a secondary list without leaving any blank cells in between. If I select option 2, the item should be ignored.
Ideally, the order of the items in column D would follow my actions chronologically, i.e. if the item in A3 is the first item I select option 1 for, then it shall be on the top of the column D list at all times, even if later on I select option 1 for A1 as well (which shall then sit on the second position of the D list).
Is it possible to be achieved?
You can do this via Apps Script with a simple onEdit trigger, using its event object.
Select Tools > Script editor to open the Apps Script editor.
Copy this function and save the project (check inline comments):
function onEdit(e) {
// Get the range that was edited (column, row, sheet, value):
var range = e.range;
var sheet = range.getSheet();
var col = range.getColumn();
var row = range.getRow();
var value = range.getValue();
var sheetName = "Sheet1"; // Name of your sheet (the tab) (please change if necessary)
// Check that edited column is B, edited value is 1, and edited sheet is "Sheet1":
if (sheet.getName() === sheetName && col === 2 && value === 1) {
// Get length of non-empty cells in column D:
var columnD = sheet.getRange("D:D").getValues().map(function(item) {
return item[0];
}).filter(function(item) {
return item !== "";
});
var firstEmptyRow = columnD.length + 1; // First empty row in column D
var itemToCopy = sheet.getRange(row, 1).getValue(); // Get value from column A to copy
sheet.getRange(firstEmptyRow, 4).setValue(itemToCopy); // Write value to column D
}
}
Now, every time you edit column B and the edited value is 1, the corresponding option from column A will get copied to column D.
Reference:
onEdit
Event objects: Edit
delete range D2:D and paste in D2 cell:
=FILTER(A:A; B:B=1)
I cant seem to figure out how to store unchecked checboxes value in my DB.
i have this in my view
{{Form::label('user_photo', 'Valodas',['class' => 'control-label'])}}
Latviešu {{Form::checkbox('val[]', 'Latviesu',false)}}
Angļu {{Form::checkbox('val[]', 'Anglu',false)}}
Krievu {{Form::checkbox('val[]', 'Krievu',false)}}
And here is my controller function to store data
if($req->input('val') == null){
$valoda = "";
} else {
$valoda = request('val');
}
And in my database im only getting the value of the values that are checked
I need the 3rd value so that in my update view i could set values to checked or unchecked for each value
If you have a column for each checkbox, you can do it this way;
$latviesu = array_has($req->val,'Latviesu')?1:0;
$anglu = array_has($req->val,'Anglu')?1:0;
$krievu = array_has($req->val,'Krievu')?1:0;
checking that the array from the request has the value
But to be honest, I would rename the checkboxes so that they are not in an array
Quick reminder in Laravel 5.6 You can check if checkbox is checked by
$request->has('yourCheckboxName'); / return true / false
I am trying to make a json array after getting each records from a grid in ExtJS 3.4.0. I want to add row number of grid as key of each row in JSON array.
var selected_value = [];
for (var i = 0; i < count; i++)
{
var rec = store.getAt(i);
selected_value[i] = rec.data;
final.push({
"i":selected_value[i],
})
}
What you do there is build an array of objects with each object containing one property called i and that property has the value of the row in it.
I guess you actually just wanted to have an array with the row objects in it, right?
final.push(selected_value[i]);
This will do the job already. No need to specify an object with associative indices.
If you're grabbing all the store's entries already or at least know the range (start and end index) you could just as well skip all the manual item picking and grab a readymade array already:
final = store.getRange();
or
final = store.getRange(from, to);
I have a dropdown that is being used to filter a list of data.
I also have a counter (underneath the dropdown) that shows how many results have been returned by the filter.
However, is it possible to append the counter to each option in the dropdown itself? So the dropdown options would look like this for example...
Show all (6)
Show x (2)
Show y (1)
Show z (3)
At the moment each option will always show the same number (see below), because it's only getting the current filter value...
Show all (6)
Show x (6)
Show y (6)
Show z (6)
Is it possible, or am I expecting too much from the filter?
Here is an example of what I mean: Example
You can get this as below:
Add the below function in your controller:
$scope.getCount = function(rating){
var count = 0;
var list = $filter('filter')($scope.data, {'rating' :rating}, true);
if(list != null){
count = list.length;
}
return count;
};
Update the code in your html as below:
ng-options="item.name + ' (' + (getCount(item.rating)) + ') ' for item in filterOptions.stores">
Updated Plunker:http://plnkr.co/edit/Q5Vs1mqomQJWmZdxRRxp?p=preview
Hope this helps!!
I have the classic setup of an NSTableView with the columns bound to various keyPaths of the arrangedObjects of an NSArrayController, and the NSArrayController bound to an array of dictionaries.
With this setup, selecting one or multiple rows in the tableview works automatically. The table view and array controller work together and it's easy to query the NSArrayController to get the list of selected objects.
One of my table columns contains NSButtonCells, the checkbox kind. Is there a way to use Cocoa Bindings to bind the checkbox in each row to that row's selection state ? I know that I could add another value to the NSDictionary representing each row, but that would duplicate the selection information that is already available in NSArrayController.
If it is necessary to do that, would also appreciate a quick sketch of your implementation.
Thanks
So, the answer to this is not for the faint of heart. The reason is you are trying to get NSTableView to do something it doesn't naturally want to do.
Start out by using cocoa's native NSTableView multiple selection behavior:
- clicking on a row selects it and deselects other rows
- holding control and clicking on a column toggles the selection state of that row only
Now, add a column of checkboxes. For this row, the rules are different:
- clicking on a checkbox toggles the selection state of that row only
This would be easy if we could capture clicks to the checkboxes and process them ourselves. Now we can, but the problem is that after we process them, they still get forwarded on to the NSTableView, altering the selection in the usual way. [Note: there may be some way to avoid this forwarding - if you know of one, please let me know]
So here's how you can (finally) accomplished this:
- add a "selectedInView" field to each object in the underlying object array. Add an observer to the associated NSArrayController for the keyPath: "selectedObjects". When the selection changes, set the selectedInView field accordingly for each object. Something like this:
if([keyPath isEqualToString:#"selectedObjects"]) {
// For table view checkbox's: keep "selectedInView" of song dictionaries up to date
[_arrayController.arrangedObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
BOOL sel = [_arrayController.selectedObjects containsObject:obj];
if([[obj objectForKey:#"selectedInView"] boolValue] != sel)[obj setValue:[NSNumber numberWithBool:sel] forKey:#"selectedInView"];
}];
Now comes the tricky part: the only time the checkboxes malfunction are when there is already a selection present. Here are the types of cases:
Setup: Row's 1,2,3 are selected. Checkbox clicked on row 4.
Result: Checkbox on row four is selected. Row four is selected. Row's 1,2,3 are deselected (because that's what NSTableView does naturally)
To solve this, whenever a checkbox is clicked you need to create a temporary array to remember the current selection, plus or minus the checkbox that just got clicked:
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
if([tableColumn.identifier isEqualToString:#"CheckBox"]) {
NSMutableDictionary *song = [_arrayController.arrangedObjects objectAtIndex:row];
if(!_tempSelectedSongs && _arrayController.selectedObjects) _tempSelectedSongs = [[NSMutableArray arrayWithArray:_arrayController.selectedObjects] retain];
if(_tempSelectedSongs) {
if([_tempSelectedSongs containsObject:song]) {
[_tempSelectedSongs removeObject:song];
} else if(![_tempSelectedSongs containsObject:song]) {
[_tempSelectedSongs addObject:song];
}
}
}
}
Now after the tableview has done it's selection processing, we want to set the selection to what it should be. There is a promising looking function that allows you to modify the tableview selection BEFORE it actually does the selecting. You can modify it like so:
- (NSIndexSet *)tableView:(NSTableView *)tableView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes {
NSMutableIndexSet *newSet = [NSMutableIndexSet indexSet];
if(_tempSelectedSongs) {
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
[_tempSelectedSongs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSUInteger index = [_arrayController.arrangedObjects indexOfObject:obj];
if(index != NSNotFound) [indexSet addIndex:index];
}];
proposedSelectionIndexes = indexSet;
[_tempSelectedSongs release]; _tempSelectedSongs = nil; [_tempSelectedSongsTimer invalidate]; [_tempSelectedSongsTimer release]; _tempSelectedSongsTimer = nil;
}
[proposedSelectionIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSProgressIndicator *progress = ((BDDiscreteProgressCell *)[[_arrayController.arrangedObjects objectAtIndex:idx] objectForKey:#"BDCell"]).progress;
if(!progress)
[newSet addIndex:idx];
}];
return newSet;
}
This works great, however there is a problem with the order in which the NSTableView delegate functions are called. Obviously we need the first function - where we setup the temporary array - to be called BEFORE the second function - where we use the information.
For whatever reason, it turns out that when you DE-select a checkbox, this is how things work,
but when you SELECT a checkbox, the opposite occurs. So for this case, you can add some more code to your above keyPath observer:
if([keyPath isEqualToString:#"selectedObjects"]) {
if(_tempSelectedSongs) {
_arrayController.selectedObjects = _tempSelectedSongs;
[_tempSelectedSongs release]; _tempSelectedSongs = nil;
}
// For table view checkbox's: keep "selectedInView" of song dictionaries up to date
[_arrayController.arrangedObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
BOOL sel = [_arrayController.selectedObjects containsObject:obj];
if([[obj objectForKey:#"selectedInView"] boolValue] != sel)[obj setValue:[NSNumber numberWithBool:sel] forKey:#"selectedInView"];
}];
}
Edit: turns out there is an additional case: if a single row is selected and it's checkbox is "unclicked," this does not automatically trigger a selectedObjects notification, so you must run a function on a timer to implement the new selection.