Is there a formal way to bind multiple values to a template? Through trial and error, I came up with this:
<template bind="{{ {param1: 'value1', param2: 'value2'} as params }}" ref="myTemplate">
But this feels clunky. Is there a better way?
Related
In our Angular project, we use interpolation as shown below, but we also need to use this interpolated value in the [state] property. But we haev not managed so far. Any idea?
If we set id values as shown below, there is no problem.
<a routerLink="/ticket/details/" [state]="{ id: '5' }" >{{row.TicketId}}</a>
But we cannot get dynamically by obtaining row.TicketId (it is obtained as label in {{row.TicketId}}) but cannot concatenate with id parameter.
<a routerLink="/ticket/details/" [state]="{ id: {{row.TicketId}} }" >{{row.TicketId}}</a>
The brackets in [state]="..." tell Angular to evaluate the template expression, so you cant use interpolation there. So, as I said in comment it should be:
[state]="{ id: row.TicketId }"
Try [state]="{ id: row.TicketId }"
Php:
$json_string = "{
"26":{"blabla":123,"group_id":1,"from":"27.08.2018","to":"02.09.2018"},
"25":{"blabla":124,"group_id":1,"from":"20.08.2018","to":"26.08.2018"},
"24":{"blabla":125,"group_id":1,"from":"20.08.2018","to":"26.08.2018"}
}"
my.blade.php template:
<my-component :records={{ $json_string }}></my-component>
MyComponent.vue:
export default {
props: ['records'],
...
mounted: function () {
console.log(this.records);
}
Output is:
{__ob__: Observer}
24:(...)
25:(...)
26:(...)
And when I use v-for, records in my table in wrong order (like in console.log output).
What I am doing wrong?
EDIT:
I figured out 1 thing:
When I do json_encode on collection where indexes are from 0 till x, than json string is: [{some data}, {some data}]
But if I do ->get()->keyBy('id') (laravel) and than json_encode, json string is:
{ "26":{some data}, "25":{some data}, "24":{some data} }
Then how I understood, issue is in different outer brackets.
In Javascript keys of objects have no order. If you need a specific order then use arrays.
Here is documentation for keyBy Laravel method: https://laravel.com/docs/5.6/collections#method-keyby
I wanted to have ids for rows data to fast and easy access without iterating over all rows and check if there is somewhere key Id which is equals with my particular Id.
Solution: not to use keyBy method in Laravel and pass json string to Vue component like following [{some data}, {some data}] (as I described in my Question Edit section) - this will remain array order as it used to be.
I found this short and elegant way how to do this, instead of writing js function by myself:
Its find() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Example:
let row = records.find( record => record.id === idToFind );
//Please excuse my poor English.
Hello everyone, I am doing a project which is about a facebook comment spider.
then I find the Facebook Graph GUI. It will return a json file that's so complicated for me.
The json file is include so many parts
then I use json.loads to get all the json code
finally it return a dict for me.
and i dont know how to access the Value
for example i want get all the id or comment.
but i can only get the 2 key of dict "data" and "pading"
so, how can i get the next key? like "id" or "comment"
and how to process this complicated data.
code
Thank you very much.
Two ways I can think of, either you know what you're looking for and access it directly or you loop over the keys, look at the value of the keys and nest another loop until you reach the end of the tree.
You can do this using a self-calling function and with the appropriate usage of jQuery.
Here is an example:
function get_the_stuff(url)
{
$.getJSON(url, function ( data ) {
my_parser(data) });
}
function my_parser(node)
{
$.each(node, function(key, val) {
if ( val && typeof val == "object" ) { my_parser(val); }
else { console.log("key="+key+", val="+val); }
});
}
I omitted all the error checking. Also make sure the typeof check is appropriate. You might need some other elseif's to maybe treat numbers, strings, null or booleans in different ways. This is just an example.
EDIT: I might have slightly missed that the topic said "Python"... sorry. Feel free to translate to python, same principles apply.
EDIT2: Now lets' try in Python. I'm assuming your JSON is already imported into a variable.
def my_parser(node, depth=0):
if type(node) == "list":
for val in node:
my_parser(val,depth+1)
elif type(node) == "dict":
for key in node:
printf("level=%i key=%s" % ( depth, key ))
my_parser(node[key], depth+1)
elif type(node) == "str":
pritnf("level=%i value=%s" % ( depth, node ))
elsif type(node) == "int":
printf("level=%i value=%i % ( depth, node ))
else:
printf("level=%i value_unknown_type" % ( depth ))
I've got this this json data that I want to look up a certain value from with a query:
http://pastebin.com/Vf59Cf9Q
I want to find description == "chassis" in this path:
$.entries..nestedStats.entries..nestedStats.entries.type
However, I just can't get it to work.
Tried these two alternatives, but no luck:
$.entries..nestedStats.entries..nestedStats.entries[?(#.type.description == 'chassis')]
$.entries..nestedStats.entries..nestedStats.entries.type[?(#.description == 'chassis')]
Have tried this on these sites:
http://jsonpath.com/
https://jsonpath.curiousconcept.com/
Any help would be appreciated!
/Patrik
This did the trick:
$.entries.*.nestedStats.entries.*.nestedStats.entries.type[?(# == "chassis")
Suppose I have created a custom element as follow:
<chat-container messages="{{ messages }}"></chat-container>
messages is an array containing chat messages related to any particular thread or contact.
Now, in my app.js, I have this kind of message array which contains all messages by thread identifiers. How can I pass the messages related to any particular thread identifier(i.e name='Rajat') to <chat-container> element? I have tried nearly all combinations and still nothing is making sense.
app.messages = [
{name: 'Rajat', messages: [{id: 1,message: 'Hello'},{id: 3,message: 'Hello 2'}]},
{name: 'Himesh', messages: [{id: 2,message: 'Hello'}]},
{name: 'David', messages: [{id: 4,message: 'Hello'}]}
]
I tried to do:
<chat-container messages="{{ app.messages.filter(function(e) { return e.name === 'Rajat'; })[0].messages }}"></chat-container>
and it did not work.
Polymer doesn't support such complex binding expression. Move the filter code to a function and call that function from the binding expression. Binding expressions only support .``!, ()
<chat-container messages="{{filter('name', 'Rajat')}}"></chat-container>
where the parent element has a function named filter()