Shuffle and Place indented list

I have a bunch of ideas and a list of projects. I need to choose one idea and match it with a project. I followed this guide to implement the drag and drop feature, but encountered an issue where every project gets assigned the same idea when dragging and dropping. The problem seems to be related to using the same name fruit for each project. I should assign it dynamically or explore alternative solutions. Any insights would be greatly appreciated. Thank you.

<li *ngFor="let fruit of fruitsInProject; let i = index" class="list-group-item" dnd-sortable [sortableIndex]="i">{{fruit.name}}</li>

This is my code:

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  listProjects: Array<Project> = [
    {
      id: 1, name: 'Project A'
    }, {
      id: 2, name: 'Project B'
    }, {
      id: 3, name: 'Project C'
    }, {
      id: 4, name: 'Project D'
    },];
  listIdeas: Array<Idea> = [
    {
      id: 1, name: 'Creative'
    }, {
      id: 2, name: 'Innovative'
    }, {
      id: 3, name: 'Visionary'
    }, {
      id: 4, name: 'Bold'
    },];
  fruitsInProject: Array<Idea> = [];
  constructor() { }

}

export interface Project {
  id: number;
  name: string;
}
export interface Idea {
  id: number;
  name: string;
}

app.component.html

<div class="row">
  <div class="col-sm-3">
    <div class="panel panel-warning">
      <div class="panel-heading">
        Available Projects
      </div>
      <div class="panel-body">
        <ul class="list-group">
          <div class="panel panel-warning" *ngFor="let project of listProjects; let pindex=index">
            <div class="panel-heading">
              {{project.name}}
            </div>
            <div class="panel-body" dnd-sortable-container [dropZones]="['projects-zone']" [sortableData]="fruitsInProject">
              <ul class="list-group">
                <li *ngFor="let fruit of fruitsInProject; let i = index" class="list-group-item" dnd-sortable [sortableIndex]="i">{{fruit.name}}</li>
              </ul>
            </div>
          </div>
        </ul>
      </div>
    </div>
  </div>
  <div class="col-sm-3">
    <div class="panel panel-success">
      <div class="panel-heading">
        Ideas
      </div>
      <div class="panel-body" dnd-sortable-container [dropZones]="['projects-zone']" [sortableData]="listIdeas">
        <ul class="list-group">
          <li *ngFor="let idea of listIdeas; let idx = index" class="list-group-item" dnd-sortable [sortableIndex]="idx">{{idea.name}}</li>
        </ul>
      </div>
    </div>
  </div>
</div>

Output:

https://i.stack.imgur.com/qqAW0.gif

Answer №1

Your issue lies in this particular line.

// app.component.ts
...
fruitsInBasket: Array<Fruit> = [];

The problem is that you are storing the same data for all the boxes, causing any fruit dropped into one basket to appear in every basket.

To address this, consider updating the type of fruitsInBasket to

// app.component.ts
... 
fruitsInBasket: { [basket: number]: Array<Fruit> };

Ensure you rewrite the component with the understanding that each basket should have its own separate datastore.

Answer №2

The reason for consistently seeing the same fruit in every basket is due to utilizing a single fruitsInBasket Array to store fruits for all baskets.

To resolve this issue effectively, it is recommended to create separate Arrays for each basket and add the dropped fruit on onDropSuccess of the droppable element. Detailed information can be found in the documentation here.

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

Tips for verifying the presence of an active session after closing the browser

Here is my situation: When a user logs into the app and then opens another browser window, they are already authenticated thanks to express-session, bypassing the login page. This pattern continues for subsequent browser windows. The issue I am facing no ...

Different ways to prevent invalid entries in an input field with type datetime-local

<input type="datetime-local" step="1"> Is there a way to prevent invalid date input? For example, entering a date like "11:11:1111" in the format "mm-dd-yyyy". How can this be addressed using Moment.js? ...

Updating a label dynamically in Angular

QUESTION: Is there a way to dynamically change the text of a label based on a certain condition? Specifically, I want the label to be blank when I'm on a specific route in my App. CURRENT APPROACH: <RadSideDrawer allowEdgeSwipe=&quo ...

Creating dynamic captions in an Angular grid is an essential feature for enhancing the

Is there a way in Angular to dynamically update the grid titles based on an array of elements in the model? How can I display them as captions? For instance, imagine we are currently in week 202010. I would like to automatically generate the next five wee ...

