Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Textarea, Checkbox with Three States, and Button

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Textarea

PrimeVue comes with the Textarea component to let us add a text area into our Vue 3 app.

To add it, we write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Textarea from 'primevue/textarea';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Textarea", Textarea);
app.mount("#app");

App.vue

<template>
  <div>
    <Textarea v-model="value" autoResize rows="5" cols="30" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: '',
    };
  },
  methods: {},
};
</script>

The autoResize prop will make it auto resize according to the text entered.

rows and cols sets the number of rows and columns to display respectively.

Toggle Button

We can add the ToggleButton component to let us add a toggle button into our Vue 3 app.

For example, we can write:

<template>
  <div>
    <ToggleButton
      v-model="checked"
      onLabel="yes"
      offLabel="no"
      onIcon="pi pi-check"
      offIcon="pi pi-times"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      checked: false,
    };
  },
  methods: {},
};
</script>

to add a toggle button that binds to the checked reactive property.

onLabel has the text that’s displayed when checked is true .

offLabel has the text that’s displayed when checked is false .

onIcon has the icon classes that’s applied when checked is true .

offIcon has the icon classes that’s applied when checked is false.

Checkbox with Three States

We can add a checkbox with an indeterminate state in addition to the checked and unchecked states with the TriStateCheckbox component.

To add it, we write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import TriStateCheckbox from 'primevue/tristatecheckbox';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("TriStateCheckbox", TriStateCheckbox);
app.mount("#app");

App.vue

<template>
  <div>
    <TriStateCheckbox v-model="value" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: false,
    };
  },
  methods: {},
};
</script>

to add it.

Buttons

PrimeVue comes with buttons in a variety of styles.

To add a basic button, we can write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Button from 'primevue/button';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Button", Button);
app.mount("#app");

App.vue

<template>
  <div>
    <Button label="Submit" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {};
  },
  methods: {},
};
</script>

The label prop is the text that’s displayed to the user.

We can add icons to the button by writing:

<template>
  <div>
    <Button label="Submit" icon="pi pi-check" iconPos="right" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {};
  },
  methods: {},
};
</script>

icon has the classes for the icon we want to add.

iconPos has the position of the icon.

Conclusion

We can add text areas, toggle buttons, 3 state check boxes, and buttons into our Vue 3 app with PrimeVue.

Categories
jQuery

jQuery — Deferred and Data

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.

.data()

We can store data within matched elements with the data method.

For example, we can write the following HTML:

<span></span>

Then use data to store and get the data as follows:

$("body").data("foo", 52);
$("span").first().text($("body").data("foo"));

We save the data with key 'foo' and value 52 in the first line.

Then in the 2nd line, we get the data and put it in the span .

.dblclick()

The .dblclick() method binds an event handler to the dblclick event or trigger the event on an element.

For example, if we have the following HTML:

<div id="target">
  Double-click here
</div>
<div id="other">
  Trigger the handler
</div>

Then we can listen to double clicks on the div with ID target by writing:

$("#target").dblclick(function() {
  alert("Handler for .dblclick() called.");
});

We can also trigger a double click on the div with ID target when we click on the div with ID other by writing:

$("#target").dblclick(function() {
  alert("Handler for .dblclick() called.");
});$("#other").click(function() {
  $("#target").dblclick();
});

deferred.always()

deferred.always() adds a callback to be called when the deferred object is resolved or rejected.

For example, we can write:

$.get("foo.php").always(function() {
  alert("$.get completed");
});

to call the alert when we make a GET request for foo.php .

deferred.catch()

The deferred.catch method adds a callback that’s called when the deferred object is rejected.

For example, we can write:

$.get("foo.php")
  .then(function() {
    alert("$.get succeeded");
  })
  .catch(function() {
    alert("$.get failed!");
  });

or:

(async () => {
  try {
    const res = await $.get("foo.php")
  } catch (error) {
    alert("$.get failed!");
  }
})()

$.get returns a thenable object so it works with async and await .

deferred.done()

The deferred.done() method lets us add a callback that’s called when the deferred object is resolved.

For example, we can write:

$.get("https://jsonplaceholder.typicode.com/todos/1")
  .done(function() {
    alert("$.get succeeded");
  });

to make a GET request https://jsonplaceholder.typicode.com/todos/1 and add run something after that.

deferred.fail()

The deferred.fail() method lets us add a callback that’s called when the deferred object is rejected.

For example, we can write:

$.get("test.php")
  .done(function() {
    alert("$.get succeeded");
  })
  .fail(function() {
    alert("$.get failed!");
  });

to add that.

