javascript - Select2 "Infinite Scroll with Remote Data" - Callback not firing -
following example on select2 project page, attempting pull more records when user scrolls bottom of result set.
<script> $(document).ready(function() { $('#style_full_name').select2({ placeholder: "please type in make , model", minimuminputlength: 3, ajax: { url: "/list_styles", datatype: 'json', quietmillis: 100, data: function (term, page) { // page one-based page number tracked select2 return { q: term, //search term page_limit: 10, // page size page: page, // page number }; }, results: function (data, page) { var more = (page * 10) < data.total; // whether or not there more results available // notice return value of more select2 knows if more results can loaded return {results: data.styles, more: more}; } }, formatresult: styleformatresult, // omitted brevity, see source of page dropdowncssclass: "bigdrop", // apply css makes dropdown taller escapemarkup: function (m) { return m; } // not want escape markup since displaying html in results }); function styleformatresult(style) { var markup = "<table class='style-result'><tr>"; if (style.dimage_url !== undefined) { markup += "<td class='style-image'><img src='" + style.dimage_url + "'/></td>"; } markup += "<td class='style-info'><div class='style-title'>" + style.full_name + "</div>"; //if (movie.critics_consensus !== undefined) { // markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>"; //} //else if (movie.synopsis !== undefined) { // markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>"; //} markup += "</td></tr></table>" return markup; } }); </script> while testing rotten tomatoes api example on select2's page , tracking network panel in chrome console can see callback fired when reaching bottom of scroll list. not happening when scroll bottom of scroll list in own use case above.
after digging around bit realized problem "total" not part of json response var more = (page * 10) < data.total evaluating false. here code styles controller (ror) required make infinite scroll work:
def list_styles #sunspot solr query @styles = style.search { fulltext params[:q]; paginate :page => params[:page], :per_page => params[:page_limit] } #build json string @styles = "{\"total\":#{@styles.total}, \"styles\":#{@styles.results.to_json(:only=>[:id, :full_name], :methods => [:dimage_url, :dimage_url_medium])}}" #render json response respond_to |format| format.json {render :json => @styles } end end
Comments
Post a Comment