What is the best way to transfer a table row from one table to another in AngularJS?

I need assistance with a feature that allows users to move rows between tables by clicking on buttons. When the 'up' button is clicked, the 2nd table row should be moved to the 1st table, and when the 'down' button is clicked, the 1st table row should be moved to the 2nd table.

Incorporating this functionality in HTML:

        <table class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>Tag Name</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="tag in vm.tags | filter: searchBar"
                    ng-init="selectedIndex = $index">
                    <td><span ng-click="vm.editTags(selectedIndex);"
                        tooltip="Click to edit tag">{{tag.name}}</span></td>
                    <td><span ng-click="vm.editTags(selectedIndex);"
                        tooltip="Click to edit tag">{{tag.status}}</span></td>
                    <td><span class="glyphicon glyphicon-remove change-color"
                        ng-click="vm.removeTag(selectedIndex)" tooltip="Delete tag">
                    </span></td>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="top-bottom-padding col-sm-1">
        <button type="button" class="btn change-color bg-white">
            <span class="glyphicon glyphicon-arrow-down glyphicon-lg"></span>
        </button>
    </div>
    <br />
    <div class="top-bottom-padding col-sm-1">
        <button type="button" class="btn change-color bg-white">
            <span class="glyphicon glyphicon-arrow-up glyphicon-lg"></span>
        </button>
    </div>

        <div class="clear pull-right col-sm-1">
          <br/>
            <button ng-disabled="vm.selectedOrder.status == 'Activated'"
                class="pull-left btn btn-primary margin-Left"
                ng-click="vm.editStatus();">Activated</button>
            <button ng-disabled="vm.selectedOrder.status == 'Paused'"
                class="pull-left btn btn-primary margin-Left"
                ng-click="vm.editStatus();">Paused</button>
        </div>
    <div class="clear top-bottom-padding col-sm-1">
        <table class="table table-color table-bordered table-hover">
            <thead>
                <tr>
                    <th>Rotation Order</th>
                    <th>Tag Name</th>
                    <th>Status</th>
                </tr>
            </thead>
            <!-- ng-sortable - External component used for re-arranging rows of table -->
            <tbody ng-sortable="{animation:150}">
                <tr ng-repeat="orders in  vm.rotationTable"
                    ng-class="{selected : orders == vm.selectedOrder}"
                    ng-click="vm.setSelected(orders)">
                    <td>{{orders.rotationOrder}}</td>
                    <td>{{orders.tagName}}</td>
                    <td>{{orders.status}}</td>
                </tr>
            </tbody>
        </table>
</div>

Implementation in JS:-

// Insert JavaScript code here



var myApp = angular.module('myApp',[]);
  myApp.controller('tagController', ['$scope',function ($scope) {

  var vm = this;  
  vm.tags = [
        {
            "name": "Tag A",
            "status": "Activated"
        },
        {
            "name": "Tag B",
            "status": "Paused"
        },
         {
             "name": "Tag C",
             "status": "Paused"
         },
         {
             "name": "Tag D",
             "status": "Activated"
         }
  ];
  vm.rotationTable = [
      {
          "rotationOrder": "1",
          "tagName": "Tag D",
          "status":"Activated"
      },
      {
          "rotationOrder": "2",
          "tagName": "Tag E",
          "status": "Paused"
      },
      {
          "rotationOrder": "3",
          "tagName": "Tag F",
          "status": "Activated"
      }
  ]
  vm.selectedOrder = null;
  vm.setSelected = function (order) {
      vm.selectedOrder = order;
      console.log(vm.selectedOrder);
  };

  vm.editStatus = function () {
      if (vm.selectedOrder.status == "Paused")
      {
          vm.selectedOrder.status = "Activated";
      }
      else if (vm.selectedOrder.status = "Activated")
      {
          vm.selectedOrder.status = "Paused";
      }
  };

     vm.addTags = function () {
      var modalInstance = $modal.open({
          templateUrl: './views/admin/addNewTag.html',
          controller: 'addNewTagController',
          controllerAs: 'vm',
          resolve: {
              tagsData: function () {
                  return vm.tags;
              }
          }
      });
  };


  vm.editTags = function (whichTag) {
      vm.whichTag = whichTag;
      var modalInstance = $modal.open({
          templateUrl: './views/admin/updateTag.html',
          controller: 'updateTagController',
          controllerAs: 'vm',
          resolve: {
              tagsData: function () {
                  return vm.tags;
              },
              whichTag: function () {
                  return vm.whichTag;
              }
          }
      });
  };      

  vm.removeTag = function (index) {
      vm.tags.splice(index, 1);
  };

 }]);

You can view the complete code at this link:http://plnkr.co/edit/bsoTjFpJrEu3nG6cnjci?p=preview

Answer №1

If you want to transfer an element from list A to list B, follow these steps:

var index = 0; // choose the index of the element in list A
listB.splice(0, 0, listA.splice(index, 1)[0]);

Similarly, to move an item from list B back to list A:

var index = 0; // select the index of the element in list B
listA.splice(listA.length - 1, 0, listB.splice(index, 1)[0]);

Answer №2

Here is an example in html:-
<button type="button" class="btn change-color bg-white" ng-click="vm.moveRight();">
  <span class="glyphicon glyphicon-arrow-right  glyphicon-lg"></span>
 </button>

<button type="button" class="btn change-color bg-white" ng-click="vm.moveLeft();">
    <span class="glyphicon glyphicon-arrow-left  glyphicon-lg"></span>
</button>

This is how it looks in js:-

