Jekyll display collection by category - jekyll

I'm having trouble figuring out how to sort the content of a collection by category. I've found a very similar question on how to sort posts by category but it doesn't seem to work with collections Jekyll display posts by category.
Let's say I have a collection called 'cars' with two categories: 'old' and 'new'. How can I display only cars with the category 'old'?

You can sort, group or filter collection like any other object like page or posts.
_my_collection/mustang.md
---
category: 'old'
abbrev: tang
---
mustang content
_my_collection/corvette.md
---
category: 'new'
abbrev: vet
---
corvette content
Filtering
{% assign newcars = site.my_collection | where: "category", "new" %}
Sorting
{% assign allcarssorted = site.my_collection | sort: "category" %}
Grouping
{% assign groups = site.my_collection | group_by: "category" | sort: "name" %}
This groups your cars and then sort groups by name that is the category.
{% for group in groups %}
{{ group.name }}
{% for item in group.items %}
{{item.abbrev}}
{%endfor%}
{%endfor%}

Related

How to insert filtered list in table and add additional row of naming for each columns

I have a list of filtered value from SQL :
in views.py
name_list=list(AB.objects.filter(name__in=xyz).values_list('name', 'surname', 'sector','industry', 'country','city'))
in template.html:
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{ name }} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{surname}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{sector}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{industry}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{country}} </li></ul>
<ul><li>{% for name, surname,sector,industry,country,city in name_list %} {{city}} </li></ul>
Output of it is :
Alex Klein Machinery Aerospace USA Kansas
Lia Michelle Healthcare Drugs Ireland Dublin
I want to add names to each columns such as Name, Surname, Sector, Industry, Country, City and insert them in table. I made ul and li as I di not know how to insert them in table
Desired output is table the following way :
Name Surname Sector Industry Country City
Alex Klein Machinery Aerospace USA Kansas
Lia Michelle Healthcare Drugs Ireland Dublin
Would appreciate your help.
Every time you write the word for you are starting a loop. Try this:
{% for name, surname,sector,industry,country,city in name_list %}
<ul><li> {{name}} </li></ul>
<ul><li> {{surname}} </li></ul>
<ul><li> {{sector}} </li></ul>
<ul><li> {{industry}} </li></ul>
<ul><li> {{country}} </li></ul>
<ul><li> {{city}} </li></ul>
{% endfor %}
Update:
To render it as a table as requested in the comments:
<table>
<tbody>
{% for name, surname,sector,industry,country,city in name_list %}
<tr>
<td>{{name}}</td>
<td>{{surname}}</td>
<td>{{sector}}</td>
<td>{{industry}}</td>
<td>{{country}}</td>
<td>{{city}}</td>
</tr>
{% endfor %}
</tbody>
</table>

In the django templating language how to count the number of items of same type

