के साथ पेजिनेशन को कैसे संभाला जाए मेरे पास एक div है जो observeableArray
से जुड़ने के लिए सेट है, लेकिन मैं केवल observeableArray
से किसी भी समय 50 से अधिक आइटम दिखाना चाहता हूं। मैं पृष्ठ पर इंडेक्स के साथ पिछले और अगले बटन के साथ पेजिनेशन के साथ इसे संभालना चाहता हूं ताकि उपयोगकर्ता संग्रह से वस्तुओं के पृष्ठों के माध्यम से चले जा सकें।
मुझे पता है कि मैं शायद computedObservable
और कस्टम डेटा बाध्यकारी के साथ ऐसा कर सकता हूं लेकिन मुझे यकीन नहीं है कि यह कैसे करें (मैं अभी भी एक नॉकआउट नियोफेट हूं)।
क्या कोई मुझे सही दिशा में इंगित कर सकता है?नॉकआउट
यहाँ मेरी कोड है (जे एस टाइपप्रति में है):
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<%=
if params[:q]
render 'active_search.html.erb'
else
render 'passive_search.html.erb'
end
%>
<%= form_tag("/search", method: "get", :class => "form-search form-inline") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q, nil, class:"input-medium search-query") %>
<%= submit_tag("Search", :class=>"btn") %>
<% end %>
<div class="media" data-bind="foreach: tweetsArray">
<%= image_tag('twitter-icon.svg', :class=>"tweet_img", :style=>"display:inline;") %>
<div class="media-body" style="display:inline;">
<h4 class="media-heading" data-bind="text: user.screen_name" style="display:inline;"></h4>
<span data-bind="text:text" style="display:inline;"></span> <br />
<span data-bind="text:'Created at '+created_at"></span> <br />
</div>
</div>
<div class="pagination pagination-centered">
<ul>
<li>
<a href="#">Prev</a>
</li>
<li>
<a href="#">1</a>
</li>
<li>
<a href="#">Next</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<script>
var viewModel = new twitterResearch.TweetViewModel();
ko.applyBindings(viewModel);
//TODO: notes to self, use custom binding for pagination along with a computed observable to determine where at in the list you are
//document.onReady callback function
$(function() {
$.getJSON('twitter', {}, function(data) {
viewModel.pushTweet(data);
console.log(data.user);
});
});
</script>
declare var $: any;
declare var ko: any;
module twitterResearch {
class Tweet {
text: string;
created_at: string;
coordinates: string;
user: string;
entities: string;
id: number;
id_str: string;
constructor(_text: string, _created_at: string, _coordinates: any, _user: any,
_entities: any, _id_str: string, _id: number){
this.text = _text;
this.created_at = _created_at;
this.coordinates = _coordinates;
this.user = _user;
this.entities = _entities;
this.id_str = _id_str;
this.id = _id;
}
}
export class TweetViewModel{
tweetsArray: any;
constructor()
{
this.tweetsArray = ko.observableArray([]);
}
//tweet is going to be the JSON tweet we return
//from the server
pushTweet(tweet)
{
var _tweet = new Tweet(tweet.text, tweet.created_at, tweet.coordinates,
tweet.user, tweet.entities, tweet.id_str, tweet.id);
this.tweetsArray.push(_tweet);
this.tweetsArray.valueHasMutated();
}
}
}
यह बहुत अच्छा है .. धन्यवाद! मैंने इसे प्रत्येक पृष्ठ संख्या दिखाने के लिए विस्तारित किया है और उपयोगकर्ता को पृष्ठ 5 से पृष्ठ 5 पर क्लिक करने की अनुमति दी है। Http://jsfiddle.net/LAbCv/31/ –
अंतिम छमाही पृष्ठ को सही तरीके से छोड़ देता है? 'Math.ceil'' Math.floor' की बजाय पृष्ठ गिनती कैल्क में होना चाहिए? संपादित करें: आह आप शेष/मॉड्यूलस को वापस जोड़ते हैं, मैं बस इसे बंद कर दूंगा और किया जाएगा। – MrYellow
इस दृष्टिकोण से सावधान रहें यदि आप किसी ऐसे चीज़ के साथ काम कर रहे हैं जिसके पास एक बड़ा परिणाम सेट हो सकता है। परिणाम क्लाइंट पक्ष की पूरी सूची लोड करने और फिर जावास्क्रिप्ट में केवल पृष्ठ लोड करना अच्छा विचार नहीं है। बड़े परिणाम सेट के लिए, आपको किसी प्रकार का AJAX समाधान चाहिए जो केवल वर्तमान पृष्ठ के लिए आवश्यक डेटा लोड करता है। – Mir