0

I'm a little new to angular and was wondering how I can set the global configuration for datatables. Currently it seems I can only set it within each component that need to use it, but this is clearly bad practice. Is there any way to set the configuration globally and have it be available to all component templates?

I want to be able to set the below in a single location and have any component template access it through [dt-options]="dtOptions"

dtOptions: DataTables.Settings = {
    language: {
        paginate: {
            first: "",
            previous: "<<",
            next: ">>",
            last: ""
        }
    },
    lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]]
}

Hope this makes sense

Angular version: 12.2.3

Datatables version: 12.0.0

1 Answer 1

1

You could create environment.ts files. This files then can be imported inside your component.

// inside environment.ts    
export const dtOptions = {
        language: {
            paginate: {
                first: "",
                previous: "<<",
                next: ">>",
                last: ""
            }
        },
        lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]]
    }

Use your configuration inside your components like here:

import { dtOptions} from '../path/to/environment.ts';

@Component({
  ...
})
export class FilterComponent implements OnInit {
   ngOnInit() {
     console.log(dtOptions.language);
   }
}

It is also possible to create different environment configurations for each stage configuration defined in angular.json.

Angular docs

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

4 Comments

Hi Martin, thanks. However, when I try to add this to the environment.ts file the DataTables.Settings throws the error "Cannot use namespace 'DataTables' as a value." and the export throws the error "Declaration or statement expected."
You have to define either a type or an interface instead using DataTables.Settings. Could you please show whats behind 'DataTables.Settings'
It works if I add it like this. This what you meant? export const environment = { production: false, dtOptions: { language: { paginate: { first: "", previous: "<<", next: ">>", last: "" } }, lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]] } }; And then I am able to assign it in my component like this: dtOptions: DataTables.Settings = environment.dtOptions;
Yes somehow i forgot to add the const keyword. I´ll update the anwser

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.