Disable a button depending on array return value

I have an array which returns;

1. training: [{dateCompleted: "2021-03-31", trainer: null, modID: 1},…]
1. 0: {dateCompleted: "2021-03-31", trainer: null, modID: 1}
2. 1: {dateCompleted: "2021-03-31", trainer: null, modID: 2}

now I would like to disable the button if the value is returned.
I have used:

 <button id="btn11" class="btn btn-block text-left" data-toggle="collapse" data-target="#c_week3" dmx-bind:disabled="view_details.data.training.where(`modID`, 3, '==')">Week 3 - {{view_details.data.dates[0].trainingStart.addWeeks(2).formatDate('dd/MM/yy')}} - Holster & Basic Movement</button>

However this is disabling the button when it should not be.
Does any one have any ideas?

Hello, which value do you want to check? There are several records from what i see?
Is the button in a repeat?

No the button is not in a repeat at there are different text I need in the button
The value is modID

I’ll message over a link of the page to you if that’s any help, just hit a road block on this

But which record's value do you want to check? I see two records there. Do you want to check the first one or the second one?

I need it to check all of them which is why I have put the ‘view_details.data.training.where(modID, 3, ‘==’)’ in the button.

If there is a better way please let me know, but I need the buttons to have different text which does not come from a database. This is why I did not put the repeat region in there

Ok i checked the link your provided, your approach for this is a bit weird, but if you want to keep your page like that then you need to check if the where formatter actiually returns a value or not, so it should be:

for the first button:

dmx-bind:disabled="view_details.data.training.where(`modID`, 1, '==').length != 0"

same for the others - just replace 1 with 2, 3, 4…

1 Like

You Sir are a legend.....
Could you please explain the logic behind the .lenght !=0 for me so and why this has made it work. So I understand for the future

Depending if the item exists in the array or not the expression

view_details.data.training.where(`modID`, 3, '==') 

returns (when the item exists):

[
    {
        "dateCompleted": "2021-03-31",
        "trainer": null,
        "modID": 3
    }
]

or if the item does not exist:

[]

so for the disabled attributes this is always a value. You just need to check if the lenght is 0, which means the returned value is [] and if it’s 0 don’t disable the button.

1 Like