Where depending on the variable ‘origin’ with values of 1, 2 or 3 I want to put {obj.URL1}, {obj.URL2} or {obj.URL3}.
And second, I don’t know how to insert images into a
depending on the value of some fields. For example, if field ‘tag1’ has a value of 1 or 3 I want to put ‘image1’, and if the value is 2 or 3 then ‘image2’. The JavaScript code is simple, but I cant attach the function into the template.
Can you help me whit this both issues? I’ve read the documentation and found how to create a method like this:
view.define("type",{
template:"#a# - {common.mymethod()}",
mymethod:function(obj){
return obj.b.toUpperCase();
}
})
// a - B
But I can’t understand how to apply this in the code, supposing this is what I must do. I don’t know if have to change words like ‘type’ or ‘common’. Can you help me, please?
Hello again. I’m very sorry, but I’m giving the best of me but I can’t figure out how to relate ‘origin’ variable and JSON structure. I’m not ver familiar with JSON. I guess I must use something like:
I tryed your proposition and always get “undefined”.
But how can I say in the template: “If in variable ‘origin’ has a value of 1, this item uses obj.URL1? And the same with values 2 and 3.” I think I can understand the code of the blue and green aquare GIFs, but it has 4 IDs (1 to 4) and 4 items in the viewer. It seems that relate each ID with each item in the viewer. I need to build only 3 rules in the JSON and use it in all items in the viewer, depending on the value of the variable.
Yes, I can build a demo for you, of course, but I think it’s not necessary. My real problem is I don’t know how to use the parse JSON data. I understand the structure of JSON, but I can’t figure out how to use it to make decisions. I simply need to use one field or another from the ‘obj’ object (who has all the fields inside it) on each item in the viewer depending on a global variable. If in this moment the variable has a ‘1’, the items in the DataView must show ‘obj.description1’. If ‘2’, ‘obj.description2’, and so on.
Hope been more clear this time. English isn’t my first language.
I load the viewer with data from a database with a connector, and that works fine. I have a number of items, not always the same, maybe 200 or 300. In the template I use {obj.field} or #field# to get data and show each item as I want: one picture (in {obj.photo}), two descriptions (in {obj.title} and {obj.date}), and the whole item is a link (in {obj.URL1}).
But, depending on the situation, sometimes I need to use {obj.URL1}, others {obj.URL2} and others {obj.URL3}. The global variable ‘origin’ has the value 1, 2 or 3 that defines the situation.
There are more decisions to take, depending on the values of other fields, but if you teach me to do the URLs example, probably I will resolve by myself.
template for dataview items can be specified by a function, not only a string. This function takes data an item as an argument and should return html string. Template is applied to each item of datavew. Here is an example:
var urls = {1: "url1", 2: "url2", ... };
var titles = {1: "object1", 2: "object2", ... };
dhxView= new dhtmlXDataView({
...
type:{
template:function(data){
return "<a href='"+urls[data.origin]+"'>"+titles[data.origin]+"</a>"
}
}
});
Thank you for the clue. And what can you say about muy second question? I have an html->type template, and need to show several images into each item depending on the tags it has. For example, item 1 has ‘FieldTag1=1’, ‘FieldTag2=0’ and ‘FieldTag3=1’, so I must include in the template and , but not (because has 0 in the tag2). Item 2 has tags ‘0,0,1’ so only Image3 must be shown. And so on with every item and diferent combinations. How can I define this template?
and I can’t be able to adapt it into a var as ‘html’ in your example (I don’t know why, maybe te quotes, maybe the carriage returns…). I think if I could do that it will resolve my problem.