Jquery select2- how to select all search results - jquery-select2

Currently select2 allows me to select only one option at a time after the list is filtered with search term.
Ex: Lets say the list contains all US states and my search term is "New"
what I want is to be able to select all search results based on the search term, in this case New York, New Mexico, New Hampshire, New Jersey, I need to select all at once.
Is there a way?

did a work around, now its working
had to set placeholder with the search term value and then did another search on all the elements with javascript string includes function to get those elements.
function onSelectAll(clicked_id) {
var parentid = $('#'+clicked_id).parent().attr("id");
var search = $('#'+parentid+' .select2-search__field').attr("placeholder");
var elements = document.getElementById(clicked_id).options;
if(search){
for(var i = 0; i < elements.length; i++){
if(elements[i].value.includes(search)) {
elements[i].selected = true;
}
}
}
else {
for(var i = 0; i < elements.length; i++){
elements[i].selected = true;
}
}
var vv = $('#'+clicked_id).val();
$('#'+clicked_id).val(vv).trigger('change');
}

Related

Get every n number of elements in list Flutter Dart

I have a list of elements and I need to get a list containing the first element followed by every nth element afterwards. For example: given n = 3 and the list [banana, cherry, apple, pear, kiwi], I need to get the list [banana, pear]. I need this regardless of specific content, since the list depends on user input.
How do I do this using Dart?
You may access list in dart by providing an index like for example:
List<String> fruits = ["banana","cherry","apple","pear","kiwi"];
print(fruits[0]); // Will print to the console "banana";
On your case, you are trying to access index 0 and index 3 which is "banana" and "pear".
You may create a function that accepts an index like:
String getFruit(int index, List<String> fruits) => fruits[index];
print(getFruit[0]); // Will print "banana";
or if you need to actually get the specific ranges you may use:
List<String> fruits =["banana","cherry","apple","pear","kiwi"].getRange(0,4);
// Will give you "banana","cherry","apple","pear
You may check : https://api.dart.dev/be/180791/dart-core/List-class.html for more information.
Edited answer based off the comment:
List<String> getElements(List userInput, nIndex){
List elements = [];
for(int x = 0; x<userInput.length;x++){
if(x % nIndex == 0){
elements.add(userInput[x]);
}
}
return elements;
}
List fruits = ["banana","cherry","apple","pear","kiwi"];
print(getElements(fruits,2));
or you may try to look and use List.retainWhere() depending on your use case.
Dart has a great set of collection operators that make this type of problem pretty straightforward to solve. For example, we could do something like:
extension X<T> on List<T> {
List<T> everyNth(int n) => [for (var i = 0; i < this.length; i += n) this[i]];
}
main() {
final fruit = ["banana", "cherry", "apple", "pear", "kiwi"];
print(fruit.everyNth(3));
}
Output:
[banana, pear]
You can use this extension method, which will work on lists of any type:
extension GetEveryN<T> on List<T> {
List<T> elementsEveryN(int n) {
List<T> result = [];
for(int index = 0; index < length; index +=1) {
if(index % n == 0) {
result.add(this[index]);
}
}
return result;
}
}
Trying it in an example:
List<String> list = ["banana", "cherry","apple", "pear","kiwi"];
print(list.elementsEveryN(2)); // [banana, pear]

Google sheets QUERY function for joined cells

for example, I have the next table. And I want to query all values which have the first cell "Computer". I have tried QUERY formula "QUERY(A1,B3;"select B where A = 'Computer'")" but it returns only the first B value - Keyboard.
Is it possible to return all values? Thanks.
I understand that you want to read a table composed of different pieces like the one that you posted, and if a match is found on the left column the script will need to return the contents of the right column. If my assumption is incorrect, please forgive me. An example initial table can look like this one:
In the example we will return the rows that contain PRAESENT in the first column. In that case, you can use the following Apps Script:
function so61913445() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var data = sheet.getDataRange().getValues();
var matchData = [];
var indexColumn = 0; // Column A
var targetIndex = "PRAESENT";
for (var r = 0; r < data.length; r++) {
for (var c = 0; c < data[0].length; c++) {
if (c == indexColumn && data[r][c] == targetIndex) {
matchData.push(sheet.getRange(r + 1, c + 2, 3, 1).getValues());
}
}
}
for (var r = 0; r < matchData.length; r++) {
for (var c = 0; c < matchData[0].length; c++) {
sheet.appendRow(matchData[r][c]);
}
}
}
In the code, the first step is to declare some variables to read the sheet (using the methods SpreadsheetApp.getActive() and Spreadsheet.getActiveSheet()), the data (with Sheet.getRange() and Range.getValues() methods) and some settings like the target word to match. After that, the code iterates over the data and, if the target word is found, the code will add the contents of the right column into the final array. Finally, the code will repeat the iteration to write the data just under the table using Sheet.appendRow(). The final result will look like this:
Please, ask me any question if you still need some help.

List in sheet doesn't update list in form