I am trying to build a table that lists items that can be categorized by type. I can list out all the items and their types which are returned from my database in a table with no problems. That is in fact what I would like to do. Since these items can be categorized by type I see no reason to list the same type multiple times. The table is listed in order by category type so all items of the same type are already grouped together. In effect what I have is something along the lines of:
/________________/
/|item A | category A|/
/|item B | category A|/
/|item C | category A|/
/|item D | category B|/
/|item E | category B|/
/|item F | category B|/
/------------------------/
what I actually would like see is
/________________/
/|item A |```````````````|/
/|item B | category A|/
/|item C |_________|/
/|item D |``````````````|/
/|item E | category B|/
/|item F |_________|/
/-------------------------/
This can be done easily enough with a rowspan in the html. My issue the amount of rows to 'rowspan' is not known ahead. What I have in the Django template is:
<table class="table table-sm">
<thead>
<tr>
<td scope="col">Col 1</td>
<td scope="col">Col 2</td>
</tr>
</thead>
<tbody>
{% for option in options %}
<tr>
<td>{{ option.itemNumber }}</td>
{% ifchanged option.itemType %}
<td rowspan="how to get this number">{{ option.itemType }}</td>
{% endifchanged %}
</tr>
{% endfor %}
</tbody>
</table>
The only way I can come up with a way to get the count is to create a second, identical, loop, run over the items, count them, then insert that number in rowspan, then continue on with the original loop. This does not seem to be great solution so I would prefer not to go that route. Any help here is very much appreciated.
I was able to resolve this by creating a custom filter that returns the count of items in a category. In the custom filter I ran a database query filtering the result by the category then running a count.
I ended up with:
<table class="table table-sm">
<thead>
<tr>
<td scope="col">Col 1</td>
<td scope="col">Col 2</td>
</tr>
</thead>
<tbody>
{% for option in options %}
<tr>
<td>{{ option.itemNumber }}</td>
{% ifchanged option.itemType %}
<td rowspan="option.itemType|count_types">{{ option.itemType }}</td>
{% endifchanged %}
</tr>
{% endfor %}
</tbody>
</table>

Get total number of members per branch_id by using Laravel

Members Table: Column: member_id, firstname, lastname, branch_id
Branch Table: Column: branch_id, name etc ```
I want to display total number of members per branch.
Example:
BranchName Number of users
Durban 1000
Cape Town 2000
Mpumalanga 2500
Assuming you have a relation from branch to members you could do the below.
#foreach ($branches as $branch)
<div>Name: {{ $branch->name }} </div>
<div>Members: {{ {{ $branch->members->count() }}</div>
#endforeach

Transforming HTML table populated by database query into matrix-like summary table

I have an HTML table that contains transaction-style information(date, category, and value) for each row. I would like to change this table from being a simple row-by-row table into a matrix-style summary table, taking in a group by query from my database and displaying the group by parameters as the column and row headers.
Query:
SELECT MONTH(date) as month, category, value
FROM transactions
GROUP BY month, category
Current Table (using a query without the group by):
Month || Category || Value
------------------------------
Jan || Produce || 10
Jan || Poultry || 20
Feb || Poultry || 30
Feb || Poultry || 20
Desired Table:
|| Jan || Feb
------------------------------
Produce || 10 || 0
Poultry || 20 || 50
I have no trouble reading the data into my table or displaying it, but I have been struggling to figure out how to format or display a table or some alternative HTML object in the matrix-style I have described.
This is the HTML code to create my current table. I am using Django as my framework (hence the django syntax {{}} to populate my table). This does not seem like a Django problem to me, so I am not including any of that code, but I can if someone feels that's where the solution lies.
<table id = 'transactions'>
<tr>
<th>Month</th>
<th>Category</th>
<th>Value</th>
</tr>
{% for transaction in transactions %}
<tr>
<td>{{ transaction.month }}</td>
<td>{{ transaction.category }}</td>
<td>{{ transaction.value }}</td>
</tr>
{% endfor %}
</table>
Both front-end and back-end ideas or solutions are welcome.
On the SQL backend, use conditional aggregation:
SELECT
category,
MAX(CASE WHEN month = 'Jan' THEN value END) Jan,
MAX(CASE WHEN month = 'Feb' THEN value END) Feb
FROM mytable
GROUP BY category
This should produce the correct row/column structure, and make it easier to display it in your frontend.

How to get the last 3 created objects from the database?

I want to display the last 3 created objects onto my website. I have no clue what to do.
I just need to know how to order the model according to created date. And, access the 1st, 2nd and 3rd item of the ordered set.
I'm pretty sure this doesn't work. This is from views.py
latest = Upcoming_Events.objects.all().order_by('date')[:3]
event1 = latest.get(pk=1)
event2 = latest.get(pk=2)
event3 = latest.get(pk=3)
I keep getting
'Cannot filter a query once a slice has been taken'
You can make ORM like this
latest = Upcoming_Events.objects.all().order_by('-date')[:3]
Then in template
{% for event in latest %}
{{ event.date }}
{% endfor %}
Update:
As per comment try this
event1 = latest[0]
event2 = latest[1]
event3 = latest[2]
make a descending order on pk(id)
latest = Upcoming_Events.objects.all().values().order_by('-id')[:3]
then:
event1 = latest[0]
UPDATE
Notice that you have to use .values().
If you do not use it the SQL Query will not contain LIMIT 3