CMS Development

JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi all, I am digging in and testing how to use hub db for the first time. I have this working except I want to be able to list the employees alphanumerically but it doesn't work. Can anyone give me some ideas about what is going on?

 

 

{% if module.choose_employees == "associate" %}
{% set queryparam = "status=associate" %}
{% elif module.choose_employees == "leadership" %}
{% set queryparam = "status=leadership" %}
{% elif module.choose_employees == "leadership & associates" %}
{% set queryparam = "status=leadership&status=associates" %}
{% endif %}
{% if module.order_by %}
{% set queryparam = queryparam + "&orderBy=name:asc" %}
{% endif %}
{% set table = hubdb_table_rows(5748488, queryparam)%}

 

Thanks for any help you can offer.

Jim 

0 Upvotes
16 Replies 16
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Hi @JLangman , You can definatelly use the sort option that hubl provides, this would look something like the following:

 

{% for employee in table|sort(False, False, "name_field") %}
    {{ employee.name }}<br>
{% endfor %}

 


You should replace "name_field" with the actual name of the field you need to sort.

Also you can see more details on this filter here in this link

If you need any further help, don't hesitate to write here.

If my answer was helpful please mark it as a solution.

JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi @miljkovicmisa thanks for setting me in the right direction. I am still a little confused. I think I have this all in place but it doesn't work. I was hoping to be able to sort my cards I have made... I am guessing I am missing a bigger part of the puzzle? 

<div class="db__card__wrapper brxe-div">

{% if module.choose_employees == "associate" %}
{% set queryparam = "status=associate" %}
{% elif module.choose_employees == "leadership" %}
{% set queryparam = "status=leadership" %}
{% elif module.choose_employees == "leadership & associates" %}
{% set queryparam = "status=leadership&status=associates" %}
{% endif %}
  
{% if module.order_by %}
{% for employee in table|sort(False, False, "name") %}
    {{ row.name }}
{% endfor %}
{% endif %}
{% set table = hubdb_table_rows(5748488, queryparam)%}


{% if table == [] %}
    <p class='align-center'>Sorry, no one found for that Search. Try changing your filter and search again.</p>
{% else %}
  {% for row in table %}
  
<div id="brxe-bhegyl" class="brxe-div shared-styles-for-exported-element">
  <div id="brxe-fgjkjz" class="brxe-div db__card__container"> <img src="{{ row.image.url }}" width="300" {% if row.image.width > row.image.height  %}class="landscape brxe-image db__card__img"{% endif %} alt="{{ row.name }} Headshot">
    <div id="brxe-wsfdxb" class="brxe-div db__card__info">
      <div id="brxe-bnzzpq" class="brxe-text-basic db__card__title">{{ row.name }}</div>
      <div id="brxe-tgymzv" class="brxe-text-basic db__card__name">{{ row.role }}</div>
      <div id="brxe-isdtkr" class="brxe-text-basic db__card__bio">{{ row.bio }}</div>

    </div>
  </div>
</div>


{% endfor %}
{% endif %}
  
  </div>
<script>
    window.onload = function() {
    // Get all the div elements using their class name
    var divs = document.getElementsByClassName("brxe-text-basic db__card__bio");

    // Loop through each div
    for (var i = 0; i < divs.length; i++) {
        var div = divs[i];

        // Get the number of words in the div
        var wordCount = div.textContent.split(" ").length;

        // Set the maximum number of words
        var maxWords = 3;

        // Check if the word count is greater than the maximum allowed
        if (wordCount > maxWords) {
            // Get the first 50 words
            var limitedWords = div.textContent.split(" ").slice(0, maxWords).join(" ");

            // Update the div with the limited words
            div.textContent = limitedWords + "...";
        }
    }
}
</script>

 Does this make sense? thanks again for your help. 

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

As far as I can see you have put the loop before defining the table, try to
move the table before outputting the sorted loop.
JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi @miljkovicmisa That is doing something now... However I am clearly missing something. Now if the Boolean is checked, the names written out before the cards- and this makes sense now that I see it, but I was hoping that I could make it so the cards would redraw in order... do I need to assign it to something with the row name? An ID on the card div maybe?

