Guide on executing get, modify, append, and erase tasks on a multi-parameter JSON array akin to an API within Angular

I have a JSON array called courseList with multiple parameters:

public courseList:any=[
      {
        id:1,
        cName: "Angular",
        bDesc: "This is the basic course for Angular.",
        amt: "$50",
        dur: "120 Hours"
      },
      {
        id:2,
        cName: "React",
        bDesc: "This is the basic course for React.",
        amt: "$55",
        dur: "125 Hours"
      },
      {
        id:3,
        cName: "MEAN Stack",
        bDesc: "This is the basic course for MEAN Stack.",
        amt: "$100",
        dur: "160 Hours"
      },
      {
        id:4,
        cName: "MERN Stack",
        bDesc: "This is the basic course for MERN Stack.",
        amt: "110",
        dur: "155 Hours"
      },
      {
        id:5,
        cName: "Full Stack",
        bDesc: "This is the basic course for Full Stack.",
        amt: "$180",
        dur: "180 Hours"
      }
    ];

The API link I previously used is inactive now. Therefore, I want to use this courseList array in my service.ts file to perform CRUD (Create, Read, Update, Delete) operations through a Reactive Angular Form.

To do this, I need to replace the API with the courseList array. Whenever data is added, updated, or deleted through the form submission, the changes will reflect in the table along with existing data. Deleted data will not be shown.

A set of Radio buttons allows selecting a specific record from the table for updating or deleting.

The Reactive Form has four fields:

Course Name=>cName

Brief Description=>bDesc

Amount=>amt

Duration=>dur

Below are sections of my code...

course.service.ts

export class CourseService {

  constructor(private http:HttpClient) { }
  // All courses related API will be defined here
  private Base_Url:string = ""
  getAllCourses(){
    return this.http.get(`${this.Base_Url}`);
  }
  // Add Course Data
  addCourseData(Data:any){
    return this.http.post(`${this.Base_Url}`,Data);
  }
  // Update Course Data
  updateCourseData(ID:any,Data:any){
    return this.http.put(`${this.Base_Url}${ID}`,Data);
  }
  // Delete Course Data
  deleteCourseData(ID:any){
    return this.http.delete(`${this.Base_Url}${ID}`)
  }
}

admin.component.ts

export class AdminComponent {
  public page:number=1;
  public courseList:any=[...];
    public selectedCourseData:any=[];
    public courseData!:FormGroup;
  constructor(private cService:CourseService,private fbuilder:FormBuilder) {...}

  ...

admin.component.html

<div class="container-fluid">

...

</div>

If there are any errors in how I've connected courseList array to the getAllCourses(){} method in service.ts and the subsequent subscriber({}) method in populatecourses() function in component.ts, please guide me as I'm new to this area. Your help is much appreciated.

Answer №1

If you're looking for the necessary information, check out of on https://rxjs.dev/api/index/function/of. Once you have that, you can utilize it to turn the courseList into an observable.