I'm a beginner at this so apologies. I'm trying to have a sheet with data in column A be the options in a specific list in a form. Ultimately I would like many columns of data be the reference of specific lists. The form doesn't need to sync with the list. I'd prefer to do that by running script in sheet.
This is how far I am but it's only pulling cell A1.
Right now the form is simple and only has one drop down question.
Many thanks for any help.
var FORM_ID = '1Edo3yPmfPLQXPRff6z8oDsYW0Hp-Mm5SYLISPnyTXfo';
var LIST_SHEET_NAME = 'List';
var range = 'A:A';
var ITEM_ID = '983918997';
function updateListInForm() {
var values_ = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(LIST_SHEET_NAME)
.getRange(range)
.getValues();
values_[0][0] = values_[0][0].toString();
for (var i = 1; i < values_.lenght; i++) {
values_[0].push(values_[i][0].toString())
}
var form = FormApp.openById(FORM_ID);
var list = form.getItemById(ITEM_ID);
list.asListItem().setChoiceValues(values_[0]);
function getitemsofform() {
var form = FormApp.openById(FORM_ID);
var items = form.getItems(FormApp.ItemType.LIST);
for (var i in items) {
Logger.log('id: ' + items[i].getId() + ' title: ' + items[i].getTitle());
}
}
}
One more thing. Can anyone point me to a program or app that teaches google script? I would like to start from scratch. ie. this is what var means, this is how to write a simple function

How do i select a record in a grouped smartgwt listgrid?

I have site with a listgrid and a openlayers map with points. When i cklick on one of these, the application shall scroll and mark this record. This works with a standard listgrid, but with a grouped listgrid it does not work.
lg = new ListGrid();
lg.setWidth(330);
lg.setDataSource(ds1);
lg.setAutoFetchData(true);
lg.setSortField("KU_NAME");
lg.setGroupStartOpen(GroupStartOpen.ALL);
lg.setGroupByField("KU_NAME");
lg.setShowFilterEditor(true);
kuName = new ListGridField("KU_NAME", "Künstler Name",150);
// Standorte
ListGridField stdOrt = new ListGridField("STDORT_NR","Standort Nr.");
ListGridField oid = new ListGridField("OID","OID.");
lg.setFields(stdOrt,kuName,oid);
and the select:
String stdortOID = stdOrtOIDjso.toString();
ListGridRecord[] records = lg.getRecords();
int i;
for (i = 0; i < records.length; i++) {
if (records[i].getAttribute("OID").equalsIgnoreCase(stdortOID)){
break;
}
}
lg.deselectAllRecords();
lg.selectRecord(i);
lg.scrollToRow(lg.getRecordIndex(record));
the reason is that in the record is only the value of the group name and the other attributs are unavailable.
When grouping is enabled, all data are "transformed" into tree and listgrid itself contains data for groups so you have to look for your record in this tree. Replace last 3 lines with (modified) Vittorio Paternostro suggestion:
Tree tree = lg.getGroupTree();
if (tree != null) {
TreeNode node = tree.find("OID", stdortOID);
if (node != null) {
lg.selectSingleRecord(node);
lg.scrollToRow(getRecordIndex(node));
lg.markForRedraw();
}
}
Note: Instead of deselectAllRecords + selectRecord use simplified selectSingleRecord.
I had the same need and the following works fine for me. You can use getGroupTree() and search the desired property in it (column value) without worrying about grouping. Make sure you search for unique values (i.e. a unique key) to identify a precise node.
Tree tree = getGroupTree();
if (tree != null) {
TreeNode node = tree.find("property", "value");
if (node != null) {
selectSingleRecord(node);
scrollToRow(getRecordIndex(node));
markForRedraw();
}
}

Entity Framework lazy loading issue

I am trying to create a multiple choice questionnaire that is table-driven. There is a Question that has child Choices within each question.
When I iterate the listOfQuestions, the first SQL is executed. I thought by "including" Choices that this would prevent a secondary lookup from occuring when I loop though the Choices for the current Question, but it did not.
Why?
var listOfQuestions = (from q in journeyEastContext.Questions.Include("Choices")
orderby q.QuestionId
select new
{
Question = q,
Choices = q.Choices.OrderBy(c => c.Sequence)
});
foreach (var questionGroup in listOfQuestions)
{
Question question = questionGroup.Question;
Literal paragraph = new Literal
{
Text = "<P/>"
};
this.QuestionPanel.Controls.Add(paragraph);
Label QuestionLabel = new Label
{
Text = question.Text
};
this.QuestionPanel.Controls.Add(QuestionLabel);
//var sortedChoices = from choices in question.Choices
// orderby choices.Sequence
// select choices;
foreach (Choice choice in question.Choices)
{
Literal carrageReturn = new Literal
{
Text = "<BR/>"
};
this.QuestionPanel.Controls.Add(carrageReturn);
RadioButton choiceRadioButton = new RadioButton()
{
ID = String.Format("{0},{1}", question.QuestionId, choice.ChoiceId),
Text = choice.Text,
GroupName = question.QuestionId.ToString()
};
this.QuestionPanel.Controls.Add(choiceRadioButton);
}
}
It is because of the projection being part of the query.
select new
{
Question = q,
Choices = q.Choices.OrderBy(c => c.Sequence)
});
There are a few ways to approach the solution to this, the simplest would be
var quesitonsList = (from q in journeyEastContext.Questions.Include("Choices")
orderby q.QuestionId).ToList();
var listOfQuestions = from q in questionsList
Select new
{
Question = q,
Choices = q.Choices.OrderBy(c => c.Sequence)
});
This would tell EF to execute the first query (with the Choices property eagerly loaded) and then let you run through your iteration without having extra queries fired off.
.Include and .Select do not mix because of the type of query being generated in T-SQL. Basically projections use inner select statements and eagerly loaded properties use denormalization and joins to flatten the record set.

Resources