What steps are involved in removing data from a subarray?

There are a couple of issues within the table that I'm working on!

First:

Adding and deleting trips is functioning well. However, I am facing a challenge in deleting entries from the subarray - DAYS within a trip. Any tips or hints on how to accomplish this would be greatly appreciated!

Second:

When creating a new trip, I encounter difficulties in adding new DAYS within it. This is possibly due to the absence of an array called DAYS which is not created when adding a new trip. Despite hours of searching, I have been unable to find a solution that works!

Apologies for my lack of expertise...

Below you'll find my code and Plunker URL

http://plnkr.co/edit/MRAhLk7NxZHx4mTLaYxt?p=preview

index.html

<body ng-controller="TripController">
  <div class="row" style="margin-top: 50px;">
    <div class="col-md-10 col-md-offset-1">
      <form name="addTrip" ng-submit="addTrip()">
        <input ng-model="newtrip" type="text" name="newtrip" placeholder="YYYY-MM-DD" required />
        <button ng-disabled="addTrip.$invalid">Add Trip</button>
      </form>

      <div class="alert alert-info" role="alert" ng-repeat="trip in trips">
        <h5>Startdatum: {{trip.Startdate | date:'dd.MM.yyyy'}} <a class="pull-right" ng-click="deleteTrip($index)">x</a></h5>
        <table class="table">
          <tr>
            <th><a href="" ng-click="predicate = 'DATE'; reverse=reverse">DATE</a>
            </th>
            <th></th>
            <th></th>
          </tr>
          <tr ng-repeat="day in trip.DAYS | orderBy:predicate:!reverse" style="background-color: #CCC;">
            <td width="25%;">{{day.DATE | date:'dd.MM.yyyy'}}</td>
            <td width="25%;">
              <input ng-model="day.IATA" />
            </td>
            <td width="25%;">
              <input ng-model="day.DUTY" />
            </td>
            <td width="25%;">
              <a ng-click="deleteDay()"></a>
            </td>
          </tr>
        </table>
        <form name="dayForm" ng-controller="DayController as dayCtrl" ng-submit="dayCtrl.addDay(trip)">
          <div class="row">
            <div class="col-xs-3 col-md-3">{{dayCtrl.day.DATE | date:'dd.MM.yyyy'}}</div>
            <div class="col-xs-3 col-md-3">{{dayCtrl.day.IATA}}</div>
            <div class="col-xs-3 col-md-3">{{dayCtrl.day.DUTY}}</div>
          </div>
          <div class="row">
            <div class="col-xs-3 col-md-3">
              <input ng-model="dayCtrl.day.DATE" type="text" class="form-control" placeholder="DATE (YYYY-MM-DD)" title="DATE" required />
            </div>
            <div class="col-xs-3 col-md-3">
              <input ng-model="dayCtrl.day.IATA" type="text" class="form-control" placeholder="IATA" title="IATA" />
            </div>
            <div class="col-xs-3 col-md-3">
              <input ng-model="dayCtrl.day.DUTY" type="text" class="form-control" placeholder="DUTY" title="DUTY" />
            </div>
            <div class="col-xs-3 col-md-3">
              <button type="submit" ng-disabled="addDay.$invalid" class="btn btn-primary">Add</button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
  {{trips}}
</body>

script.js

(function() {
  var app = angular.module('showTrips', []);


app.controller('TripController', ['$scope', '$http', function ($scope, $http){
        $http.get('trips.json').success(function(data) {

        $scope.trips = data;

        $scope.addTrip = function(){
          $scope.trips.push({'Startdate':$scope.newtrip})
          $scope.newtrip = ''
        }

        $scope.deleteTrip = function(index){
          $scope.trips.splice(index, 1);
        }


      });


}]);


app.controller("DayController", function(){

  this.day = {};
  this.addDay = function(trip)
  {
  trip.DAYS.push(this.day);
  this.day = {};
  }

});

})();

Answer №1

Here is a suggestion:

1)

$scope.removeTripDay = function(trip, index) {
    trip.DAYS.splice(index, 1);
}

<a ng-click="removeTripDay(trip, $index)">REMOVE</a>

2)

$scope.addNewTrip = function() {
    $scope.trips.push({'Startdate':$scope.newtrip, DAYS: []})
}

View Plunkr Demo

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

Divide a Multidimensional Array into Separate Arrays