  fetchAllCourses(){
    return of(courseList);
  }

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

Resolve the issue of text overlapping on an image when zooming in

There seems to be an issue with overlapping text and images when zooming the page. I have included a screenshot for reference, please take a look and provide a solution. Thank you in advance. Here is the CSS code causing the overlap: .Pad{ padding: 6 ...

Narrowing Down State Types

I am working on a functional component in NextJS with props passed from SSR. The component has a state inside it structured like this: const MyComponent = ({err, n}: {err?: ErrorType, n?: N})=>{ const [x, setX] = useState(n || null) ... if(e ...

Setting up "connect-redis" in a TypeScript environment is a straightforward process

Currently, I am diving into the Fullstack React GraphQL TypeScript Tutorial I encountered an issue while trying to connect Redis with express-session... import connectRedis from "connect-redis"; import session from "express-session"; ...

Is it possible for anyone to access a website's source code using a web browser?

As I navigate the complex world of storing authentication tokens securely using Angular, with the added layer of encryption in the front end before placing it in browser local storage to prevent unauthorized decoding, I have encountered various conflicting ...

disappearing of vue event on single file component HTML element

I'm currently working on an ElectronJs project with Electron Forge, using the Webpack + Typescript template project In addition to that, I've integrated Vue and vue-loader for webpack in order to utilize Single File Component (SFC) files: { ...

What is the best way to adjust the output size for ngx-webcam?

I am looking to determine the smallest possible size for an image that is captured. From my testing with ngx-webcam, I have found that I can adjust the minimum height of the output image based on the display height of the webcam. Is there a method to set ...

What is a way to nest a promise request within another request?

Q: How can I create a new promise within a request, and then return it in a nested request? Here is the code snippet: request({ // --------------request 1------------ url: request_data.url, method: request_data.method, form: request_data.data ...

Strategies for managing the "ref" property of Material-UI component props within our custom components

I have a unique custom component setup as shown below: // ... import { PaperProps, styled } from '@mui/material'; type ComponentProps = PaperProps & { a: string, b: string }; export const MyPaper = styled(Paper)(/* ... */); const Compo ...

Guide to utilizing services in Angular 2

As I've developed a service with numerous variables and functions, my goal is to inject this service into multiple components. Each component should have the ability to update certain variables within the service so that all variables are updated once ...

Determine the sum of exported identifiers based on ESLint rules

Currently, I am facing a requirement in my JavaScript/TypeScript monorepo to ensure that each library maintains a minimal amount of exported identifiers. Is there any existing eslint rule or package available that can keep track of the total number of exp ...

Restricting the number of lines within a paragraph in Angular 2

Is there a method to limit the number of lines in a <p> tag and add an ellipsis (...) at the end? Using character count for truncation doesn't work well as the width of the element varies according to device screen size. ...

Filter the array while maintaining its current structure

I'm struggling to create an array filter that can handle exact and partial data within a nested array structure. The challenge is maintaining the integrity of the top-level structure while filtering based on data in the second layer. Here's an ex ...

Determining in Angular 8 whether a value has been altered by a user or by a method call

Within my select element, the value is currently being assigned through an ngOnInit call. Here is an example of the HTML code: <select name="duration" [(ngModel)]="exercisePlan.duration" (ngModelChange)="onChange($event)"> <option *ngFor="l ...

Steps for inserting a new entry at the start of a dictionary

My fetch method retrieves recordings from the database, but I need to prepend a new record to the existing data for frontend purposes. Can someone assist me with this? <script> export default { components: { }, data: function() { ...

Child component not inheriting Angular Material styles

I am experiencing an issue with the default styles of Angular Material. I have a parent dashboard component with child components named "HomeComponent" and "RegistrationComponent". The Input box and button from Angular Material work properly on the dashboa ...

Creating a functional component in React using TypeScript with an explicit function return type

const App: FC = () => { const addItem = () => { useState([...items, {id:1,name:'something']) } return <div>hello</div> } The linter is showing an error in my App.tsx file. warning There is a missing return type ...

Any tips on making Angular 8's sort table function seamlessly integrate with data retrieved from Firebase?

I am currently working on an angular PWA project that utilizes a material table to display data from Firebase. The data is being shown properly and the paginator is functioning as expected. However, I am facing an issue with sorting the data using mat-sort ...

The type "AppRouterInstance" cannot be assigned to type "nextRouter"

Within my Next.js project, a registration form is included as seen below: "use client"; import * as React from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form" ...

The issue here pertains to npm's inability to successfully retrieve a required dependency for download

C:\Users\Manoj\Desktop\accounts>npm install intro.js --save npm ERR! code ENOENT npm ERR! syscall spawn git npm ERR! path git npm ERR! errno ENOENT npm ERR! enoent Error while executing: npm ERR! enoent undefined ls-remote -h -t ssh: ...

When I try to reverse the words in a string, I am not receiving the desired order

Currently delving into TypeScript, I have set myself the task of crafting a function that takes in a string parameter and reverses each word within the string. Here is what I aim to achieve with my output: "This is an example!" ==> "sihT ...