Leverage information stored in an array within the HandsonTable Angular directive

Some of my columns in a HandsoneTable created using Angular directives are not rendering when I try to use an array as the data source with common array notation (name[0]). I'm unsure if this is supposed to work like this or if I am doing something wrong.

The data source looks like this:

$scope.data = [
    {
        'name': ['Bob', 'Bobson'],
        'email': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddbfb2bf9daebcb0adb1b8f3beb2b0">[email protected]</a>'
    },
    {
        'name': ['John', 'Johnson'],
        'email': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3852575056784b595548545d165b5755">[email protected]</a>'
    }
];

Here is my template setup:

<div ng-app="Test">
    <div ng-controller="tableCtrl">
        <hot-table datarows="data">
            <hot-column data="name[0]"></hot-column>
            <hot-column data="name[1]"></hot-column>
            <hot-column data="email"></hot-column>
        </hot-table>
     </div>
</div>

You can view a simple example here: https://jsfiddle.net/9qzo3wnv/4/

Answer №1

It is technically not feasible to utilize array notation (name[0]) on the data attribute. However, there are several alternative methods you can employ to achieve the same result:

Approach 1

Transform your data into an object instead.

Example: https://jsfiddle.net/4yuv1vyu/

$scope.data = [
  {
    'name': {0:'Bob',1:'Bobson'},
    'email': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91f3fef3d1e2f0fce1fdf4bff2fefc">[email protected]</a>'
  },
  {
    'name': {0:'John',1: 'Johnson'},
    'email': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7ada8afa987b4a6aab7aba2e9a4a8aa">[email protected]</a>'
  }
];

Approach 2

Convert the array into an object using a controller function:

Example: https://jsfiddle.net/vypahad6/

Details:

$scope.toObject = function(arr) {
  var rv = {};
  for (var i = 0; i < arr.length; ++i)
    rv[i] = arr[i];
  return rv;
}

for (var d in $scope.data) {
  $scope.data[d].name = $scope.toObject($scope.data[d].name);
}

Approach 3

Take advantage of handsontable's custom cell renderer capabilities:

Example: https://jsfiddle.net/sbkwcfr5/

Details:

View:
<hot-column data="name" renderer="customRendererName"></hot-column>

Controller:
$scope.customRendererName = function(instance, td, row, col, prop, value, cellProperties) {
  var html = Handsontable.helper.stringify(value[0]);
  td.innerHTML = html;
  return td;
  return value[0];
}

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

Using Node to upload various large JSON files into mongoDB

Currently, I am dealing with the task of parsing numerous large JSON files into my mongoDB database. To accomplish this, I have been utilizing the stream-json npm package. The process involves loading one file at a time, then manually changing the filename ...

Transferring an Array from PHP to Javascript

I'm attempting to pass a PHP array so that I can use it in JavaScript. The PHP code I have written is shown below: <?php $link = mysqli_connect("localhost", "root", "password", "database"); /* check connection */ if (mysqli_connect_errn ...

Tips for making a React/Socket.IO app accessible from external sources

Trying to get my React app (built with Node.js and Socket.IO) running locally and accessible from external sources has been a challenge for me. I've managed to expose port 3000 using ngrok tunneling, but my socket connection is listening on port 3001. ...

Effective Pagination in Rails 5: Retrieving Total Entries with will_paginate and Active Model Serializers

I have implemented pagination using will_paginate and api-pagination for managing data, along with active_model_serializers to serialize the JSON. My goal is to send the total number of entries of the resource to the client side where AngularJS is being ut ...

URLRouterProvider's otherwise state reloads due to empty state parameters of the parent

My state configurations are set up as follows: $stateProvider .state('parentState', { abstract: true, url: '/:tenantId/', param: { tenantId: { array: false } }, ...

Styling Challenges with CSS/AngularJS Accordion in Footer

I want to create a page layout as shown below: +---------------------------+ | Auto-fill / v scrollable ^| | || | || | v| +---------------------------+ | Fixed [][][] ...

What is the best way to test a try/catch block within a useEffect hook?

Hey, I'm currently dealing with the following code snippet: useEffect(() => { try { if (prop1 && prop2) { callThisFunction() } else { callThatFunction() } ...

Click handler that transmits data to a server using ajax

I have been tasked with creating a website using HTML, CSS, and JavaScript that includes three buttons. When clicked, these buttons will send codes such as "10," "01," and "11" to a C++ program. The C++ program will then respond and perform a function base ...

Troubleshooting: Responsive attribute settings causing ng-click in AngularJS slick carousel to malfunction

I am utilizing the angular wrapper https://github.com/vasyabigi/angular-slick to incorporate the slick slider into my project. Each individual slide within the slider has a ng-click function set up, which directs users to a different page upon clicking. I ...

strange occurrences with the angularjs directive when enclosed

In my current project, I am working with a directive that wraps an element within a container. Here is the code snippet: app.directive('myDirective', function($compile, $timeout) { var num=0; return { link: function(scope, el, attrs) { ...

I'm attempting to create a text toggle button for displaying more or less content

I am currently working on implementing a show more/show less button feature. However, the current version is not very effective as I only used slicing to hide content when the state is false and display it all when true. Now, my goal is to only show the ...

Reposition div when clicked

I have encountered a challenge where I am unable to perform a small task. My goal is to have the position of "div1" change upon clicking on "div2", taking into account that "div2" is nested inside "div1". Additionally, when clicking on "div2" again, "div1" ...

Including additional data to a page after each iteration in order to display the current progress

I am currently working on a loop that iterates through the lines of a text area and processes each line sequentially. However, I am facing an issue where the page becomes unresponsive until all the data has been processed. Is there a way to dynamically u ...

"Utilizing Node.js and Express to return JSONP data based on

For some reason, my Express application is giving me a strange result when using res.jsonp. It looks like this: /**/ typeof jsonp1406719695757 === 'function' && jsonp1406719695757({"published":true,"can_add_to_cart":true,"updated_at":"20 ...

Struggling to navigate the world of JavaScript and find the sum of odd numbers?

Currently facing a roadblock with a codewars exercise and in need of some assistance. The exercise involves finding the row sums of a triangle consisting of consecutive odd numbers: 1 3 5 7 9 11 13 15 17 ...

The process of uploading a file is interrupted by an AJAX Timeout

My HTML form includes a file input field that utilizes AJAX to upload the selected file, complete with a progress bar. However, I encountered an issue where the request would hang without any response. To prevent this from happening in the future, I aim t ...

Unnecessary PHP code will run on its own in JavaScript

Attempting to initiate a session in PHP and assign a value to a session variable within a Javascript function on a PHP/HTML page is creating an issue. The problem arises when the PHP code inside the if statement runs automatically upon loading the page, ra ...

Toggle between resizing a set of boxes and fading in/out their images

I have a series of clickable boxes that I want to expand and hide the image when clicked. Additionally, I need to be able to close any previously opened box by returning it to its original height and width while fading back in its image content. The .info ...

Incorporate unique color variables from the tailwind.config.js file

I am working on a project using nextjs and typescript, with tailwind for styling. In my tailwind.config.js file, I have defined some colors and a keyframe object. My issue is how to access these colors to use as background values in the keyframe. Here is ...

I designed a dropdown menu with a searchable <mat-select> feature, where selecting an item is automatic when a space bar is pressed in the search box

Description : I have implemented a dropdown list using element that contains a list of items along with a search field. This allows users to easily search for specific items in the list instead of manually scrolling through a long dropdown menu. They can ...