Enhance table functionality with AngularJs by implementing expandable middle rows

I am developing a payment platform where users can set up a paymentPlan for a specific paymentMethod and choose the number of numberOfIntallments. The details of the paymentPlan are displayed in a table using ng-repeat. Users can select up to 36 numberOfIntallments, but I only want to show specific details about the installments.

My Objective:

If the numberOfIntallments is less than 7, then display all installments.
If the numberOfIntallments is 7 or more, display the first 2 and last 2 installments and include a button in the middle of the table to toggle and show all installments.

How I Plan to Address This Challenge:

Current Code Snippet:

<tr ng-repeat="model in paymentPlan.installments">
    <td>{{model.dueDate | date: 'shortDate'}}</td>
    <td>{{model.principal | currency:undefined:0}}</td>
    <td ng-show="numberOfInstallments > 1">{{model.contractInterest | currency:undefined:0}}</td>
    <td ng-show="numberOfInstallments > 1">{{model.lendingFee | currency:undefined:0}}</td>
    <td ng-show="paymentMethod == 0">{{model.noticeAndPaymentFee | currency:undefined:0}}</td>
    <td>{{model.installmentFee | currency:undefined:0}}</td>
    <td>{{model.total | currency:undefined:0}}</td>
</tr>

Answer №1

To achieve the desired outcome, it is important to perform some calculations in your controller and store the result in the scope. After that, you can make necessary adjustments to your ng-repeat to display the desired result.

CONTROLLER:

$scope.ShowMiddleRows = false;

if ($scope.numberOfInstallments >= 7) {
    // Incorporate the first two installments into PrefixRows[] array
    for (i=0; i < 2; i++) {
        $scope.PrefixRows.push($scope.paymentPlan.installments[i]);
    }
    // Include the last two installments into SuffixRows[] array
    for (i=$scope.paymentPlan.installments-2; i < scope.paymentPlan.installments; i++) {
        $scope.SuffixRows.push($scope.paymentPlan.installments[i]);
    }
    // Store the remaining middle rows in MiddleRows[] array
    for (i=2; i < $scope.paymentPlan.installments-2; i++) {
        $scope.MiddleRows.push($scope.paymentPlan.installments[i]);
    }
}

HTML:

<!-- this <tbody> will only be displayed if numberOfInstallments >= 7 -->
<tbody ng-if="numberOfInstallments >= 7">

    <!-- 2 PREFIX ROWS -->
    <tr ng-repeat="model in PrefixRows">
        <td> ... </td>
    </tr>

    <!-- MIDDLE ROWS or a button -->
    <!-- ShowMiddleRows is a scope variable that toggles between true and false based on button click. If set to true, it displays the middle rows -->
    <tr ng-show="!ShowMiddleRows">
        <td colspan="10"><button ng-click="ShowMiddleRows = !ShowMiddleRows">Show All Installments</button></td>
    </tr>
    <tr ng-repeat="model in MiddleRows" ng-show="ShowMiddleRows">
        <td> ... </td>
    </tr>
    <!-- 2 SUFFIX ROWS -->
    <tr ng-repeat="model in SuffixRows">
        <td> ... </td>
    </tr>
</tbody>

<!-- this <tbody> will only be displayed if numberOfInstallments < 7 -->
<tbody ng-if="numberOfInstallments < 7">
    <tr ng-repeat="model in paymentPlan.installments">
        <td> ... </td>
    </tr>
</tbody>

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

What is the best way to iterate over an array of objects?

I have an Array of Objects that I need to use in order to create an HTML Table: Array(5) 0: Object id: 4 name: Sand Jane address: Green Sand Street ... ... ... 1: Object 2: Object ... ... ... Currently, I am able to perform a search wit ...

What is the best way to make a selected link stand out with a highlight?

I'm having an issue with the code below that is supposed to keep the selected link highlighted, but it only flashes the green color on click. Can someone help me figure out what's going wrong here? #sidebarContent a:active{ background-colo ...

Switching up the Label Colors in Chart.JS

It's been a while since my last question, so please bear with me if I'm not following the rules. I've attached my current code and a reference image of the chart. I am completely new to working with ChartJS. The situation is a bit unique: t ...

"Embracing the power of Spring Boot with AngularJS

