Skip to main content
Nintex Community Menu Bar
Question

how to check if a model has data rows with javascript?

  • July 10, 2024
  • 7 replies
  • 19 views

Forum|alt.badge.img+11

I’m trying to run some if statements in javascript and need to check if a model has data rows. I’m sure there’s some easy way to do this, but couldn’t find it in the api reference or on the community. 

I’m using the getFirstRow action to set a firstRow variable and then checking if firstRow is undefined. It works, but i’m sure there’s a better way. 

var templates = skuid.model.getModel('ConditionTemplates');<br>var templateFirstRow = templates.getFirstRow();<br>if (templateFirstRow !== undefined){ <br>//do stuff <br>}

7 replies

Forum|alt.badge.img+5

Jack! I’m learning more and more as I work with snippets and javascript lately so hopefully I can help. I use getRows() instead of getFirstRow(). Then I set a variable that checks that last variable of the getRows() one for length which gives me the number of rows in the model. Example: var rows = model.getRows(); var rowsLength = rows.length; I think this should help you out hopefully. I hope that helps! Jeff Rutter


Forum|alt.badge.img+5

T08e66ddfe887c49853ee7d6b0c3f8e1bae687bee.png This is what I see in my console…


Forum|alt.badge.img+11

Turns out there’s an even simpler way! I figured out that modelName.data.length counts the records the same way as rows.length

var templates = skuid.model.getModel('ConditionTemplates');<br>if (templates.data.length !== 0) {<br>//do stuff<br>}

Forum|alt.badge.img+5

Awesome…you’re right that is definitely a better option! Glad to help! Regards, Jeff Rutter


Forum|alt.badge.img+18

Gentlemen, just a heads-up that while .data isn’t likely to go away, .getRows() is the documented way to access the model’s data rows. I always recommend using the documented method.

In your example above templates.getRows().length will return the same thing as templates.data.length


Forum|alt.badge.img+11

Awesome! Thanks Matt!


Forum|alt.badge.img+5

Matt, thanks for the reminder!