I'd like to display just two names from a list at a time and have the option to click on a button to reveal the next two names, unfortunately, there

I am trying to modify my code so that it only displays the first 2 items in an array initially, and then on clicking a button, the next 2 names are displayed until the end. However, the code I have written seems to have a bug. Can you help me correct this Knockout.js code?

Here is the JavaScript code:

function viewModel() {
    this.displayCount = ko.observable(2);
    this.readMore = function () {
        this.displayCount(this.displayCount() + 2);
    };

    this.WhoElseAttends = ko.observableArray([]);
    for (var i = 0; i < WhoElseAttends().length; i++) {
        this.WhoElseAttends.push({ data-bind="text:FirstName", data-bind="text:LastName", data-bind="text:Company" });
    }   
}
ko.applyBindings(new viewModel());

In HTML:

<div class="slide">
    <button type="button" data-bind="click: readMore, visible: displayCount() < WhoElseAttends().length">Read More</button>
        <ul data-bind="foreach: WhoElseAttends.slice(0, displayCount())">
            <li>
                <span data-bind="text:FirstName"></span>
                <span data-bind="text:LastName"></span>,
                <span data-bind="text:Company"></span>
          </li>
      </ul>                  
   <span data-bind="if: WhoElseAttends.length <0">No Attendees</span>                    
</div>

Answer №1

There seems to be a few issues in the functionality of your javascript code. In order to add new items to an observableArray, it must be a valid Javascript object.

this.WhoElseAttends.push({ data-bind="text:FirstName", data-bind="text:LastName", data-bind="text:Company" });

This syntax is not correct. It should be changed to something like:

this.WhoElseAttends.push({
            FirstName: 'First Name',
            LastName: 'Last Name' ,
            Company: 'My Company'
        });

Also, it's important to be cautious when using this as it can vary depending on the context. A common practice in javascript is to immediately assign this to a variable, for example:

var ViewModel = function(){
    var self = this;
    self.someProperty = ko.observable();
};

I believe this is what you are essentially looking for.

http://jsfiddle.net/M3dgk/2/

Paging: http://jsfiddle.net/M3dgk/3/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Endowed with improper dimensions, the BootStrap collapse feature

Yesterday, I posted about an issue with BootStrap and panel collapsables causing graph sizes to become distorted. The post was locked because there was no accompanying code. I have now created a code snippet for you all to see the exact problem I am facing ...

The grid is intended to be positioned horizontally, but is currently being shown vertically

I need help fixing the layout of my items. They are currently displaying vertically instead of in a horizontal row. Any suggestions on how to correct this? https://i.stack.imgur.com/CQ4yG.jpg <div class="col-lg-12 col-sm-12 portfolio-item"> <d ...

Change the parent's color when the child is hovered over

head has no background and the color is black. On mouseover, its color changes to white with a black background, so far so good but When I hover over content, I want to change the color of head. How can I achieve this? Is it possible using only CSS? < ...

Use a text link to upload a file instead of using an input field

While I've seen some discussions on this topic already, the conflicting answers have left me uncertain. Is there a foolproof method for triggering file upload without using an input field? Essentially, I'd like to create a div with an icon and te ...

Tips for adding animation to the div instead of the content

I have implemented a hover animation to animate the div, but unfortunately, when I added the animation to #i :hover {}, it ended up animating the words only. Moreover, the cursor transforms into a pointer solely when hovering over the words instead of the ...

In HTML, specify that the height of both the `html` and

I am facing an issue with the container-about div. I have set its height to 100% so that the div occupies the entire width and height after the header div. The problem is that currently, I cannot scroll to view the full text. It would be great if there wa ...

The pop-up dialog on Facebook is limited to the width of the like button, cutting off any excess information

Feel free to check out my blog here. When attempting to click the "LIKE" button, the pop-up appears at the correct height, but the width is limited to the size of the button. Below is the CSS code I am using: .blog_social_media { float: right; b ...

Calculating the Sum of Values in PHP using an HTML Form and Jquery

Looking to expand my knowledge of jQuery, I am attempting to create a straightforward page for practice. The goal is to develop a form that will send its values to PHP to be summed and return the results. These results will then be displayed dynamically on ...

How can I retrieve the span html element without using a class with ajax?

Trying to work with Ajax syntax for the first time, I have this HTML code. <div class="select2-container select2" id="s2id_ServiceID-2-1" style="width: 100%;"> <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabind ...

Adjust the element based on hover over the pseudo element

Looking to modify an element based on the hover state of a pseudo-element. Here's what I've tried so far, but it isn't working as expected: nav .logo { display: none; } nav:before{} nav:hover:before .logo { display: block; } I want th ...

Locked content and inactive link in the browser toolbar

I've encountered a challenge with my page where the content is not scrollable beyond the bar. Additionally, I'm looking to adjust the size of the image to fit the page perfectly without any gaps on the sides, and ensure that the text following th ...

Merge HTML documents with this powerful JavaScript library

Our team has developed an email generator that creates HTML emails based on specific parameters. Users can simply select or deselect options and the email will be regenerated accordingly. Occasionally, users may need to make minor manual changes to the co ...

The search results on my website are completely unaffected by jQuery

One website I have allows me to search within the database, focusing on boats at the moment. I have set up this form: <form method="post" action=""> <input type="text" class="form-control" id="searchField" name="searchField" autocomplete="of ...

Looking to reduce the size of a logo image within a container as you scroll down a webpage?

I've been working on creating a logo section for my website, but I'm struggling to make it shrink as users scroll down and expand back to its original size when they scroll up. I've tried various JavaScript and jQuery functions without succe ...

Leverage htaccess functionality to modify website configurations without altering the URL

I currently have the following htaccess configuration: <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ dl.php?id=/$1 [QSA,L] </IfModule> It works perfectly fine. However, I am facing a problem with my site URLs and images ...

I've exhausted all my knowledge but still unable to get Tailwind to work

I've been troubleshooting Tailwind for the past 6 hours. Managed to set it up in a simpler Nextjs/React/Typescript project, but struggling to get it working in this larger codebase. I'm sure I'm missing something obvious, but I'm at a ...

Content within a div that is floated left

The scenario at hand is: HTML Structure: <div id="main"> <div id="a"></div> <div id="b">Some Text</div> </div> CSS Styles: #a{ float:left; width:800px; height:150px; background-color:#CCC; } ...

"Autocomplete disable" feature malfunctioning on the final input field of all web pages

I'm dealing with a web application that has a login page. The username and password fields have the autocomplete attribute set to "off," as well as other variations like nope, new-password etc. However, it's still not working in Chrome version 62 ...

Attempting to eliminate the mouseleave event by utilizing the jQuery off function

I have a set of buttons in an array and I want to remove the event listeners for each button once it has been clicked. Here is my current approach: JavaScript: var currentButton; var selectedButtons = $("#moto-menu a"); function onEnter(){ TweenMax. ...

Learn a valuable trick to activate CSS animation using a button - simply click on the button and watch the animation start over each time

I already know how to do this once. However, I would like the animation to restart or begin again when the user clicks on it a second time. Here is what I have: function animation() { document.getElementById('ExampleButton').className = &apo ...