deferred.pipe()

We can use the deferred.pipe() method to filter and chain deferred objects.

It returns another deferred object.

We can use it by writing:

const defer = $.Deferred(),
  filtered = defer.pipe(function(value) {
    return value * 2;
  });defer.resolve(5);
filtered.done(function(value) {
  alert(value);
});

We call defer.pipe with a callback that takes the resolved value from the defer object, multipole by it by 2, and return it in the callback.

Then we call defer.resolve(5) to pass 5 as the value of value in the pipe callback.

And we get the finalvalue in the done callback, which is 10.

We can chain deferred by writing:

const url = 'https://jsonplaceholder.typicode.com/posts/1';
const url2 = 'https://jsonplaceholder.typicode.com/posts/2';
const request = $.ajax(url, {
    dataType: "json"
  })
  .pipe(function(data) {
    console.log(data)
    return $.ajax(url2);
  })
  .done(function(data) {
    console.log(data)
  });

We call pipe on the first deferred object returned by $.ajax and then call done to get the data from the 2nd $.ajax call.

Conclusion

We can work with deferred objects with various jQuery methods.

Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Button Group Multiple Selections and Sliders

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Button Group Multiple Selections

We can add a button group with multiple selections.

For instance, we can write:

<template>
  <div>
    <SelectButton
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      multiple
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      cities: [
        { name: "London", code: "LND" },
        { name: "Paris", code: "PRS" },
        { name: "Rome", code: "RM" },
      ],
    };
  },
  methods: {},
};
</script>

We add the multiple prop to enable multiple selections.

Also, we can customize the items displayed by populating the option slot:

<template>
  <div>
    <SelectButton
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      multiple
    >
      <template #option="slotProps">
        <div>
          <b>{{ slotProps.option.name }}</b>
        </div>
      </template>
    </SelectButton>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      cities: [
        { name: "London", code: "LND" },
        { name: "Paris", code: "PRS" },
        { name: "Rome", code: "RM" },
      ],
    };
  },
  methods: {},
};
</script>

We get the option data from theslotProps.option slot.

Slider

PrimeVue comes with a slider input component.

For instance, we can write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Slider from 'primevue/slider';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Slider", Slider);
app.mount("#app");

App.vue

<template>
  <div>
    <Slider v-model="value" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: 0,
    };
  },
  methods: {},
};
</script>

We add the Slider component and we bind the selected value to the value reactive property with v-model .

Also, we can change it to a range slider with the range prop:

<template>
  <div>
    <Slider v-model="value" range />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: [20, 80],
    };
  },
  methods: {},
};
</script>

We can set the range slider to display vertically with the orientation prop:

<template>
  <div>
    <Slider v-model="value" orientation="vertical" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: 0,
    };
  },
  methods: {},
};
</script>

And we can snap the slider value to the nearest increment with the step prop:

<template>
  <div>
    <Slider v-model="value" :step="20" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: 0,
    };
  },
  methods: {},
};
</script>

Also, we can set the min and max values with the min and max props:

<template>
  <div>
    <Slider v-model="value" :step="20" :min="50" :max="200" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: 0,
    };
  },
  methods: {},
};
</script>

Conclusion

We can add button groups that allow multiple selections and sliders into our Vue 3 app with PrimeVue.

Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — Radio Button, Rating Input, and Select Button Group

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

Radio Button

We can add radio buttons into our Vue 3 app with PrimeVue’s RadioButton component.

To do this, we write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import RadioButton from 'primevue/radiobutton';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("RadioButton", RadioButton);
app.mount("#app");

App.vue

<template>
  <div>
    <div class="p-field-radiobutton">
      <RadioButton id="city" name="city" value="Los Angeles" v-model="city" />
      <label for="city">Los Angeles</label>
    </div>
    <div class="p-field-radiobutton">
      <RadioButton id="city2" name="city" value="New York" v-model="city" />
      <label for="city2">New York</label>
    </div>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      city: null,
    };
  },
  methods: {},
};
</script>

We register the RadioButton component in main.js

Then we add them into the App component.

We bind both to the same reactive property so that they are set to the value prop of the selected one.

The p-field-radiobutton class lets us add the radio button with the label beside it.

Rating Input

We can add a rating input into our Vue 3 app with the Rating component.

For instance, we can write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Rating from 'primevue/rating';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Rating", Rating);
app.mount("#app");

App.vue

<template>
  <div>
    <Rating v-model="val" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      val: null,
    };
  },
  methods: {},
};
</script>

We can change the number of stars displayed with the stars prop:

<template>
  <div>
    <Rating v-model="val" :stars="8" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      val: null,
    };
  },
  methods: {},
};
</script>

Also, we can remove the cancel icon that lets us reset the value by setting the cancel prop to false :

<template>
  <div>
    <Rating v-model="val" :stars="8" :cancel="false" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      val: null,
    };
  },
  methods: {},
};
</script>

Select Button Group

PrimeVue comes with the SelectButton component to let us add a button group.

We can click on a value to select it.

For instance, we can write:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import SelectButton from 'primevue/selectbutton';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("SelectButton", SelectButton);
app.mount("#app");

App.vue

<template>
  <div>
    <SelectButton v-model="selectedCity" :options="cities" optionLabel="name" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      cities: [
        { name: "London", code: "LND" },
        { name: "Paris", code: "PRS" },
        { name: "Rome", code: "RM" },
      ],
    };
  },
  methods: {},
};
</script>

We add the SelectButton component with the options prop to set the options for the button group.

And we bind the selected value to the selectedCity reactive property with the v-model directive.

optionLabel is set to the property name with the value we want to display to the user.

Conclusion

We can add radio buttons, rating inputs, and button groups into our Vue 3 app with PrimeVue.

Categories
PrimeVue

Vue 3 Development with the PrimeVue Framework — MultiSelect and Password Input

PrimeVue is a UI framework that’s compatible with Vue 3.

In this article, we’ll look at how to get started with developing Vue 3 apps with PrimeVue.

MultiSelect

PrimeVue comes with a multi select dropdown component to let us select multiple choices.

To add it, we can add the MultiSelect component:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import MultiSelect from 'primevue/multiselect';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("MultiSelect", MultiSelect);
app.mount("#app");

App.vue

<template>
  <div>
    <MultiSelect
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      cities: [
        { name: "Miami", code: "MI" },
        { name: "Rome", code: "RM" },
        { name: "London", code: "LDN" },
        { name: "Istanbul", code: "IST" },
        { name: "Paris", code: "PRS" },
      ],
    };
  },
  methods: {},
};
</script>

options lets us set the options to render.

optionLabel has the name of the property to render as choices.

The filter prop adds an input box to let users search for choices.

We can display selected choices as chips with the display='chip' prop:

<template>
  <div>
    <MultiSelect
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      display="chip"
    />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      cities: [
        { name: "Miami", code: "MI" },
        { name: "Rome", code: "RM" },
        { name: "London", code: "LDN" },
        { name: "Istanbul", code: "IST" },
        { name: "Paris", code: "PRS" },
      ],
    };
  },
  methods: {},
};
</script>

We can customize how the selected choices are displayed by populating the value slot.

And we can customize how each option is displayed by populating the option slot:

<template>
  <div>
    <MultiSelect
      v-model="selectedCity"
      :options="cities"
      optionLabel="name"
      display="chip"
    >
      <template #value="slotProps">
        <div v-for="option of slotProps.value" :key="option.brand">
          <span>{{ option.name }}</span>
        </div>
        <template v-if="!slotProps.value || slotProps.value.length === 0">
          Select Brands
        </template>
      </template>
      <template #option="slotProps">
        <div>
          <span>{{ slotProps.option.name }}</span>
        </div>
      </template>
    </MultiSelect>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      selectedCity: null,
      cities: [
        { name: "Miami", code: "MI" },
        { name: "Rome", code: "RM" },
        { name: "London", code: "LDN" },
        { name: "Istanbul", code: "IST" },
        { name: "Paris", code: "PRS" },
      ],
    };
  },
  methods: {},
};
</script>

We can get options from the slotProps of each slot.

Password Input

PrimeVue comes with a password input.

We can add it with the Password component:

main.js

import { createApp } from "vue";
import App from "./App.vue";
import PrimeVue from "primevue/config";
import Password from 'primevue/password';
import 'primevue/resources/primevue.min.css'
import 'primevue/resources/themes/saga-blue/theme.css'
import 'primeicons/primeicons.css'
import 'primeflex/primeflex.css';

const app = createApp(App);
app.use(PrimeVue);
app.component("Password", Password);
app.mount("#app");

App.vue

<template>
  <div>
    <Password v-model="value" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      value: null,
    };
  },
  methods: {},
};
</script>

It comes with a password strength meter.

A medium password has at least 1 lowercase character, 1 uppercase or number, and a minimum of 6 characters.

And a strong password has at least 1 lowercase character, at least 1 uppercase character, at least 1 numeric character, and a minimum of 8 characters.

Conclusion

We can add a multi select dropdown and password input into our Vue 3 app with the PrimeVue framework.