I am working with an array that contains information about users. Each user has attributes like their ID, name, and number of records for today and in total. Array ( [0] => Array ( [u_id] => 2 [u_name] => Test ...

Ionic's statement is clear: using multiple abstract views is not an effective approach

I have been working on a project using Ionic where I have nested multiple abstract views. However, I am running into issues as it doesn't seem to be working properly, even though I've followed the instructions in this post angular_app = angular. ...

Implementing line breaks in JSON responses in an MVC application

I am looking to show a JavaScript alert with line breaks using the message returned from a controller action that returns JSON result. I have included "\n" in the message for line breaks. Below is the code snippet from my controller: [HttpPost] publ ...

having trouble customizing react-select to fit bootstrap sm styling

I am currently utilizing react-select multi-select feature from the npm package at this link. My project is styled using bootstrap 4, and I am aiming to have the react-select component match the size of the sm class in Bootstrap. To achieve this, I attempt ...

PHP array_key_exists - flexible on certain data types, stringent on others

Despite my decent knowledge of PHP quirks, I found myself puzzled by the unexpected behavior demonstrated in the code below today: // loose $a = array(true => 'foo'); var_dump(array_key_exists(1, $a)); // strict $a = array('7.1' =& ...

Error occurs with iron-form when the submit button is within a div

I'm encountering an issue when trying to submit an iron-form. The error message I receive is: VM7938:3 Uncaught TypeError: Polymer.dom(...).localTarget.parentElement.submit is not a function This problem only occurs when the submit button is place ...

Tips for identifying, initiating, and linking to the mobile version of Metamask through a button click on a web application, similar to the process on OpenSea

I recently integrated a shortcode into my project that leverages the Metamask browser extension to display the user's account using a combination of web3 and reactjs. The code functions flawlessly on desktop browsers, but encounters an issue when atte ...

Ways to delete an item from the environment

I need to change the position of some cylinders in the scene by removing them and adding new ones. Here is a snippet of code to show how I currently place the cylinders: for (i = 0; i < cylinderCount; i++) { var geometry = new THREE.CylinderGeometry ...

Triggering a click event within a JavaScript OnClick function leads to a malfunction

Working on creating a context menu with various options using <li> tags as outlined below. The goal is to trigger a click event on another ID (image) when selecting and clicking on an option from the context menu. However, I am facing an issue where ...

Tips for triggering the .click() function upon returning to index.html after visiting a different page

I'm in the process of creating a portfolio website where the index.html page features a sliding navigation bar that displays a collection of projects. Each project is linked to a separate work.html page. I would like the sliding nav to automatically o ...

What is the AngularJS equivalent of prevAll() and nextAll() functions in jQuery?

Currently, I am working on a project that involves AngularJS and I'm having trouble finding an example that fits my needs... With AngularJS, I know how to apply a class when an element is clicked, but how can I add a class to all previous items and r ...

Merge two arrays based on date and sort them using Angular.js/JavaScript

I am facing a challenge where I have two JSON arrays, each containing a field named date. My goal is to compare the two arrays and merge them into a single array. Check out the code snippet below: var firstArr=[{'name':'Ram','date ...

What is the method for including a placeholder with sequential numbering?

When I click on the "Add String" button, it clones the first table row with an input in the table and adds it to the table. I also need to add a +1 number in the placeholder of the copied element. How can I determine the last placeholder before copying and ...

Transform an array list of strings into an array list of integers

I am working with a Model class called ModelWeeklyGuarantee and I need to convert a string ArrayList to an integer ArrayList For example, converting [5,7,9] from a string ArrayList to [5,7,8] in an IntegerArrayList at index 0. public static ArrayList< ...

Exploring the process of comparing dates within the Fullcalendar plugin when utilizing an array of dates

Looking to compare a specific set of days in an array with all the dates on the calendar. Here is the code that I've written so far. How should I go about passing the array to the dayrender function? Function for retrieving the dates function GetFoo ...

Retrieve data from MongoDB using the $and query operator only when the condition of $and is satisfied within the same index of a document, particularly in the case of a doubly

1. Locating a Match: Strict Criteria within Array Elements Consider the following scenario with two documents stored in a mongoDB: {_id: 1, people: [{height: 10, age: 10}, {height: 5, age: 5}]} {_id: 2, people: [{height: 10, age: 5}, {height: 5, age: 10 ...

KineticJs layer malfunctioning after stage reconstruction

My canvas is created using KineticJs Stage, with three layers - a background that is always visible and two overlays toggled by a checkbox. Whenever the parent div of this stage is resized, I redraw the entire stage to maintain the layout. The toggling wor ...

The callback function in Angular JS does not seem to be functioning correctly with the $interval

As a newcomer to angularjs, please excuse me if my question seems trivial. function getCams(callback){ var media_list = []; MediaStreamTrack.getSources(function(sourceInfos){ var i=0; while(i!=sourceInfos.length){ if (sourceInfos[i].kind ...

jQuery's Hide() and Show() methods are sluggish and inefficient

I am looking to trigger an ajax call when the submit button is clicked. During this process, I want to hide the submit button and display a loading gif image until the ajax operation is completed. However, I have noticed that there is a delay in hiding the ...

Leveraging dynamic keys with v-for in Vue.js

In my Vuejs project, I am currently developing a reusable table component that looks like this: Table Component Structure: <template> <div> <table class="table"> <thead> <tr> ...