Transform a row in an ng Smart table to a routerlink using Angular 2

I've been exploring ng2 Smart Table and I'm looking to convert a row (or even cell data) into a clickable link using routerlink. The current method I'm employing to retrieve some of my row's data is as follows:

onUserRowSelect(event) 
{ 
    console.log('user row select: ', event);
    this.selected = event.selected;
    console.log('selected list: ', this.selected);
    this.router.navigate(["/dashboard"]);
    console.log("this is a test ");
}

It's worth noting that the event comes from the HTML file, and I utilize this.router.navigate to move to another page. However, I keep encountering an error message stating:

TypeError: this.router is undefined Stack trace: [1215]/SmartTables.prototype.onUserRowSelect@http:

Could somebody please explain 1) what might be causing this issue, and 2) how I can extract the data received from the event? It returns an Object. Thank you!

Answer №1

To set up your settings in the column, follow these instructions:

objektelinks: {
title: 'Objektelink',
type: 'custom',
renderComponent: LinkViewComponent,
},

You can create a render component like this:

import { Component, OnInit, Input, Output, EventEmitter } from 
'@angular/core';
import { ViewCell } from 'ng2-smart-table';

@component({
selector: 'ngx-link-view',
template: <a [routerLink]= "['/auth/register']"> Objekte </a>,
})
export class LinkViewComponent implements ViewCell, OnInit {
renderValue: string;

@input() value: string | number;
@input() rowData: any;

@output() save: EventEmitter = new EventEmitter();

ngOnInit() {
// add your code here
}


}

Answer №2

Dealing with a similar issue myself, but I've found a potential solution:

  1. The first problem to address is:

    • Make sure you have imported Router from '@angular/core'. If not, add this line: import { Router / ... / } from "@angular/router";

    • Ensure you have passed the Router object to the constructor. If not, include it like so: constructor(private router:Router) {/ ... /}

  2. Next up is problem #2:

    If you log your Object, you should see it as an expandable list in the browser console. You can then easily access any data you need.

    To extract specific data, try something like this:

    event.data.someValue;

I hope this information proves helpful in resolving your issue.

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

Extract Method Parameter Types in Typescript from a Generic Function

Can we retrieve the type of parameters of methods from a generic interface? For instance, if we have: interface Keys { create: any; ... } type MethodNames<T> = { [P in keyof Keys]: keyof T; } Then, is it feasible to obtain the type of paramete ...

Even after the component is destroyed, the subscription to the service observable continues to emit

I'm facing an issue with an observable in my service. The provided code below demonstrates this: @Injectable({ providedIn: 'root' }) export class MyService { public globalVariable: BehaviorSubject<string> = new BehaviorSubject(&ap ...

How can I generate codegen types using typeDefs?

I have been exploring the capabilities of graphql-codegen to automatically generate TypeScript types from my GraphQL type definitions. However, I encountered an issue where I received the following error message: Failed to load schema from code file " ...

Retrieve the value of [innerHTML] from a subcomponent

I am trying to retrieve the [innerHTML] from a child component Greetings everyone There is a component named 'contact-us' containing the following field. <div [innerHTML] = "legalContent.disclaimer"></div> I have included ...

Definition of TypeScript array properties

Having some trouble creating a type for an array with properties. Can't seem to figure out how to add typings to it, wondering if it's even possible. // Scale of font weights const fontWeights: FontWeights = [300, 400, 500]; // Font weight alia ...

Trouble with Nextjs link not functioning properly with a URL object when incorporating element id into the pathname

Recently I added translations to my website, which means I now need to use a URL object when creating links. Everything has been going smoothly with this change except for one issue: when I try to click a link that points to /#contact. When simply using h ...

Typescript - optional type when a generic is not given

I am hoping for optionalFields to be of type OptionalFieldsByTopic<Topic> if a generic is not provided, or else OptionalFieldsByTopic<T>. Thank you in advance for the assistance. export interface ICreateItem<T extends Topic = never> { // ...

Tips for ensuring only one property is present in a Typescript interface

Consider the React component interface below: export interface MyInterface { name: string; isEasy?: boolean; isMedium?: boolean; isHard?: boolean; } This component must accept only one property from isEasy, isMedium, or isHard For example: <M ...

Exploring Typescript keyof in Storybook's User Interface customizations

Currently, I am working on developing components for integration with Storybook, but I am encountering an issue related to Typescript inferred types. While striving for code reusability, I prefer not to specify the options for a control within the story i ...

Hide Decimal Places with Angular Pipe

I am working with a progress bar that can have a progress ranging from 0.1 to 99.999 In the TypeScript file, I prefer not to use the math.round function as it will disrupt the logic. My intention is to display the progress without decimal points, for exam ...

Issue encountered while setting up controls and AbstractControls in form development

Here is a snippet of code showing how I create and manipulate a form in Angular: this.myForm = new FormGroup({ points: new FormArray([ new FormGroup({ date: this.date, startTime: new FormControl(null, Val ...

Concealed URL - Navigation with Angular 2 Routing

Is it possible to conceal the URL when using window.open()? Alternatively, can the Angular2 router be utilized to open the URL in a fresh tab? I am familiar with the method of concealing the URL during routing by using this.router.navigate(["/Pages"], { s ...

What is the best way to preserve the information from the previous page when returning to it after visiting a new one?

My current setup involves 4 components, each with a 'next' button (except the last) and a 'previous' button (except the first). When I click on the Next button, it takes me to the following component. However, when I navigate back to th ...

Child Route Handling Based on Conditions (Angular)

Can a child route be conditionally displayed as a default? I want users to land on a specific child route based on their past orders (e.g., go to the store page if they ordered from a physical store, online page for online orders, or social page otherwise ...

Ways to specify a setter for a current object property in JavaScript

Looking to define a setter for an existing object property in JavaScript ES6? Currently, the value is directly assigned as true, but I'm interested in achieving the same using a setter. Here's a snippet of HTML: <form #Form="ngForm" novalida ...

What is the importance of having the same data type for the searchElement in the argument for Array.prototype.includes()?

Is there an issue with my settings or is this a feature of TypeScript? Consider the code snippet below: type AllowedChars = 'x' | 'y' | 'z'; const exampleArr: AllowedChars[] = ['x', 'y', 'z']; f ...

Having trouble with Angular CLI on your Windows 10 system?

Recently, I reinstalled node.js, npm, and angular-cli on my Windows 10 PC. However, after installing angular-cli using the command npm install -g @angular/cli, I encountered an issue. Despite having node.js version 8.1.2 and npm version 5.0.3, whenever I t ...

When reference variables are utilized before being parsed, what happens?

I'm a beginner learning Angular and have a question regarding the use of reference variables. Here is an example code snippet: <div class="bg-info text-white p-2"> Selected Product: {{product.value || '(None)'}} </div> <di ...

When incorporating leaflet-routing-machine with Angular 7, Nominatim seems to be inaccessible

Greetings! As a first-time user of the Leafletjs library with Angular 7 (TypeScript), I encountered an error while using Leaflet routing machine. Here is the code snippet that caused the issue. Any ideas on how to resolve this problem? component.ts : L. ...

Is it possible to conceal dom elements within an ng-template?

Utilizing ng-bootstrap, I am creating a Popover with HTML and bindings. However, the ng-template keeps getting recreated every time I click the button, causing a delay in the initialization of my component. Is there a way to hide the ng-template instead? ...