Basically, I just want to know how to check if a sortable list is empty or not. I have an ajax call after a list is updated, but if there are no items I want to bypass the call because it will be unnecessary.
What would be the method to achieve this? Is there a simple method?
Using jQuery you can attempt to select the sortables and test how many elements are returned:
if ($('.sortable-class').length > 0) {/*something with the class `sortable-class` exists*/}
The above code assumes that each of the elements in your sortable have the class sortable-class
. All you really need is a unique selector that will only find your sortables. For instance if your sortables are all children of an element then your selector could look like this:
HTML
<ul id="sortable_parent">
<li>this is sortable</li>
<li>this is also sortable</li>
</ul>
JS
if ($('#sortable_parent').children('li').length > 0) {/*atleast one li exists*/}
Here's some documentation if you're into that kind of thing: http://api.jquery.com/length
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With