Creating a clickable cell within a table utilizing an Angular directive

Looking for someone with experience in AngularJS directives and jQuery to help me out.

I have a simple table displayed and I want users to be able to select any cell by clicking on it, and then navigate using the keyboard.

Any ideas on how to achieve this?

Check out my Plunker here: http://plnkr.co/edit/whfYEOV1MV2Rrs4i4g7b

Answer №1

I took the initiative to fork and enhance your Plunker by creating a custom directive for a table. It's essential to use semantic markup when working with tables, so I made some modifications to your HTML code.

Furthermore, I made improvements to your select-me directive to demonstrate how auto-selection on click and arrow key navigation can be implemented.

Check out the updated Plunker here!

layout.html

<table>
  <thead>
    <tr ng-repeat="element in header" class="header-cells" style="width:{{element.width}}px">
      <th>{{element.column}}</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="element in body" data-indexrow="{{$index}}">
      <td ng-repeat="h in header" class="custom-row" data-indexcol="{{$index}}">
        <input type="text" ng-model="element[h.column]" class="body-cell" style="width:{{h.width}}px" select-me>
      </td>
    </tr>
  </tbody>
</table>

selectMe directive (JS)

ct.directive("selectMe", function() {
  return ({
    restrict: "A",
    link: link
  });

  function link(scope, element, attributes) {

    element.on('click', function(e) {
      element.select();
    });

    element.on('keyup', function(e) {
      var $input = angular.element(this),
          $td = $input.parent(),
          $tr = $td.parent(),
          indexrow = parseInt($tr.attr('data-indexrow'),10),
          indexcol = parseInt($td.attr('data-indexcol'),10);

      console.log(indexrow);
      console.log(indexcol);

      // Additional functionality for arrow key navigation can be added 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

Simulated 'paths' configuration in non-TypeScript Node.js environment

In my TypeScript project, I've utilized a helpful configuration option in tsconfig.json that enables me to set up aliases for folders. { "compilerOptions": { "paths": { "@src/*": ["src/*"], "@imag ...

Adjust the body class dynamically based on the URL in AngularJS

Here is the link to my website To Log In - http://localhost/ang/#/login To Access Dashboard - http://localhost/ang/#/dashboard See below for the HTML code for the body tag If the current URL is http://localhost/ang/#/login, then the body should include ...

Struggling with making /api/login/ function properly - using JavaScript and Reddit

Oh no, I've reached my limit of attempts... just received the message "you are doing that too much. try again in 27 minutes." So, I decided to come here and seek help. Here is the request I am making URL: http://www.reddit.com/api/login/ Headers: ...

Encountered a Next-Auth Error: Unable to fetch data, TypeError: fetch failed within

I've been struggling with a unique issue that I haven't found a solution for in any other forum. My Configuration NextJS: v13.0.3 NextAuth: v4.16.4 npm: v8.19.2 node: v18.12.1 When the Issue Arises This particular error only occurs in the pr ...

Whenever I attempt to start my ReactJS project using the command line with `npm run it`, I keep encountering an error

Encountered an issue with the webpack-dev-server script while working on my project. Ensure that your node.js and npm versions are up to date. If they are, the problem might lie within the reactjs package rather than npm itself. Kindly inform the author ab ...

Tips for simulating node-redis with jest

I've been exploring how to use Jest to mock Node Redis by incorporating Redis Mock. // redis.js const redis = require("redis"); const client = redis.createClient({ host: '127.0.0.1', port: 6379 }); // redis.test.js const redisMo ...

Getting a specific piece of information from a JSON file

I am encountering an issue with my JSON file collection. When I access it through http://localhost:5000/product/, I can see the contents without any problem. However, when I try to retrieve a specific product using a link like http://localhost:5000/product ...

Mastering the Vue 3 Composition API: A guide to efficiently utilizing it across multiple Vue instances spread across different files

tl;dr What is the best way to import Vue3 in a base Javascript file and then utilize Vue's composition API in subsequent standalone files that will be loaded after the base file? I incorporate Vue to improve user interactions on specific pages like t ...

choosing between different options within Angular reactive forms

I am attempting to create a select element with one option for each item in my classes array. Here is the TypeScript file: @Component({ selector: 'app-create-deck', templateUrl: './create-deck.component.html', styleUrls: [' ...

When utilizing Cytoscape.js with Angular, there may be an issue with the Graph not loading

I've been tackling a project that involves using Cytoscape.js alongside Angular.js. Unfortunately, I'm encountering an issue with the graph not loading properly upon page refresh. The peculiar scenario is when I navigate to a view containing the ...

Removing buttons from a table row dynamically

Below is how I am adding the Button to Element: (this.sample as any).element.addEventListener("mouseover", function (e) { if ((e.target as HTMLElement).classList.contains("e-rowcell")) { let ele: Element = e.target as Element; let ro ...

Tips for populating a datalist with additional data from an array

I have an array of data that needs to be displayed in a datalist. I want to initially load a limited amount of data and then dynamically load more as the user scrolls down. This functionality must be implemented within the datalist itself, triggering the l ...

$scope does not yield any output

While trying to access the $scope variable within my controller, I encountered an issue. When I console log the $scope, it displays a number of values but returns undefined when accessed directly through $scope. I have included a screenshot of the $scope ...

Using jqGrid to load additional JSON data after the initial local data has already been populated in the

I encountered a specific issue: I have a compact form with four choices. Users can choose to fill them out or not, and upon clicking 'Ok', a jqGrid is loaded with data based on those selections. To accommodate dynamic column formatting, my servle ...

Detecting repeated property values within a collection of embedded objects

I am currently working with a JSON file that contains an array of nested objects and arrays to represent a shopping cart. My goal is to identify duplicate values and update the quantity of the item if duplicates exist, otherwise simply add the items to the ...

oamSubmit.js is absent if f:ajax is used on a webpage

I've encountered a frustrating issue while using MyFaces that is really getting on my nerves. I have tried versions 2.2.10 and 2.2.12, along with PrimeFaces 6.0, although the latter may not be relevant. Every time I add an f:ajax tag to a page, my co ...

The form created using jQuery is not submitting correctly because the PHP $_FILES array is empty

Creating an HTML form dynamically in jQuery and submitting the form data via Ajax to 'add_sw.php' for processing is my current challenge. However, I have encountered an issue where the PHP script cannot access the PHP $_FILES variable as it turn ...

I am unable to connect my jQuery

I've searched far and wide on numerous coding forums for solutions to this issue. It's usually just minor syntax mistakes like forgetting a closing tag or improper src attribute formatting. Despite double-checking, my html and js seem to be erro ...

Issues with react-bootstrap component Switches functionality not operating as expected

It looks like the bootstrap switches are not functioning properly. Even the documentation version is not working as expected <Form> <Form.Check type="switch" id="custom-switch" label="Check this switch" /> <Form.Check ...

What steps can I take to fix the 422 error when making a POST request?

Currently, I am working on a project following a tutorial on Udemy but creating my version. Although I have successfully implemented bcrypt and jwt for user authentication, I am facing issues when trying to make posts in my application. Whenever I attempt ...