Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 select boxes in my app and they are populated from db, when I choose a value from first select box, second select box is populate via ajax. this scenarion works great. but as the values in select boxes are static I didn't want populate them from db, I think use for this a file is more usefull and good for app performance. so how I can populate them from file (json, xml, etc.)? first select box to the second is one-to-many
This sounds like a lot of work since your going to have to populate the text files from db. Infact its end result will be in effect no different to a caching solution.http://mrdustmite.blogspot.co.uk/2010/09/simple-ehcache-in-grails.html?m=1
The mentioned plugin above generates primary/secondary values from as JSON values..
https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/services/ajaxdependancyselection/AutoCompleteService.groovy
def selectSecondary(params) {
if (params.domain2) {
def domainClass = grailsApplication?.getDomainClass(params?.domain2)?.clazz
def query = {
eq params.bindid, params.id.toLong()
projections {
property(params.collectField)
property(params.searchField)
}
order(params.searchField)
}
def results =domainClass.createCriteria().list(query)
def primarySelectList = []
results.each {
def primaryMap = [:]
primaryMap.put('id', it[0])
primaryMap.put('name', it[1])
primarySelectList.add(primaryMap)
}
return primarySelectList as JSON
}
}
https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/taglib/ajaxdependancyselection/AutoCompleteTagLib.groovy
def gsattrs=['optionKey' : "${attrs.collectField}" , 'optionValue': "${attrs.searchField}", 'id': "${attrs.id}", 'value': "${attrs.value}", 'name': "${name}"]
gsattrs['from'] = primarylist
gsattrs['noSelection'] =attrs.noSelection
gsattrs['onchange'] = "${remoteFunction(controller:''+attrs.controller+'', action:''+attrs.action+'', params:'\'id=\' + escape(this.value) +\'&setId='+attrs.setId+'&bindid='+ attrs.bindid+'&collectField='+attrs.collectField2+'&searchField='+attrs.searchField2+'&domain2='+attrs.domain2+'&controller='+attrs.controller+'\'',onSuccess:''+attrs.id+'Update(data)')}"
def link = ['action': attrs.action , 'controller': attrs.controller ]
out<< g.select(gsattrs)
You should be able to use the functionality to reproduce the same effect from a text file if you so wish to go down this route...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I wanted to know how to add a field in a certain user uid
So I'd like to add another field like Address for the currentUser "Rowan Whitethorn". How can I do this?
Currently, i could add another field but it creates another documents.
const userRef = firestore.collection('users').doc(currentUser.uid);
async function addAddress() {
const res = await userRef.set({
'Address' : 'Ayala'
}, { merge : true});
}
Firestore documentation describes a feature "merge", where you would typically create a new doc with using ".set", but then pass a parameter "merge" which tells firebase not to overwrite your doc data, in your case it would be something like:
const userRef = db.collection('users').doc(doc_id);
const res = await userRef.set({
'some_new_field': 'value'
}, { merge: true });
Here's the link for their documentation, you will find a similar example under "merge": https://firebase.google.com/docs/firestore/manage-data/add-data
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my Flutter app, I want sorted data to depend upon the last date which is present in medicine list for a particular user, so how can I write my Firebase query?
I'm using Firebase Firestore.
Try something like this (code has not yet been tested):
List<dynamic> getMedicinesSorted() async {
final snapshot = await FirebaseFirestore.instance
.collection('users/tnzdrlpLD1hfJJ6o8iMV/MedicinesList')
.get();
final sorted = snapshot.docs.sort((docA, docB) =>
new DateTime.fromMicrosecondsSinceEpoch(docA.data()['Last_Date']).compareTo(
new DateTime.fromMicrosecondsSinceEpoch(docB.data()['Last_Date'])
)
);
return sorted;
}
[enter image description here][1]
image
[1]: https://i.stack.imgur.com/C6qy8.jpg
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have json format data that I am retrieving from my ORDS server and it looks like this:
{"items":[{"cust_code":"C00013","cust_name":"Holmes","cust_city":"London
","working_area":"London","cust_country":"UK","grade":2,"opening_amt":6000,"receive_amt":5000,"payment_amt":7000,"outstanding_amt":4000,"phone_no":"BBBBBBB","agent_code":"A003
"},{"cust_code":"C00001","cust_name":"Micheal","cust_city":"New York
","working_area":"New
York","cust_country":"USA","grade":2,"opening_amt":3000,"receive_amt":5000,"payment_amt":2000,"outstanding_amt":6000,"phone_no":"CCCCCCC","agent_code":"A008
"},{"cust_code":"C00020","cust_name":"Albert","cust_city":"New York
","working_area":"New
York","cust_country":"USA","grade":3,"opening_amt":5000,"receive_amt":7000,"payment_amt":6000,"outstanding_amt":6000,"phone_no":"BBBBSBB","agent_code":"A008
"},{"cust_code":"C00025","cust_name":"Ravindran","cust_city":"Bangalore
","working_area":"Bangalore","cust_country":"India","grade":2,"opening_amt":5000,"receive_amt":7000,"payment_amt":4000,"outstanding_amt":8000,"phone_no":"AVAVAVA","agent_code":"A011
"},{"cust_code":"C00015","cust_name":"Stuart","cust_city":"London
","working_area":"London","cust_country":"UK","grade":1,"opening_amt":6000,"receive_amt":8000,"payment_amt":3000,"outstanding_amt":11000,"phone_no":"GFSGERS","agent_code":"A003
"}],"hasMore":false,"limit":25,"offset":0,"count":5,"links":[{"rel":"self","href":"http://localhost:9000/ords/demo/customers/getAll/"},{"rel":"describedby","href":"http://localhost:9000/ords/demo/metadata-catalog/customers/getAll/"},{"rel":"first","href":"http://localhost:9000/ords/demo/customers/getAll/"}]}
so I was wondering if there is a way to to break down the items part and to put it in a tables using anguler.
I tried to use this way in my ts file:
http.get('http://localhost:9000/ords/demo/customers/getAll/')
.subscribe(data => this.items = data);
But I keep getting this error:
Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed
The array you want is in the items property so you have to do
http.get('http://localhost:9000/ords/demo/customers/getAll/')
.subscribe(data => this.items = data.items);
I'm gonna go straight forward for this. This question is connected with my passed question, I will offer you the bounty within 3 days from now if you solved this problem.
What I want
After the user answered a quiz, the user can save it without submitting, so they can edit/continue later. Now, after they saved, how can I loop through the user's answer in the current loop of questions in a quiz? So that the user can edit or continue the quiz.
What I have
database:
quiz_result_of_user has 9 fields, ['id', 'quiz_id', 'user_id', 'question_id', 'answer', 'question_type', 'attempt_number', 'marks', 'grades']
This table saves all the user's answer so this serves as the history table of the user's quiz.
quizattempt_user has 3 fields, ['id', 'quiz_id', 'user_id']
This table saves all the user's attempt, so I can reference all the answer of the user where the id = attempt_number in the quiz_result_of_user table.
Controller - Update
$quiz = Quiz::with('multiple_choices.answers', 'true_false', 'short_answer', 'descriptions')->findOrFail($id);
$questions = collect($quiz->multiple_choices);
$questions = $questions->merge(collect($quiz->true_false));
$questions = $questions->merge(collect($quiz->short_answer));
$questions = $questions->merge(collect($quiz->descriptions));
$questions = $questions->sortBy('question_number');
Problem
I can now loop through the questions, and the answers of the user, but I can't figure out how can I put the user's answer because it is also a collection of data. Note: every quiz can have different types of question, multiple choice, true or false, and short answer/fill in the blank.
Based on what I understand of the table structure, we can create a dictionary of the user's answers keyed by the the question IDs, so we can easily look up the answer for each question. Here's a minimal implementation that we can expand on:
$questions = ... // see question description
$answers = QuizResults::where('quiz_id', $quiz->id)
->where('user_id', $userId)
->where('attempt_number', $attemptNumber)
->get()
->keyBy('question_id');
foreach ($questions as $question)
{
echo $question->question_number + ". " + $question->description;
if ($question is MultipleChoice) {
foreach ($question->answers as $choice) { echo $choice; ... }
}
echo 'Your answer: ' + $answers->get($question->id)->first()->answer;
}
I'm not sure if the page uses Blade to generate the results. For clarity, this is written in plain PHP, but it should be easy to rewrite this for a Blade template.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is my current object, Home refers to my import
import { Home } from '../home/home';
arr = [
{ name: "Home", root: "Home", icon: "calc" }
];
This is what i want to achieve:
import { Home } from '../home/home';
arr = [
{ name: "Home", root: Home, icon: "calc" }
];
Im getting a error because "Home" is a string. eval() works but i'm looking for a alternative
Working code:
for (var i = 0; i < arr.length; i++) {
arr[i].root = eval(arr[i].root);
}
It seems that this is your situation:
You are receiving a json from a server which has in certain fields names of classes that you need to use in runtime.
The names are strings and you need to get the classes they refer to.
You can indeed use eval as you used, but take into account that it is recommended to avoid using eval, especially when using it on data received from remote, for example from MDN:
eval() is a dangerous function, which executes the code it's passed
with the privileges of the caller. If you run eval() with a string
that could be affected by a malicious party, you may end up running
malicious code on the user's machine with the permissions of your
webpage / extension. More importantly, third party code can see the
scope in which eval() was invoked, which can lead to possible attacks
in ways to which the similar Function is not susceptible.
If you still want to use that, then go ahead, but an alternative is to have a registry of those classes:
const REGISTRY = {} as { [name: string]: { new(): any };
...
class Home { ... }
REGISTRY["Home"] = Home;
And then:
for (var i = 0; i < arr.length; i++) {
arr[i].root = REGISTRY[arr[i].root];
}
Another thing is that this approach creates coupling between the data which is returned from the server and the client side.
You always need to make sure that the class names you return in your json have a corresponding implementation on the client side.
If you rename the class in the client side you need to remember to change it on the server as well.
It's better to only return data and then have the client figure out by itself how to act and find the needed classes.