Assigning and Comparing a Variable

This is probably something small I am missing. I have a list and each item has a category. The goal for the code is to just output a row with the category information after it changes but I just can’t seem to get it to work. The if statement is always true even if the values are identical. I just want that row to print when the category name changes. What am I missing?

        {{"cat no exist" |> to=>cat}}
        <table>
            {{#each model.orderItems where !isLinked orderby orderItemCategory}}

            {{#if it.orderItemCategoryString != cat}}
            <tr>
                <td>-{{it.orderItemCategoryString}}-{{cat}}- {{ it.orderItemCategoryString.length }} {{ cat.length }}</td>
                {{it.orderItemCategoryString |> to=>cat }}
                <td>Name - {{cat}}</td>
                <td>Amount</td>

                <td>Sub Total</td>
            </tr>
            {{/if}}
{{/each}}

Output would be (the code above is the Category row header):

Category A
– Item
– Item
– Item
Category B
– Item
– Item
– Item

What is isLinked did you mean it.isLinked?

I’ve tried to create a quick repro but this is working as expected:

{{ var orderItems= [{orderItemCategoryString:"A"},{orderItemCategoryString:"cat no exist"}] }}
{{"cat no exist" |> to=>cat}}

{{#each orderItems}}
  {{#if it.orderItemCategoryString != cat}}
     {{it.orderItemCategoryString}} != {{cat}}
  {{else}}
     {{it.orderItemCategoryString}} == {{cat}}
  {{/if}
{{/each}}

Which I’ve run by just pasting it in any #Script text box, e.g https://sharpscript.net/#learn-script

Which outputs the expected:

A != cat no exist
cat no exist == cat no exist

I’ll need a repro to investigate further.

Thanks but in your example you aren’t reassigning the catorderItemCategoryString to cat when it is found so that the header only displays when the category changes. Just a simple thing regarding logic that I can’t seem to get to work here. Here is some code that reproduces it:

{{ var orderItems= [{orderItemCategoryString:“A”},{orderItemCategoryString:“A”},{orderItemCategoryString:“B”},{orderItemCategoryString:“B”},{orderItemCategoryString:“C”}] }}
{{“cat no exist” |> to=>cat}}

{{#each orderItems}}
{{#if it.orderItemCategoryString != cat}}
Table Header for my example should only be one per category - {{cat}}
{{ it.orderItemCategoryString |> to=> cat}}

{{/if}
{{/each}}

The output should be:

Table Header for my example should only be one per category - A
Table Header for my example should only be one per category - B
Table Header for my example should only be one per category - C

It’s because the changes are only visible to the local scope, you can use toGlobal to assign variables that are global to the page:

{{ it.orderItemCategoryString |> toGlobal => cat}}
1 Like

Thank you, that’s what I missed. I read the docs a while ago and just didn’t recall that.