Get first-level categories in liferay 7 - categories

I am facing a problem in getting the categories in Liferay 7,
I am using the ADT to get categories of specific vocabulary as following :
<#if entries?has_content>
<#list entries as entry>
<#assign categories = entry.getCategories()>
<#list categories as category>
<a>${category.getName()}</a>
</#list>
</#list>
</#if>
entry type is : AssetVocabulary
my problem is : I am getting the whole categories and sub categories of the vocabulary
for example I have this categories :
fruits ( apples - banana )
vegetables
meat
so in the results I am getting all categories and sub categories as :
fruits
apples
banana
vegetables
meat
but what i need is :
fruits
vegetables
meat
I hope that I can find help here , thank you in advance

Check if the category has any parent.
if doesn't have any then display it otherwise ignore.
#set($assetUtil = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetCategoryLocalService"))
#foreach ($category in $assetUtil.getCategories())
#if($category.parentCategoryId==0)
#set( $temp = $category.getName())
<div class="span10">$temp</div>
#end
#end

Related

ionic 3 input data binding is updating the different arrays of same type

I have a problem in input data-binding for one of our ionic 3 application. Whenever input changes which is changing the array values of different arrays of same type.
Here is my HTML
<ion-list>
<ion-item-sliding *ngFor="let item of storageItem ; index as i">
<div class ="itemList">
<input type="number" [value] ="item.storageOrderQuantity" (blur)="updateInputItemValue(item)"
[(ngModel)]="item.storageOrderQuantity" />
</div>
</ion-item-sliding>
</ion-list>
When ever input value changes, it is updating 'storageItem' array along with other arrays which has the same object(there are some other arrays 'item').
Here is my arrays declaration. Item is a model class.
item: Item[];
storageItem: Item[] = [];
storageItem' is a subset of 'item
Can anybody tell what would be the mistake in data-biding?
You have mentioned that storageItem is a subset of item
You probably already know this, Arrays and Object use concept of assign-by-reference. If you don't know this then read below article on Medium.
https://medium.com/#naveenkarippai/learning-how-references-work-in-javascript-a066a4e15600
So if you have the same object in both the arrays then updating one array will update the another,
const obj = { name : 'value' };
const arr1 = [1,2,3,obj];
const arr2 = [4,5,6,obj];
obj.name = 'yash'; // both arrays will have updated object
Now if you want to avoid this, then you can create a copy of the object before using it in another array.
Take reference from https://flaviocopes.com/how-to-clone-javascript-object/
item: Item[] = [
{ id : 'item1', list : [] },
{ id : 'item2', list : [] },
{ id : 'storageItem', list : [...storageItem] }
];
storageItem: Item[] = [list of storage items];
Now storageItem and item > id='storageItem' point to different arrays.
So your template will only update the storageItem now.

In CrafterCMS, how can I query a model to get an array with all the fields in a repeated group?

In CrafterCMS, I have a component Team with a itemSelector field where I'm assigning some instances of another component TeamMember.
In the template of Team I'm using siteItemService.getSiteItem to get the model information of child components:
<#assign memberModel = siteItemService.getSiteItem(memberItem.storeUrl) />
Between the fields of type TeamMember I have:
Some fields of TeamMember
I'm able to get the value of skillTitle like this:
<#assign skillsTitle = memberModel.queryValue("//skillsTitle")!"" />
But I'm not able to get the value of the values in the repeating group.
I tried with:
<#assign skills = memberModel.queryValues("//skills")![] />
It returns an array of just one element, I think is an empty string
<#assign skills = memberModel.queryValues("//skills/item")![] />
It returns an array with the right number of elements, but I think all of them are empty strings
If I use:
<#assign skills = memberModel.queryValues("//skills/item/skillName")![] />
I get a correct array with all the skill names, but I need iterate over both values (skillName and skillLevel)
How can I query the model in order to get an array which elements have all the values in the repeated group?
Once you get the SiteItem with
<#assign memberModel = siteItemService.getSiteItem(memberItem.storeUrl) />
it works just like any other contentModel variable within a FreeMarker template. So, you can iterate it with
<#list memberModel.skills.item as skill>
${skill.skillName} = ${skill.skillLevel}
</#list>

Laravel Eloquent Relationship 3 tables

