Tips for choosing multiple files with the Ctrl key in a list item element:

We have decided to create our own browsing functionality due to the limitations of the existing HTML browse feature. While using the HTML browse, we can only select multiple files by pressing the Ctrl key, and we want to replicate this feature in our custom browse.

<div style="width: 387px; height: 300px; padding-top:5px;margin-right:10px; border: 1px solid white; ">
      <ul>
          <li ng-repeat="folders in folderList">
              <button ng-attr-id="{{ 'object-' + $index}}"
                      style="cursor:pointer;border: 0px solid white; border-radius: 0px; padding-left: 30px;height:auto;margin-bottom: 5px;"
                      class="button btn-bgc bgc-hover"
                      data-ng-click="getFolderList($index)">
                  {{folders.name}}
               <i class="fa fa-hdd-o"
                  ng-if="folders.type === 'Device'"
                  style="display: inline; float:left; padding: 2px; color: #D3D3D3;margin-left: -30px;"
                  aria-hidden="true"></i>
               <i class="fa fa-folder-open"
                  ng-if="folders.type === 'folder'"
                  style="display: inline; float:left; padding: 2px; color: #FFE4B5;margin-left: -30px;"
                  aria-hidden="true"></i>
               <i class="fa fa-file-o"
                  ng-if="folders.type === 'file'"
                  style="display: inline; float:left; padding: 2px; margin-left: -30px;" aria-hidden="true"></i>
             </button>
          </li>
      </ul>
    </div>

When a folder is selected in my application, it opens the folder; when a file is selected, it simply marks the file for future use. In the provided sample plunker, there are 2 folders and 3 files. Currently, only one file can be selected at a time. How can I implement multiple file selection by pressing Ctrl (similar to Windows file selection) and then display those selected files when the "Display Selected Files" button is clicked? Here is the link to the plunker:

https://plnkr.co/edit/REBtXPSH8sa0cvYvp9A4?p=preview

If any additional information is required, please let me know. Any assistance would be greatly appreciated.

// Code goes here

var testController = angular.module('test', []);

testController.controller('testController', ['$scope', '$document', function($scope, $document) {


  $scope.folderList = [{name:"folder1",type:"folder"},{name:"folder2",type:"folder"},{name:"file1.txt",type:"file"},{name:"file2.txt",type:"file"},{name:"file3.txt",type:"file"}];

  $scope.resetBackground = function () {

            for (var i = 0; i < $scope.folderList.length; i++) {
                document.getElementById('object-' + i).style.backgroundColor = "white";
            }
        }

  $scope.getFolderList = function (index) {
    $scope.resetBackground();
    $document.on('keypress', function (event) {
              if(event.keyCode == 17) { // 17 - Ctrl
                  // need to select multiple files here.
              }

      })
    document.getElementById('object-' + index).style.backgroundColor = "#00FF00";
  }; 

}]);

Answer №1

Your code is not following best practices in AngularJS, such as direct DOM manipulation and using the global document variable. I recommend familiarizing yourself with these principles first: https://docs.angularjs.org/tutorial

To address your issue:

In your HTML, make sure to pass the clickEvent into the getFolderList method:

data-ng-click="getFolderList($event, $index)"

In your controller, make sure to check if the ctrlKey flag is true

$scope.getFolderList = function (clickEvent, index) {
  if (clickEvent.ctrlKey) {
    // ctrlKey is pressed while clicking
  } else {
    $scope.resetBackground();  
  }

  document.getElementById('object-' + index).style.backgroundColor = "#00FF00";
}; 

You can view a demonstration on Plunker at https://plnkr.co/edit/QLwZuQa3AHdelJKFSIkL?p=preview (which also includes multi-select functionality for the metaKey on macOS).

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 are the troubleshooting tools available in Electron for detecting errors and debugging issues?

What is the process for accessing error messages and console logs in Electron while developing? Can the logs be saved directly to a file as well? Edit: Similar to how error messages and console logs are shown in Chrome's dev tools, but within Electro ...

What is the correct way to utilize the Vuex mutation payload object effectively?

I have two input fields where I can enter a value to search for either the name of a company or the location. The search functionality works when only one argument is provided in the foundJobs mutation and action. However, when the payload contains an obje ...

What is the best way to call a JavaScript function with multiple arguments from a Silverlight project?

I encountered an issue when trying to invoke a JavaScript function with multiple arguments from an HTML page. Here is what I am attempting to do: wbNavigator.Navigate(new Uri("http://localhost:56433/route.html", UriKind.Absolute)); object results = wbNavi ...

