0

i am trying to assign data to dtOptions - but not works. what is the correct way to do it?

here is my function:

 getDatas(){
        this.datas = this.http.get('data/data.json').subscribe(values =>  values);//values available  here
    }

    ngOnInit() {

        this.categoryListCopy = this.categoryList;
        this.getDatas()

        const that = this;
        this.dtOptions = {

            columns:[{
                data: 'id'
            }, {
                data: 'firstName'
            }, {
                data: 'lastName'
            }],
            pagingType: 'full_numbers',
            pageLength: 10,
            serverSide: false,
            processing: true,
            ajax: this.datas
            };



        }

1 Answer 1

1

You just need to use Observable, your code will look like this:

getDatas(): Observable{
        return this.http.get('https://jsonplaceholder.typicode.com/todos/1');
    }

  ngOnInit() {
        this.getDatas().subscribe(values =>  {
           console.log(values);
           // You can assign dtOptions here, you will have your data in values.
        });      
  }

You can find the working plunk here.

Sign up to request clarification or add additional context in comments.

2 Comments

as well how to update with adding some filter later? ( user want to see x item of data only )
You can simply use .map and .filter to filter out your response, please look for the how to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.