Skip to main content

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>}

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


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


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>}

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


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


Awesome! Thanks Matt!


Matt, thanks for the reminder!


Reply