{% if module.choose_employees == "associate" %}
{% set queryparam = "status=associate" %}
{% elif module.choose_employees == "leadership" %}
{% set queryparam = "status=leadership" %}
{% elif module.choose_employees == "leadership & associates" %}
{% set queryparam = "status=leadership&status=associates" %}
{% endif %}
  
{% set table = hubdb_table_rows(5748488, queryparam)%}

{% if module.order_by %}
{% for row in table|sort(False, False, "name") %}
    {{ row.name }}
{% endfor %}
  
{% endif %}
{% if table == [] %}
    <p class='align-center'>Sorry, no one found for that Search. Try changing your filter and search again.</p>
{% else %}
  {% for row in table %}

 

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Hi @JLangman , I'm not sure I follow here, so you are basically checking if the order_by value is true or false and depending on that you either sort the table or not, right?

In this case as I can see in your sample code you should restructure things a bit, it should look like the following (note the change is in the "if's", you need to restructure the if statements like so):

{% if table == [] %}
  <p class='align-center'>Sorry, no one found for that Search. Try changing your filter and search again.</p>
{% else %}
  {% if module.order_by %}
    {% for row in table|sort(False, False, "name") %}
      {{ row.name }}
    {% endfor %}
  {% else %}
    {% for row in table %}
      {{ row.name }}
    {% endfor %}
  {% endif %}
{% endif %}


So what we do here is:

  1. Check if the table is empty
    1. If the table is empty then show the message.
    2. Else if the table is full
      1. Check if the order_by is true
        1. If it is true then render the table with order
        2. If it is not true then render the table in the default order.

Hope this makes some sence and produces the result you need.

If my answer was helpful please mark it as a solution.

JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi @miljkovicmisa  I tried it, I am attaching a screenshot since I think it will describe better what my problem is than me trying to explain it. Hopefully you dont mind taking another look.

 

Thanks,
Jim

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Could you please elaborate a bit more on the end result you want to achieve, I think I'm missing something here in order to help you more.

JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

@miljkovicmisa sorry, so all I want to do is add the ability for the cards to sort by alphanumeric by the persons name, nothing I try to do will affect the cards. The string of text does sort , but thats not what I need, in fact the text string is just by accident and testing and trying to learn how I could do it.

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Ow okay, I see now where the misunderstanding is, so when you apply the filter "|sort(False, False, "name")" it only applies to the current loop, meaning that the table object is not affected globally by this, this is why the string of names is sorted but the cards are not.

You should apply the filter in every loop where you need it, or you could apply it once in the parameters like you already do with the filters in the query, probably the latter is a better solution for your case.

Check this full code example, I have included some comments as well:

<div class="db__card__wrapper brxe-div">

{% if module.choose_employees == "associate" %}
{% set queryparam = "status=associate" %}
{% elif module.choose_employees == "leadership" %}
{% set queryparam = "status=leadership" %}
{% elif module.choose_employees == "leadership & associates" %}
{% set queryparam = "status=leadership&status=associates" %}
{% endif %}

