Connecting two fields with a line on click in AngularJS

In the top section, there are 10 fields and in the bottom section, there are another 10 fields. I want to be able to connect two fields with a line when they are clicked (one from the top section and one from the bottom section). This connection should apply to all pairs of fields using AngularJS.

I would like all top fields to be connected to their corresponding bottom fields by a connecting line. By clicking on a field in the top section and its counterpart in the bottom section, a line will be drawn between them. This functionality should be present for all fields and triggered by a click event in AngularJS.

Answer №1

var myApp = angular.module("plunk", []);

myApp.controller("MainController", function ($scope) {
  $scope.top_section = [
    {
      name: "apple",
    },
    {
      name: "banana",
    },
    {
      name: "orange",
    },
    {
      name: "grapes",
    },
    {
      name: "melon",
    },
  ];

  $scope.bottom_section = [
    {
      name: "kiwi",
    },
    {
      name: "mango",
    },
    {
      name: "pear",
    },
    {
      name: "peach",
    },
    {
      name: "strawberry",
    },
  ];

  $scope.lines = [];
  $scope.unlink = {};
  
  $scope.getTopPosition = function ($index) {
    let elem = document.getElementById(`1_${$index}`);
    elem.style.transform = "scale(0.8)";
    let x = elem.offsetLeft;
    let y = elem.offsetTop;
    
    if ($scope.unlink["from"]) {
      return (
        ($scope.unlink["to"] = { x: x + 15, y: y + 30 }), $scope.clearLine()
      );
    } else {
      $scope.unlink["from"] = { x: x + 15, y: y + 30 };
    }
  };

  $scope.getBottomPosition = function ($index) {
    let elem = document.getElementById(`2_${$index}`);
    elem.style.transform = "scale(0.8)";
    let x = elem.offsetLeft;
    let y = elem.offsetTop;

    if ($scope.unlink["from"]) {
    	$scope.unlink["to"] = { x: x + 15, y: y };
    } else {
     return ($scope.unlink["from"] = { x: x + 15, y: y });
    }
    
    $scope.clearLine();
  };

  $scope.clearLine = function () {
    $scope.lines.push($scope.unlink);
    $scope.unlink = {};
  };
});
.container section {
  display: flex;
  margin-bottom: 100px;
  justify-content: space-between;
}
.container section.bottom {
  display: flex;
  justify-content: space-between;
}
.top-section {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.bottom-section {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.line {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
}
.line svg {
  width: 100%;
  height: 100%;
}
<!DOCTYPE html>
<html ng-app="plunker">
  <head>
    <meta charset="utf-8" />
    <title>AngularJs Demo</title>
    <link rel="stylesheet" href="style.css" />
    <script
      data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3f3039332a37346521283b7c27123135">[email protected]</a>"
      src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"
    ></script>
    <script src="app.js"></script>
  </head>
  <body ng-controller="MainCtrl">
   <div class="container section">
      <div class="top-section" ng-repeat="t in top_section">
        <span id="1_{{t.name}}" ng-click="getTopPosition(t.name)"
          ><svg fill="#000000" height="30px" width="30px" viewBox="0 0 490 490">
            <path
              d="M0,0v490h490V0H0z M430.1,332.9h-87.5v50.9h-33.1v50.9H180.4v-50.6h-33.1v-51.3H59.9v-278h46.7v66.5h38.5V54.8h40.8v66.5
         h38.5V54.8h40.8v66.5h38.5V54.8h40.8v66.5H383V54.8h46.7v278.1L430.1,332.9L430.1,332.9z"
            /></svg
        ></span>
        {{t.name}}
      </div>
    </div>

    <div class="container section bottom">
      <div class="bottom-section" ng-repeat="b in bottom_section">
        <span id="2_{{b.name}}" ng-click="getBottomPosition(b.name)"
          ><svg fill="#000000" height="30px" width="30px" viewBox="0 0 490 490">
            <path
              d="M0,0v490h490V0H0z M430.1,332.9h-87.5v50.9h-33.1v50.9H180.4v-50.6h-33.1v-51.3H59.9v-278h46.7v66.5h38.5V54.8h40.8v66.5
       h38.5V54.8h40.8v66.5h38.5V54.8h40.8v66.5H383V54.8h46.7v278.1L430.1,332.9L430.1,332.9z"
            /></svg
        ></span>
        {{b.name}}
      </div>
    </div>
    <span class="line">
      <svg>
        <line
          ng-repeat="line in lines"
          x1="{{line.from.x}}"
          y1="{{line.from.y}}"
          x2="{{line.to.x}}"
          y2="{{line.to.y}}"
          style="stroke: rgb(255, 0, 0); stroke-width: 2"
        />
      </svg>
    </span>
  </body>
</html>

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

The functionality of the onclick button input is not functioning properly in the Google

Here is some JavaScript code: function doClick() { alert("clicked"); } Now, take a look at this HTML code: <div > <table border="2" cellspacing="0" cellpadding="0" class="TextFG" style="font-family:Arial; font-size:10pt;"> <tr> <t ...

Can you explain the process of gathering texts based on CSS selector?

I wanted to place texts next to the div-class as shown in the following code snippets. <div class="review-contents__text">소재가 좀 저렴해 보이지만 그래도 입으면 휠씬 나아보여요</div> Initially, I wrote a code ...

Experiencing difficulties connecting JavaScript with HTML

I'm encountering difficulty with implementing my JavaScript on my website. While it functions properly in this JS Fiddle, I am experiencing issues when trying to use it on my actual site. Below is how I have linked the JS on my site: <head> < ...

Maximizing PUT Methods in HTTP RESTful Services

I've been playing around with my routes file and I'm looking to switch up the method being called (delete instead of update). Code Snippets: # User management API GET /users @controllers.Users.findUsers POST /user ...

Is it possible to create a grid by arranging each statement with a specific format?

After fetching and parsing some data, I have utilized the 'each' function to display it: $.ajax({ url : 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent('htt ...

NodeJS Streams: Delay in data transfer with Readable.pipe()

My understanding was that Stream.Readable.pipe() would immediately pipe data as it receives it, but I'm facing unexpected results while trying to create my own streams. const { Writable, Readable } = require("stream"); const writable = new ...

The FAB button animation is causing delays in the transition process and is not functioning as originally anticipated

I am facing an issue with the FAB button and 3 Icons. The functionality is working fine on click for both show and hide actions, but the transition is too delayed. I want the icons to appear step by step, even though I have adjusted the transition delay se ...

Embracing async-await while awaiting events in node.js

I am attempting to incorporate async await into a project that is event-driven, but encountering an error. The specific error message I am receiving is: tmpFile = await readFileAsync('tmp.png'); ^^^^^^^^^^^^^ SyntaxError: Unexpec ...

Is there a javascript function that performs the reverse action of indexof()?

Is there a JavaScript function that is the opposite of indexOf()? In my code, when you press the button it will display 3 [from indexOf(".")]. However, I am looking for a function that is the opposite of indexOf(), which would show me 2 [decimal]. http: ...

Using a MySQL statement within a conditional statement

Looking to modify this code to display different information based on the values retrieved, here is the current code: $sql_doublecheck = mysql_query("SELECT * FROM adminpage WHERE setting='main' AND close_site='1'"); $doublecheck = mys ...

Why does it seem like Typescript Promise with JQuery is executing twice?

Figuring out Promises in Typescript/JS seemed to be going well for me, but I've hit a roadblock. I've set up Promises to wait for two JQuery getJSON requests to finish. In my browser, when connecting to a local server, everything functions as ex ...

Substituting a JavaScript function with a UserStyle solution

I am currently working on customizing a UserStyle for Instapaper. Since the original UserStyle was created, Instapaper has implemented several JavaScript functions in their header that control page width and font styles. Below are the functions: ...

The promise catch method does not handle JSON parsing correctly

Utilizing Angular's Http to interact with my API has been successful for handling responses with a status of 200. The data is parsed correctly and outputted as expected within the first .then() block. However, when encountering an error with a status ...

Placing two images next to each other with accompanying captions

Currently working on developing a webpage, I am aiming to have two images in each row with captions positioned beneath them. The images are already configured as follows, I just require the captions: <div class = "container"> <img class = "pi ...

The date and time displayed in the Kendo grid are incorrectly shown as strange values

I have a datetime column formatted like this: { field: "ModifiedDate", dataType: "System.Date", title: accountCodeResourceProvider.ModifiedDate, format: "{0:G}", width: 150, hidden: true }]; However, when an empty value cell appea ...

Is there a more efficient method for invoking `emit` in Vue's Composition API from an external file?

Is there a more efficient way to access the emit function in a separate logic file? This is my current approach that is functioning well: foo.js export default (emit) => { const foo = () => { emit('bar') }; return { foo }; } When ...

Show Pop in relation to modified Text

When the user clicks on the text, a pop-up is displayed after the last word in the text.... https://i.stack.imgur.com/VWQCa.png The logic being used is : left = layer.width + layer.x Code : document.getElementById(lightId).style.left = layer.x + docume ...

Guide to updating the value of an input field with Selenium in Python

I have an input element that I need to clear its current value and input a new one. The HTML structure is as follows: <input class="input-mini" type="text" name="daterangepicker_start" value=""> To locate this element, I used the following code: ...

Element eradicated by mysterious force - what is the reason behind this destruction?

I encountered a peculiar issue while working on a JS game demo. For some reason, one of the functions is unexpectedly deleting the container element (even though I didn't intend for it to do so). This function usually creates another element inside th ...

Ways to transform a poster into a clickable link in an HTML video

I am currently working on embedding an mp3 audio file in a browser, along with an accompanying image and a URL for redirection upon clicking. To achieve this, I have placed the mp3 file within a video tag and designated the image as a poster using the foll ...