When moving right:
vm.moveRight = function () {
             var selectedIndex = vm.selectedTag;
              vm.rotationTable.push(vm.selectedTag);
              vm.tags.splice(index,1);
             };

When moving left:
      vm.moveLeft = function () {
          var selectedIndex = vm.selectedOrder;// or any valid index from vm.tags
          vm.tags.push(vm.selectedOrder);//({ name: vm.tagsData.name, status: vm.tagsData.status });
          vm.rotationTable.splice(index, 1);
      };

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

Retrieve the values and IDs for every span element within the form

I have been attempting to retrieve all span elements within a form and make them editable input text fields. Once you click away, they should revert back to span elements. I will provide a live example in the attached fiddle. I tried my hand at it, but the ...

What is the best way to create a reliable and distinct identifier in React while using server-side rendering (

Currently, I am utilizing SSR within Next.js. My goal is to create a unique ID within a component in order to use it as an attribute for a DOM element's id. Since this component might be utilized multiple times on a single page, the ID needs to be dis ...

Leverage the power of Angular by configuring a custom webpack setup to

Implementing the CSS modules concept in my Angular app has been a challenge due to conflicts with existing frontend CSS. My project utilizes SCSS, and I am looking for a way for webpack to modularize the CSS generated from SCSS during the final build step. ...

What is the best way to utilize a single AngularJS 1.5 component multiple times within a single view?

While working on a project using AngularJS 1.5's new components, I encountered an issue with widget isolation. It seems that when I use the same widget multiple times, they end up sharing their controller or scope. I was under the impression that comp ...

Accessibility considerations for anchor tags with no content

Currently on our website, there is a component that utilizes an <a> tag with a background image set by the following CSS: a.class-name::after { content: url('image.png'); } The anchor tag itself has no visible content (appears as < ...

Struggling with Bootstrap 5: Issue with full width footer display

Struggling with making the Bootstrap footer in full width? I've been using the Bootstrap footer example but can't seem to figure out how to achieve a full-width footer. https://i.sstatic.net/uGOdz.png <link rel="stylesheet" href="https://c ...

What could be preventing my image from displaying in the header section?

Having trouble displaying the image I added in the header section as a logo. The code works for my online course instructor, but no luck for me. I've tried different images and links to no avail. If you would like to take a look at the codes I used, ...

What steps can I take to modify the class of a button once it has been clicked using JQuery?

Currently, I am experimenting with Jquery to dynamically change the classes of bootstrap buttons when they are clicked. However, I have encountered a limitation while using toggleClass. The issue is that I am only able to toggle between two classes, whic ...

The proper way to close file descriptors on a terminated HTTP connection in a Node.js environment

I am currently experimenting with Node.js, where I am attempting to stream files from the filesystem over HTTP. However, my experience on an OS X Lion machine has been hindered by a buggy Apache Bench (ab) app that prematurely aborts connections. This issu ...

React material ui is not compatible with custom styles that are applied to components

I've developed a unique UserIcon Component that showcases an icon along with accompanying text. Take a look at the code snippet below: import React from "react"; import PropTypes from "prop-types"; import { withStyles, Avatar, Typography } from "@mat ...

"Material-Table does not have the ability to insert a new row

Just started learning JS and React. I'm attempting to integrate Material-table with an add row button, but encountering issues where the added row is not reflecting. Every time I refresh the page, the rows are reset. I suspect there's a problem w ...

Attempting to insert an element after the .load event has been triggered

I have been attempting to update the contents of ".myShop" after it's loaded from an external XML file, but without success. I believe my timing is correct, but I suspect that a loaded element may not be part of the DOM? Here's what I'm exp ...

Ways to confirm the presence of strings within an array?

For instance: var array = ["apple", "banana", "cherry", "date", "elderberry", "fig"]; What is the best way to determine if "apple", "banana", and "cherry" are present in the array? I attempted using the indexOf() method but struggled to check for multip ...

The JavaScript program is occasionally receiving unconventional input

I'm really struggling with this one. As part of a programming exercise, I am developing a JavaScript calculator. You can access the functioning calculator here on Codepen. At the bottom left corner of the calculator interface, you will notice a "+-" ...

Invisible style date input value on iPhone mobile devices

My goal is to enhance the appearance of an HTML5 input with a custom icon, using a font pack. Although I mention FontAwesome in this example, in reality, I'm utilizing a different custom font pack. On my mobile device (iPhone X), I can see the calend ...

How to use CSS animations to remove an element from the DOM

My website has a structured HTML layout with product containers and individual products. I also have a JavaScript function that can remove any product from the page. <div class="products-container"> {foreach from=$cart.products item=product} & ...

Update your AngularJS to the most recent version of Angular

We are faced with the task of upgrading a legacy MVC website that currently utilizes AngularJs to the latest version of Angular. Surprisingly, our current website only scratches the surface of what Angular has to offer - mainly using it for databinding and ...

Using Jquery to hide or show objects within a <div> when clicked

My goal is to create a webpage that displays three different contents based on which button is clicked. Below is the code for reference: I want the page to show three specific sections: A search bar only when the 'search' button is clicked, T ...

What is the best way to ensure that the same information is included on every page of

Currently developing a dynamic website where certain components such as the fixed header, menubar, and footer are common across multiple pages. What is the best way to include these fixed components on every webpage? I am utilizing JSP and JavaScript for ...

Changing the background color using jQuery switch case statement

I am attempting to utilize jquery to dynamically change the background image of a webpage based on different cases in a JavaScript switch statement. I have completed three steps: 1) Included this script tag in my HTML document: <script src="http://co ...