Picture fails to load on Ionic app on the device

Currently, I am utilizing Ionic 3 for my project. The location of the images file is \src\assets\img. This pertains to a basic page implementation. export class BasicPage { items = []; constructor(public nav: NavController ,private adm ...

Is there a way to recursively convert property types from one to another in an object that contains optional properties?

The scenario: I am currently working with MongoDB and a REST API that communicates using JSON. MongoDB uses objects instead of identifiers for documents, but when these objects are stringified (such as in a response body), they get converted into strings. ...

"Utilizing aws-sdk in a TSX file within a React project: a step-by

When working on a project using TypeScript (tsx) for React, I encountered an issue with uploading images to the server using aws-sdk to communicate with Amazon S3. To resolve this, I made sure to install aws-sdk via npm and typings. UploadFile.tsx import ...

I'm struggling to find a solution to this pesky TypeScript error that keeps popping up in the button component's styling. How can

An error related to style is appearing: <Button style = No overload matches this call. Overload 1 of 3, '(props: { href : string; } & { children?: React Node; classes?: Partial<Button Classes> | undefined; color?: "primary" | ...

Is Typescript generating error TS2411 within its own Typescript module?

After transitioning to using @types in Typescript 2.0, I executed npm i -S typescript @types/typescript to ensure everything was set up correctly with typescript. However, whenever I run tsc on my project and exclude "node_modules", I encounter the same e ...

What causes the Angular child component (navbar) to no longer refresh the view after a route change?

Hello everyone, I'm excited to ask my first question here. Currently, I am working on developing a social network using the MEAN stack and socket.io. One of the challenges I am facing is displaying the number of unread notifications and messages next ...

Uncertain about where and how to properly register a service in Angular 2

All: Today was my first day diving into Angular2, as I embarked on the official TUTORIAL at part 6: Routing Around the App https://angular.io/docs/ts/latest/tutorial/toh-pt5.html Within the Create AppComponent section, it is mentioned: Add HeroService ...

Is there a way to pass a token variable from the main page to an iframe without relying on a post message?

I am seeking assistance on how to transfer a variable from a parent window to an iframe. My situation is as follows: I am working with 2 Angular5 applications named A and B . Application B is loaded within Application A using an iframe. The aut ...

The absence of essential DOM types in a TypeScript project is causing issues

Recently, I've been working on setting up a web app in TypeScript but I seem to be missing some essential types that are required. Every time I compile using npm run build, it keeps throwing errors like: Error TS2304: Cannot find name 'HTMLEleme ...

The explanation of the Angular tutorial is not displayed correctly

Hi there! I was working on a tutorial in Angular about using geofire location queries with Google Maps. It was quite interesting and I followed all the instructions provided in this video tutorial: . However, when I completed the project and ran it, I ende ...

Title remains consistent | Angular 4

Struggling to change the document title on a specific route. The route is initially set with a default title. { path: 'artikel/:id/:slug', component: ArticleComponent, data: {title: 'Article', routeType: RouteType.ARTICLE, des ...

Creating Excel documents using Angular and XLSX template generator

In my Angular application, I am utilizing the XLSX library to manipulate an Excel file. The file I start with is encoded in base64 format, so my first task is to decode it, make some changes to specific cells, and then save the modified file back in base64 ...

Retrieve the values of private variables within a defined function

While experimenting with typescript, I have encountered an issue that I can't seem to resolve. My project involves using Angular, so I will present my problem within that context. Here is a snippet of my code: class PersonCtrl{ private $scope: I ...

Metronome in TypeScript

I am currently working on developing a metronome using Typescript within the Angular 2 framework. Many thanks to @Nitzan-Tomer for assisting me with the foundational concepts, as discussed in this Stack Overflow post: Typescript Loop with Delay. My curren ...

Use the constant INLINE with npm commands to specify inline behavior

Today I was working on Angular2 using the template provided at . Following the installation guide, I executed the commands as instructed. I have successfully installed Node.js v6.9.1. npm install --Executed without any issues. npm server --Encountered a ...

The compatibility between `webpack-streams` and `@types/webpack`

I encountered an issue with my gulpfile while building it with TypeScript. Everything was working fine until I included @types/webpack-stream. Suddenly, I started getting a series of errors that looked quite ugly: node_modules/@types/webpack-stream/index.d ...