Having trouble with enter key input not triggering?

I have scoured various resources for answers to my query, including Stackoverflow. Unfortunately, none of the posts I came across have helped me resolve my issue with getting the enter key to work in my project for FreeCodeCamp on Codepen. When I press the ...

PHP not displaying radio button values

My latest project involved creating a quiz program using only PHP. The program consists of 4 different questions and radio buttons, and when an option is selected, the value of the radio button (A, B, C, or D) should be printed. I have set up 4 PHP varia ...

Struggling to get ng-cloak to function properly

I've encountered an issue with my custom directive where I am using {{}} to display values, but upon page reload, the {{}} placeholders are visible before the values are set. I attempted to resolve this by utilizing ng-cloak, but unfortunately, it did ...

Discover the package.json file within an Angular application

Currently, I have my app running with ng serve. I'm curious if there is a method to access the package.json file within my app. My initial thought was to move package.json to the directory ./dist and retrieve it from there. However, this does not see ...

Node 18 is having trouble locating NPM and is unable to locate the module './es6/validate-engines.js'

Previously, I attempted to install Node.js without any issues. However, this time around, I am encountering difficulties accessing the npm package. While I can successfully retrieve the version of Node.js after installation, attempting to check npm -v or w ...

Template for Joomla with a white stripe running along the right side

When I resize my browser to be half the width of my screen at www.thames.ikas.sk, a white stripe appears on the right side. Why is this happening? Why aren't my html and body elements taking up the full width of the browser? This issue doesn't oc ...

Reacting to URL Changes but Failing to Load the Requested Page

Welcome to my App Component import React, { useEffect } from 'react'; import './App.css'; import Navbar from './components/layout/Navbar'; import Landing from './components/layout/Landing'; import { BrowserRouter as ...

Find the JavaScript code that selects the previous value chosen

When working with a select in React, I am facing an issue where the console.log is returning the last value selected instead of the current one. For instance, if I select 4 followed by 3 and then 5, the code will display 1 (default value), then 4, and fin ...

Understanding the distinction between deleting and nullifying in JavaScript

var obj = {type: "beetle", price : "xxx", popular: "yes" ,.... }; If I want to remove all the properties from obj, what is the correct way to do it? Should I use delete obj.type; delete obj.price; delete obj.popular ... and so on. Or should I set ob ...

Show the current time using Moment.js

I am currently working on developing a clock component that displays the current time in real-time. Issue: The initial time is correctly displayed when the page loads (HH:mm A), but the clock does not update dynamically. clock.component.ts : import { ...

Retrieving the selected date from mat-datepicker into a FormControl

When creating a POST request to an API, I encountered an issue with the mat-datepicker field as it throws an error when inside the ngOnInit() call (since nothing is selected yet). Other fields like name, email, etc. work fine, but extracting a value from t ...

I possess an item that I must display its title as a <choice> in a <menu> while returning a different variable

I am working with an object: company: { name: 'Google', id: '123asd890jio345mcn', } My goal is to display the company name as an option in a material-ui selector (Autocomplete with TextField rendering). However, when a user selects ...

Tips for downloading a file using a Django function triggered by JavaScript instead of redirecting to the URL

Managing a page with multiple buttons that trigger various functions, such as sending an SMS (via API), emailing a file, or downloading a PDF. The button actions are powered by Ajax requests through JavaScript instead of using forms. I initially used Java ...

Exploring the integration of React Context API within a Next.js application to streamline the authentication process

I am looking to build a react app using Next.js. However, I am currently stuck and need assistance in figuring out how to proceed. I have implemented user authentication on the backend with node.js, passport.js, passport-local-mongoose, and express.sessi ...

The array is failing to pass through ajax requests

Here is the JavaScript code snippet for making an AJAX call. In this scenario, the code variable is used to store a C program code and then pass it on to the compiler.php page. function insert(){ var code = document.getElementById("file_cont").val ...

How can I execute JavaScript within a PHP loop without generating multiple <script> tags?

I'm currently developing a simple WordPress plugin and I would like to have a portion of it write to the footer. In order to achieve this, I need to utilize an inline script that allows me to call PHP within certain functions that retrieve dates from ...

Contact Company by Sending Email According to Selected Product Dropdown Choice

If a customer selects a specific business option from the dropdown menu on the product page, I want to send the standard WooCommerce Order Confirmation Email to both the site admin and that selected business. Backpack $50 Choose an Option Business First ...