{# below we set a new variable to hold the sorting parameter, it is empty by default #}

{% set sortParam = "" %}
{% if module.order_by %}
{% set sortParam = "&sort=name" %}
{% endif %}

{# now we concatenate all the parameters, filter and sort, and add them to a new variable #}

{% set finalParameters = queryparam~sortParam %}
{% set table = hubdb_table_rows(5748488, finalParameters)%}


{% if table == [] %}
    <p class='align-center'>Sorry, no one found for that Search. Try changing your filter and search again.</p>
{% else %}
  {% for row in table %}
  
<div id="brxe-bhegyl" class="brxe-div shared-styles-for-exported-element">
  <div id="brxe-fgjkjz" class="brxe-div db__card__container"> <img src="{{ row.image.url }}" width="300" {% if row.image.width > row.image.height  %}class="landscape brxe-image db__card__img"{% endif %} alt="{{ row.name }} Headshot">
    <div id="brxe-wsfdxb" class="brxe-div db__card__info">
      <div id="brxe-bnzzpq" class="brxe-text-basic db__card__title">{{ row.name }}</div>
      <div id="brxe-tgymzv" class="brxe-text-basic db__card__name">{{ row.role }}</div>
      <div id="brxe-isdtkr" class="brxe-text-basic db__card__bio">{{ row.bio }}</div>

    </div>
  </div>
</div>


{% endfor %}
{% endif %}
  
  </div>
<script>
    window.onload = function() {
    // Get all the div elements using their class name
    var divs = document.getElementsByClassName("brxe-text-basic db__card__bio");

    // Loop through each div
    for (var i = 0; i < divs.length; i++) {
        var div = divs[i];

        // Get the number of words in the div
        var wordCount = div.textContent.split(" ").length;

        // Set the maximum number of words
        var maxWords = 3;

        // Check if the word count is greater than the maximum allowed
        if (wordCount > maxWords) {
            // Get the first 50 words
            var limitedWords = div.textContent.split(" ").slice(0, maxWords).join(" ");

            // Update the div with the limited words
            div.textContent = limitedWords + "...";
        }
    }
}
</script>

 

Hope this helps you out in your result.

If my answer was helpful please mark it as a solution.

JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

@miljkovicmisa  Sorry for the slow reply, I managed to come down with Covid, its slowing me down but not stopping me.

I tried this and now when I click on the Boolean filter for ASC it reports the  no filtered results message? I thought I might be able to figure it out but Im still lost. All of what you have done is helping me to understand more and more so thank you. I am guessing its something simple at this point?

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Hi @JLangman , I wish you a quick recovery, I delt with covid last summer and it was a real struggle, stay strong 💪!

As for the table now, it seems something went wrong with the parameters, we would need to debug this, can you change the following part like this:

{# below we set a new variable to hold the sorting parameter, it is empty by default #}


{% if module.order_by %}
  {% set sortParam = "&sort=name" %}
{% else %}
  {% set sortParam = "" %}
{% endif %}


Hope this resolves it.

Cheers!

0 Upvotes
JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi and thanks @miljkovicmisa 

 

Crazy, it is still doing the same thing. I even tried another column name to sort by... role to see if it was something with the data, but I had the same problem.

 

do you think it's a problem the hubdb? I wish I knew more about how to troubleshoot these sorts of things. I feel powerless lol. 

I appreciate your help, I also understand if your kindness has reached a limit. I can see where it's hard to keep offering help.

 

Thanks. 


 

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Sorry for the late reply, you can troubleshoot this with the following and check the output:

{# check the output of this var #}
{{sortParam}}

{# and the output of this var #}
{{ finalParameters }}


So basically you need to check if the parameters render correctly.

 

0 Upvotes
JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi @miljkovicmisa Sorry, just getting back up to speed since beating covid... 

 

I think it must be adding something, when outputting this code and selecting the sort by it reads:
&sort=name status=leadership&sort=name

 

So I wonder if the first &sort=name is causing this? 

 

Thanks again.

 

Jim

0 Upvotes
miljkovicmisa
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

how to sort a row of employees by name?

Hello @JLangman , great news about beating covid, hope you are doing well.

Hmm the second part is the final parameters in the query, it seems correct to me, could you replace the variable by hardcoding the parameters in the hubdb query directly?
Like so:
{% set table = hubdb_table_rows(5748488, "status=leadership&sort=name")%}


0 Upvotes
JLangman
Participant | Partner
Participant | Partner

how to sort a row of employees by name?

Hi @miljkovicmisa thanks again. feeling much better! It went through the whole house, the wife and kids caught it but everyone is good to go!

 

I tried that and it did not return any cards... I thought the parameters looked good too, I just cant imagine why this would be the case. I only know enough to be dangerous or enough to make it close but not working. LOL I guess I will have to hope I never need to do this for the client... otherwise I'm cooked. 

0 Upvotes