I'm new to grails 1.3.7 and I try to access my database and to show my data on a gsp. Now Ive got the following problem: I've got a list of questions (listofQuestions) and a list of answers (listofAnswers). To each question belongs one Lpicanswer object which contains various answers (answera, answerb)
So when I create those lists, in the end I've got one list containing the questions and one list containing lpicanswer-objects. each lpicanswerobject has an lpicid (which is the id of the question), so that they are related to each other.
Here is the code to create those lists:
List listofQuestions = []
List listofAnswers = []
def ques
def question
def ans
// we create a questions list containing questions
// we create a answers list containing answers-objects for a question
for (int i = 0; i <= cacheService.questionList.size()-1; i++) {
ques = Lpicquestions.get(cacheService.questionList[i]);
question = ques.question;
listofQuestions.add(question);
}
for (int i = 0; i <= cacheService.questionList.size(); i++) {
ans = Lpicanswers.get(cacheService.questionList[i]);
listofAnswers.add(ans);
}
return new ModelAndView("/result/resultdetail", [ qlist : listofQuestions, alist : listofAnswers ]);}
now I want to show them on my gsp. here is what I do:
<g:each in="${qlist}">
<b>${it}</b><br/>
${alist.answera}<br/>
${alist.answerb}<br/>
${alist.answerc}<br/>
${alist.answerd}<br/>
${alist.answere}<br/>
${alist.answerf}<br/>
${alist.answerg}<br/>
${alist.answerh}<br/>
</g:each>
what happens is, that the questions are given out correct, but the answers of course not. For each question all answersa, all answersb, etc are shown (like: [answera-from-question1, answera-from-question2] and so on) how can I solve this?
any help will be apreciated! :-)
[EDIT] Here is the structure of lpicquestions and lpicanswers, thanks for helping!! :-)
package com.lpic
class Lpicquestions {
int lpicchapter
String question
static constraints = {
question(nullable:false, blank:false, maxSize:1000)
lpicchapter(nullable:false, blank:false)
}
}
package com.lpic
class Lpicanswers {
Lpicquestions lpicid
String answera
String answerb
String answerc
String answerd
String answere
String answerf
String answerg
String answerh
static constraints = {
}
}
aList is not an object or map. So you can't put something like:
${alist.answera}
change the view to.
<g:each var="question" in="${qlist}">
<b>${question}</b><br/>
<g:each var="answer" in="${aList}">
<g:if test="${answer.lpicid?.question == question}">
<b>${answer.answera}</b><br/>
<b>${answer.answerb}</b><br/>
<b>${answer.answerc}</b><br/>
<b>${answer.answerd}</b><br/>
<b>${answer.answere}</b><br/>
<b>${answer.answerf}</b><br/>
<b>${answer.answerg}</b><br/>
<b>${answer.answerh}</b><br/>
</g:if>
</g:each>
</g:each>
if assuming that cacheService.questionList contains list of id for Lpicquestions
change
for (int i = 0; i <= cacheService.questionList.size(); i++) {
//ans = Lpicanswers.get(cacheService.questionList[i]);
ans = Lpicanswers.findWhere(['lpicid' : Lpicquestions.get(cacheService.questionList[i])]);
listofAnswers.add(ans);
}
Related
class Article{
final String id;
final List<ArticleArray> arrays;
}
class ArticleArray {
final String id;
final String array;
}
TextFormField(
onChanged: (_searchinput) {
List<String> searcharray = _searchinput.split(',');
),
_articlesForDisplay = _articles.where((article){
for(int i = 0; i < article.arrays.length; i++)
{
searchinit = article.arrays[i].arrays.toLowerCase();
}
return searchinit.contains(
RegExp(//necessary code here, caseSensitive: false),
);})
basically what i am trying to do is i am trying to search
array by array like:
-Textformfields follows the user input and every time "," is entered,i
put that word into "searcharray".
-Then i initiate a search in my function(total code is not needed to write here,
i included necessary functions),i try to use "regexp" function to search for the arrays
in my article archives.
Example:
articles[0].arrays[0].array = 'a1'
articles[0].arrays[1].array = 'a2'
articles[1].arrays[0].array = 'a1'
articles[1].arrays[1].array = 'b2'
searcharray = ['a1','b2']
Here,basically what i try to do is i try to search my article arrays by every list of arrays as possible so it matches the right one. Thats what i want.
Not sure I understand your specific requirements.
Here, I define a RegExp that I try to match to at least one item of my data.
void main() {
List<String> data = ['a1','b2', 'c3', 'a4', 'a5', 'd6'];
RegExp exp = new RegExp(r"(a\d+)");
print(data.any((item) => exp.hasMatch(item)));
}
This is the point.
I've followed the suggest linked here More efficient way to append a list item after some text in Google Docs ,whose very helpfull tom me. But i have the problem mentioned at the bottom.
My placeholder is inside a known table within a google doc.
I've tried several ways to figure it out, but i didn't achieve.
Here below there is the portion of code of interest.
Note:
"CarattCentralGas1" is an array of values;
"DescrizioneCentralGas" is another array of values;
My intent is to insert a list, created if the if condition is true, at the placeholder place and than remove it.
Any ideas would be great!`
for (var i=0; i < CarattCentraleGas1.length; ++i)
{ var ValueToTest = CarattCentraleGas1[i][0];
Logger.log(ValueToTest)
if (ValueToTest ==='Presente')
{ var valueToextract = DescrizioniCentraleGas[i][0];
var search = '%Placeholder%';
var Table1 = body.getTables()[0];
var found = Table1.findText(search);
while (found) {
var elem = found.getElement().getParent().getParent();
var index = Table1.getChildIndex(elem);
Table1.insertListItem(index+1, valueToextract+';');
found = Table1.findText(search, found);}
Logger.log(valueToextract)}
}
var Table1Text = Table1.editAsText().replaceText('%CarattCentrale1%',"");
`
this is the template document I want to edit with the script.
I have a list of sObject elements. I want to remove the duplicate element having the same record name along with the original record .
Like suppose if I have a list of element having record names as
Chair1,Chair2,Chair3,Chair4,Chair5,Chair6,Chair7,Chair1,Chair2
I want to print a list having only the elements that have no duplicates.For this case I should get the list Chair3,Chair4,Chair5,Chair6,Chair7.
I am using the below code to achieve this functionality.But I am getting thw records as : Chair1,Chair2,Chair3,Chair4,Chair5,Chair6,Chair7.
In ideal case we should not get the records Chair1,Chair2 as these already have duplicate records.
List <Chair__c> chairList = [SELECT
ID,
Name
FROM Chair__c
ORDER BY Name ASC];
System.debug('chairListOrderbyName::'+chairList);
List <String> chairNameList = new List <String>();
for(Integer i = 0; i < chairList.size();i++) {
for(Integer j = 0;j < chairList.size();j++) {
if(chairList[i].Name.equalsIgnoreCase(chairList[j].Name) && i != j) {
chairList.remove(i);
chairList.remove(j);
}
}
}
System.debug('chairList::'+chairList);
If names is really all you need you could do it with pure SOQL using GROUP BY and HAVING. Something like
SELECT Name
FROM Chair__c
GROUP BY Name
HAVING COUNT(Id) = 1 // only unique entries
If you need full sObjects then I'd make a helper Set<String> and loop through results. If name isn't in the set - add it. But if it already is there -> remove it!
Actually let's make it Map, similar idea...
Map<Set, Chair__c> chairs = new Map<Set, Chair__c>();
for(Chair__c c : [SELECT ...]){
if(chairs.containsKey(c.Name)){
chairs.remove(c.Name);
} else {
chairs.put(c.Name, c);
}
}
System.debug(JSON.serializePretty(chairs));
System.debug(chairs.values());
Try using a Set<String> instead of List<String>.Refer below solution
List <Chair__c> chairList = [SELECT
ID,
Name
FROM Chair__c
ORDER BY Name ASC];
Set<String> chairNameSet = new Set<String>();
for(String item: chairList ) {
chairNameSet.add(item);
}
It made more sense in a particular case today to use a standard for (i = 0.. loop rather than a forEach, but I realised I don't know how to access objects of an Ember Array by number.
So lets say we have:
var order = this.get('order');
var orderItems = order.get('orderItems');
orderItems.forEach(function(orderItem) {
orderItem.set('price', 1000);
});
I thought I could do the equivalent as:
var order = this.get('order');
var orderItems = order.get('orderItems');
for (i = 0; i < orderItems.get('length'); i++) {
orderItems[i].set('price', 1000);
}
but I get orderItems[0] is undefined etc.
How do I access the nth element in an ember array in js?
Ember.js Array provides a objectAt method for accessing the nth element, which you can use for iteration.
Your updated code would look like :
var order = this.get('order');
var orderItems = order.get('orderItems');
for (i = 0; i < orderItems.get('length'); i++) {
orderItems.objectAt(i).set('price', 1000);
}
Check out it's documentation here: http://emberjs.com/api/classes/Ember.Array.html#method_objectAt
I have found successes using ideas of immutable List but I am stumped when come to this piece of code here. I find myself has written something more Java than of Scala style. I would prefer to use List(...) instead of Buffer(...) but I don't see how I can pass the same modified immutable List to the next function. guesses is also modified within eliminate(...).
Any suggestions to help me to make this the Scala way of doing this is appreciated. Thanks
val randomGuesses = List(...) // some long list of random integers
val guesses = randomGuesses.zipWithIndex.toBuffer
for ( s <- loop()) {
val results = alphaSearch(guesses)
if (results.size == 1) {
guesses(resultes.head._2) = results.head._1
eliminate(guesses, resultes.head._2)
}
else {
val results = betaSearch(guesses)
if (results.size == 1) {
guesses(resultes.head._2) = results.head._1
eliminate(guesses, resultes.head._2)
} else {
val results = betaSearch(guesses)
if (results.size == 1) {
guesses(resultes.head._2) = results.head._1
eliminate(guesses, resultes.head._2)
}
}
}
}
Here are some general tips since this might be better suited for codereview and the code posted is incomplete with no samples.
You can use pattern matching instead of if and else for checking the size.
results.size match{
case 1 => ... //Code in the if block
case _ => ... //Code in the else block
}
Instead of mutating guesses create a new List.
val newGuesses = ...
Then pass newGuesses into eliminate.
Lastly, it looks like eliminate modifies guesses. Change this to return a new list. e.g.
def eliminate(list: List[Int]) = {
//Eliminate something from list and return a new `List`
}