Note that there are some explanatory texts on larger screens.

plurals
  1. POBind function in jQuery plugin function
    primarykey
    data
    text
    <p>I'm trying to implement an infinite scroll in a div with filtering option, as well, filtering should work when user stops typing in the box.</p> <p>Html:</p> <pre><code> &lt;div class="span2" style="margin-top: 50px;"&gt; &lt;input id="Search" class="input-mysize" /&gt; &lt;div id="listNav" style="height: 370px; border: 1px solid; overflow: auto; margin-right: 20px; width: 90%;"&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>JS in Html:</p> <pre><code> $(function() { function onSuccess(row, container) { container.append('&lt;div style="border:1px solid; cursor: hand; cursor: pointer;" &gt;' + '&lt;table border="0"&gt;' + '&lt;tr&gt;' + '&lt;td id="Location' + row.Id + '"&gt;'+ '&lt;b&gt;Name: &lt;/b&gt;' + row.Name + '&lt;/br &gt;' + '&lt;b&gt;Address: &lt;/b&gt;' + row.Address + '' + '&lt;/td&gt;' + '&lt;td onclick="locationDetails(' + row.Id + ')"&gt; &gt; &lt;/td&gt;' + '&lt;/tr&gt;' + '&lt;/table&gt;' + '&lt;/div&gt;'); var tdId = "Location" + row.Id; var element = $('#' + tdId); $(element).click(function() { google.maps.event.trigger(arrMarkers[row.Id], 'click'); }); }; //... $('#listNav').empty(); $('#listNav').jScroller("/Dashboard/GetClients", { numberOfRowsToRetrieve: 7, onSuccessCallback: onSuccess, onErrorCallback: function () { alert("error"); } }); //... $('#Search').keyup(function(){ clearTimeout(typingTimer); if ($('#myInput').val) { typingTimer = setTimeout(doneTyping, doneTypingInterval); } }); function doneTyping () { startInt = startInt + 5; $('#listNav').empty(); $('#listNav').unbind(); $('#listNav').jScroller("/Dashboard/GetClients", { numberOfRowsToRetrieve: 7, start : startInt, locationFilter: $('#Search').val(), onSuccessCallback: onSuccess, onErrorCallback: function () { alert("error"); } }); }; }); </code></pre> <p>rest of JS (based on jScroller plug in):</p> <pre><code>(function ($) { "use strict"; jQuery.fn.jScroller = function (store, parameters) { var defaults = { numberOfRowsToRetrieve: 10, locationFilter: '', onSuccessCallback: function (row, container) { }, onErrorCallback: function (thrownError) { window.alert('An error occurred while trying to retrive data from store'); }, onTimeOutCallback: function () { }, timeOut: 3 * 1000, autoIncreaseTimeOut: 1000, retryOnTimeOut: false, loadingButtonText: 'Loading...', loadMoreButtonText: 'Load more', fullListText: 'There is no more items to show', extraParams: null }; var options = jQuery.extend(defaults, parameters); var list = jQuery(this), end = false, loadingByScroll = true; var ajaxParameters; if (options.extraParams === null) { ajaxParameters = { start: 0, numberOfRowsToRetrieve: options.numberOfRowsToRetrieve, locationFilter: options.locationFilter }; } else { ajaxParameters = { start: 0, numberOfRowsToRetrieve: options.numberOfRowsToRetrieve, locationFilter: option.locationFilter, extraParams: options.extraParams }; } list.html(''); function loadItems() { preLoad(); jQuery.ajax({ url: store, type: "POST", data: ajaxParameters, timeOut: options.timeOut, success: function (response) { list.find("#jscroll-loading").remove(); if (response.Success === false) { options.onErrorCallback(list, response.Message); return; } for (var i = 0; i &lt; response.data.length; i++) { if (end === false) { options.onSuccessCallback(response.data[i], list); } } if (loadingByScroll === false) { if (end === false) { list.append('&lt;div&gt;&lt;a class="jscroll-loadmore"&gt;' + options.loadMoreButtonText + '&lt;/a&gt;&lt;/div&gt;'); } } ajaxParameters.start = ajaxParameters.start + options.numberOfRowsToRetrieve; if (ajaxParameters.start &gt;= response.total) { end = true; list.find('#jscroll-fulllist').remove(); list.find(".jscroll-loadmore").parent("div").remove(); } }, error: function (xhr, ajaxOptions, thrownError) { if (thrownError === 'timeout') { options.onTimeOutCallback(); if (options.retryOnTimeOut) { options.timeOut = options.timeOut + (1 * options.autoIncreaseTimeOut); loadItems(); } } else { options.onErrorCallback(thrownError); } } }); } function preLoad() { if (list.find("#jscroll-loading").length === 0) { list.find(".jscroll-loadmore").parent("div").remove(); list.append('&lt;a id="jscroll-loading"&gt;' + options.loadingButtonText + '&lt;/a&gt;'); } } //called when doneTyping is called and first time page loaded jQuery(document).ready(function () { loadItems(); }); //called when user scrolls down in a div $('#listNav').bind('scroll', function () { if ($(this).scrollTop() + $(this).innerHeight() &gt;= $(this)[0].scrollHeight) { loadItems(); } }); }; })(jQuery); </code></pre> <p>It's mostly working, <strong>but at some cases when user stops typing</strong>, the </p> <pre><code>jQuery(document).ready(function () { loadItems(); }); </code></pre> <p>and</p> <pre><code>$('#listNav').bind('scroll', function () { if ($(this).scrollTop() + $(this).innerHeight() &gt;= $(this)[0].scrollHeight) { loadItems(); } }); </code></pre> <p>are both called <strong>instead of just first one</strong>, adding bad html to the div. Most cases only jQuery(document).ready is called, <strong>which is what i need</strong>.</p> <p>As well why is jQuery(document).ready() is called every time the doneTyping() is called ? I though it should be called only the first time the page is loaded after DOM is ready.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload