Categories
JavaScript Answers

How to export JavaScript data to a CSV file without server interaction?

Sometimes, we want to export JavaScript data to a CSV file without server interaction.

In this article, we’ll look at how to export JavaScript data to a CSV file without server interaction.

How to export JavaScript data to a CSV file without server interaction?

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

For instance, we write

const csvString = csvRows.join("\r\n");
a.href = "data:application/csv;charset=utf-8," + encodeURIComponent(csvString);

to create the csvString with the CSV data.

Then we put the data in a base64 string with

"data:application/csv;charset=utf-8," + encodeURIComponent(csvString)

And we assign the value to a.href where a is an a element.

Then when we click on the link, the data will be downloaded.

Conclusion

To export JavaScript data to a CSV file without server interaction, we can set a link to a base64 string with the CSV content.

Categories
React Answers

How to create constants file with React?

Sometimes, we want to create constants file with React.

In this article, we’ll look at how to create constants file with React.

How to create constants file with React?

To create constants file with React, we can create a JavaScript file with variables exported.

For instance, we write

fileWithConstants.js

export const ACTION_INVALID = "This action is invalid!";
export const CONSTANT_NUMBER_1 = "hello I am a constant";
export const CONSTANT_NUMBER_2 = "hello I am also a constant";

to export the ACTION_INVALID, CONSTANT_NUMBER_1 and CONSTANT_NUMBER_2 constants in a file.

Then we can use them by writing

import * as consts from "path/to/fileWithConstants";

const errorMsg = consts.ACTION_INVALID;

to import all the contents of fileWithConstants.js that are export and reference ACTION_INVALID with consts.ACTION_INVALID.

How to create constants file with React?

To create constants file with React, we can create a JavaScript file with variables exported.

Categories
Vue Answers

How to center content vertically on Vuetify?

Sometimes, we want to center content vertically on Vuetify.

In this article, we’ll look at how to center content vertically on Vuetify.

How to center content vertically on Vuetify?

To center content vertically on Vuetify, we can use the align and justify props.

For instance, we write

<template>
  <v-container fill-height fluid>
    <v-row align="center" justify="center">
      <v-col></v-col>
    </v-row>
  </v-container>
</template>

to set the align and justify props to set the align-items and justify-content CSS properties respectivelt on the v-row.

Since align is set to center, the content should be vertically centered.

Conclusion

To center content vertically on Vuetify, we can use the align and justify props.

Categories
JavaScript Answers

How to open PDF string in new window with JavaScript?

Sometimes, we want to open PDF string in new window with JavaScript.

In this article, we’ll look at how to open PDF string in new window with JavaScript.

How to open PDF string in new window with JavaScript?

To open PDF string in new window with JavaScript, we can open an empty window and then render an iframe in it.

For instance, we write

const pdfWindow = window.open("");
pdfWindow.document.write(
  `<iframe width='100%' height='100%' src='data:application/pdf;base64,${encodeURI(
    yourDocumentBase64VarHere
  )}''></iframe>`
);

to call window.open with an empty string to open an empty window.

Then we call pdfWindow.document.write with a string with an iframe element in it.

And its src attribute is set to the base64 PDF URL.

Then the PDF will be rendered in the iframe.

Conclusion

To open PDF string in new window with JavaScript, we can open an empty window and then render an iframe in it.

Categories
JavaScript Answers

How to change Google Map marker color with JavaScript?

Sometimes, we want to change Google Map marker color with JavaScript.

In this article, we’ll look at how to change Google Map marker color with JavaScript.

How to change Google Map marker color with JavaScript?

To change Google Map marker color with JavaScript, we can use the setIcon method or the Marker constructor.

For instance, we write

marker.setIcon("http://maps.google.com/mapfiles/ms/icons/green-dot.png");

to call setIcon with the URL string of the green marker icon.

We can do the same thing when we create the marker with

const marker = new google.maps.Marker({
  icon: "http://maps.google.com/mapfiles/ms/icons/green-dot.png",
});

Conclusion

To change Google Map marker color with JavaScript, we can use the setIcon method or the Marker constructor.