Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8440051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:10:45+00:00 2026-06-10T08:10:45+00:00

I want to show for example 10 items from my observableArray and hide rest

  • 0

I want to show for example 10 items from my observableArray and hide rest of them, and when I add a new element to beginning of array hide the 10Th element, and user to be able to click show more items.

Here are my current markups

Javascript:

var itemViewModel = {
    item: {},
    isLoaded: ko.observable(false),
    comments: ko.observableArray([]),
    loadcontent: function (getID) {
        $.ajax({
            url: '/api/item/details/' + getID,
            dataType: 'json',
            success: function (data) {
                itemViewModel.item = data;
                itemViewModel.comments([]);
                $.each(data.Comments, function (index, thisComment) {
                    itemViewModel.comments.push(thisComment);
                });
                itemViewModel.comments.sort(function (a, b) {
                    return new Date(a.DateTime) == new Date(b.DateTime)
                        ? 0 : (new Date(a.DateTime) < new Date(b.DateTime) ? 
                        1 : -1)
                });
                itemViewModel.isLoaded(true);
                itemDetailBindings();
            }
        });
    },
    showComment: function (ele) {
        if (ele.nodeType === 1) $(ele).hide().fadeIn()
    }
};
//Item detail element bindings
var itemDetailBindings = function () {
    // Add auto expand to textarea
    $('#this-text-is-comment').TextAreaExpander(50, 200);
    //Add comment
    $('#this-text-is-comment').bind('keypress', function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            if ($(this).val() != "") {
                addComment($("#this-text-is-comment").val(), $("#hidden-item-id").val());
                $(this).val('');
            };
        }
    });
};
var addComment = function (cText, getID) {
    $.ajax({
        url: '/api/comment/create',
        type: 'POST',
        dataType: 'json',
        data: { comment1: cText, itemid: getID },
        success: function (data) {
            itemViewModel.comments.splice(0, 0, data);
        }
        /*error: function (xhr, status) {
        switch (status) {
        case 404:
        alert('File not found');
        break;
        case 500:
        alert('Server error');
        break;
        case 0:
        alert('Request aborted');
        break;
        default:
        alert('Unknown error ' + status);
        }
        }*/
    });
};

HTML:

<div class="comment-list clearfix" data-bind="template: {foreach: comments, afterAdd: showComment }">
    <div class="comment-container clearfix">
        <div class="left-side">
            <img src="../../content/u/2012.08.17.634808075593134766.jpg" />
        </div>
        <div class="right-side clearfix">
            <div class="top">
                <span class="user-name" data-bind="text: User.FullName"></span><span class="time-posted"
                    data-bind="text: $.datepicker.formatDate('yy-mm-dd', new Date(DateTime))"></span>
            </div>
            <div class="middle clearfix">
                <div class="body">
                    <p data-bind="text: Comment1">
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

For staters: I have no clue how can I do that in ko.js

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:10:46+00:00Added an answer on June 10, 2026 at 8:10 am

    I happened upon computed filtered arrays (see tip2) but after thinking this through you can do this simply by combining Computed Observables and Javascript slice() method.

    In this following example you would bind to the filteredComments. self.comments() is the main list of comments, the array is sorted and then sliced to show the first # number of comments (self.commentsShown()).

    self.filteredComments = ko.computed(function() {
        return self.comments()
            .sort(compareComments) // important to sort before slicing
            .slice(0, self.commentsShown());
    });
    

    See fiddle for a full working example. It allows for sorted/filtered list of comments, add to to start of comments array (not filtered array) and show more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to show the items queried from database in the listview with SimpleCursorAdapter.
I want to show some letters in Bibliography as capital. For example: @misc{libsvm, abstract
I want to show array objects at the same time in different 10 labels.
I want to show a border type look for an element but without using
I'm trying to make a mini draggable element that will show items in a
I want to dynamically add items to a class, but someone may have to
I'm quite new to parallel programming and threads. I want to calculate and add
HI @ll! I want to add and remove some items to my datasource for
I want to show most popular items of store on website Table 1 is
I want show an information page after success db opartions. And after the info

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.