I'm attempting to create a Spring Boot application. I have already set up the project, but I'm encountering an issue with my controller code: @RestController public class IndexController { @RequestMapping(value="/home",method = RequestMetho ...

Execute a Node.JS query using an HTML select dropdown

My goal is to customize queries to a mySQL database based on the user's selection from select options on my website. Here is the HTML code: <select id = "year"> <option value = "yr" selected>Choose a Year</option> <option id = " ...

Retrieve a variety of items and pass them to a view using the mssql module in Node

I'm facing an issue while trying to retrieve data from multiple tables and pass them to my view. Below is the snippet of code I've written that's causing the error. router.get('/', function(req, res, next) { sql.connect(config ...

JavaScript Grouping Arrays

I am working with an array and need to filter it by both Country and Service. So far, I have successfully filtered the array by Country. Now, I want to achieve the same filtering process based on the Service as well. Here is a snippet of the array: [ ...

The static files for the icon CSS (404 Error) from Flaticon and Font-mfizz are failing to load properly

When I was designing a website, I needed an icon of Python, so I turned to Flaticon and found what I was looking for. This snippet shows the HTML code I used: {% load static %} <!DOCTYPE html> <html lang="en"> <head> <li ...

Is it possible to initially design a login page using HTML/CSS and JavaScript, and then integrate VUE into it?

As part of a school project, I am tasked with developing a web-based application for a library system. The goal is to create a platform where librarians can login and manage books by adding, removing, or editing them. My responsibility lies in handling the ...

The Web Browser is organizing CSS elements in an alphabetized sequence

map.css({ 'zoom': zoom, 'left': map.width()/(2*zoom) - (point[0]/100)*map.width(), 'top': map.height()/(2*zoom) - (point[1]/100)*map.height() Upon observation, it appears that Chrome adjusts the map zoom first be ...

How to print a Base64 encoded file with the Print.js library

I am facing an issue with printing a Base64 file. Despite my efforts, the file does not print as expected. function convertToBase64() { var selectedFile = document.getElementById("inputFile").files; if (selectedFile.length > 0) { var fi ...

Issue: "Access-Control-Allow-Origin does not allow origin null" error message is returned when attempting to access a PHP file using jQuery's ajax method with dataType text/html selected

Why am I encountering this issue in Chrome when using dataType text and HTML? It seems to work fine if I output JavaScript and set the dataType to script. How can I return non-JavaScript data from a PHP file? Client-side code: $.ajax({ url: ...

Is it possible to implement a single OrbitControls with two cameras in ThreeJS?

Is there a way to link the OrbitControls of two canvases on the same page? For example, if the user zooms in on one canvas, I want the other canvas to also zoom in simultaneously. How could I achieve this synchronization between multiple canvases? ...

What is the most effective way to integrate webkitSpeechRecognition into a Vue.js project?

I found a demo at this link, but I'm having trouble understanding it. Can you provide a simple implementation? <div id="app"> <v-app id="inspire"> <v-container fluid> <v-layout row wrap justify-cen ...

Alternative methods for submitting form data without dependency on ngClick and ngSubmit

Is it possible to send data in AngularJS without using ngClick or ngSubmit, as these methods require explicitly mentioning the function name which may pose security concerns? This is the code snippet I am currently using: <form name="userForm" noval ...

Unexpected behavior observed with Async/Await

I am currently learning how to use Async/Await, which is supposed to wait until the Await function finishes executing before moving on with the code. However, I have encountered an issue where my code stops completely after using Await. Here is the method ...

Arrange the keys of a map in ascending order, prioritizing special characters and their precedence

JavaScript ES6 Map Example: const map = new Map(); map.set('first', ['1', '2']); map.set('second', ['abc', 'def']); map.set('_third', []); map.set(')(*', []); map.set('he ...

Implement pagination for API calls within a React Component

I am trying to implement pagination for the results of an API call. The following code snippet shows how I am making the API call using Axios: apiCall() { const API = `http://www.omdbapi.com/`; axios.get(API, { params: { apikey: proces ...

Tips for obtaining the retrieved URL from an ajax call

How can I extract only the returned URL from an ajax request? I have tried implementing it like this: $.ajax({ type: "GET", dataType : "jsonp", async: false, url: $('#F ...

Trigger a Vue method using jQuery when a click event occurs

I'm attempting to attach a click event to an existing DOM element. <div class="logMe" data-log-id="{{ data.log }}"></div> ... <div id="events"></div> Struggling to access external Vue methods within my jQuery click handler. A ...