I am a newbie to Laravel 5.2 and am working on a legacy system and am a bit confused regarding eloquent and would appreciate someone giving me the code.
There are 3 tables:
cards
categories
cards2cat
cards can be part many categories which are kept in cards2cat table.
The cards2cat table has the following structure
id (primary key)
image (card)
category
What I want to do is to have a method in the Cards model called something like getCardsWithCategores which returned the cards info plus the names of the categories from the category table.
The categories table has a key of id and a field category.
Thanks!
Go to your Card2Cats model and add this:
public function categories()
{
return $this->hasOne('App\Categories','id','category');
}
public function cards()
{
return $this->hasOne('App\Cards','id','image');
}
For the query you do this:
$cards = Card2Cat::with('categories','cards')->get();
foreach ($cards as $key => $value) {
echo $value->id.', Card:'.$value->cards->name.', Category:'.$value->categories->category.'<br>';
//$value->cards gives you all column of cards and you can do
//$value->cards->colName
// same goes for $value->categories
}
Make sure the spelling of your classes and table column names are correct before running the code :D

Sorting a list of items into different categories

Imagine a site that lists different articles with different subjects, and you as a user can mark your favorite articles to read later. Favorited articles get saved and displayed in your personal little space on this site like this:
Movies
Upcoming summer blockbusters
Will there never be a end of Superheroes?
Are romcoms dead?
Science
Bezos or Musk, who will reach Mars first?
Philosophy
How can mirrors be real if our eyes aren't real?
Etc, etc.
I have saved each article in the database with the following fields:
{
articleName: "Upcoming summer blockbusters",
subject: "Movies",
link: //link to the article
}
Then when a user favorites one I simply duplicate it into his own collection in the database.
Then comes the problems...
I could iterate through his articles and print them out on his user page like so:
<ul>
{{#each articles}}
<heading>{{subject}}</heading> //how do I avoid duplicates?
<li>{{articleName}}</li>
{{/each}}
</ul>
This, however, would duplicate the subjects that are shared across multiple articles.
I could iterate through the subjects only (making them into an array that checks for duplicates, for instance) and print them out:
<ul>
{{#each subjects}}
<heading>{{this}}</heading>
<li>{{../articleName}}</li> //how do I print the correct one?
{{/each}}
</ul>
But this way would be completely broken, since the articles wouldn't show up under the correct subject headings...
How should I proceed?
Actually I think you should store favorite articles only, the way you did. It will be way simpler to handle cases like article deletions. Just parse the articles in a helper function, and you should get the same result:
favArticlesBySubjects: function () {
var favArticles = Meteor.user().profile.favArticles;
var articlesBySubject = [];
favArticles.forEach(function (article) {
var index = _.findIndex(articlesBySubject, function (obj) {
return obj.subject === article.subject;
});
if (index < 0)
articlesBySubject.push({subject: article.subject, articles:[article]});
else
articlesBySubject[index].articles.push(article);
};
return articlesBySubject;
}
This way, you can display your favorite articles using :
{{#each favArticlesBySubjects}}
<heading>{{subject}}</heading>
<ul>
{{#each articles}}
<li>{{articleName}}</li>
{{/each}}
</ul>
{{/each}}

How can we get all element values, when more than element hold same class name

How can we get all element values, when more than element hold same class name.
Eg:
Consider I'm having n number of elements that having same class name as follows
<span class="country-name">Country 1</span>
<span class="country-name">Country 2</span>
<span class="country-name">Country 3</span>
<span class="country-name">Country 4</span>
<span class="country-name">Country 5</span>
How can I get all the element values that having the class name as country_name.
Also I have tried as follows:
span(:country, :class => 'country-name')
puts country
When I execute it, It only printing the first value (Country 1) other values are not printed. How can I get all values?
Any suggestions?
You can create a collection accessor that returns all of the related spans (ie those with class "country-name").
In the page object, instead of calling span, call the pluralized version - spans:
class MyPage
include PageObject
spans(:country, :class => 'country-name')
end
This will create a country_elements method that return returns an array of all matching spans. You can iterate over this array to get the text of each country (element):
page = MyPage.new(browser)
page.country_elements.each{ |c| puts c.text }
#=> "Country 1"
#=> "Country 2"
#=> "Country 3"
#=> "Country 4"
#=> "Country 5"