Categories
Quasar

Developing Vue Apps with the Quasar Library — Rating Control

Spread the love

Quasar is a popular Vue UI library for developing good looking Vue apps.

In this article, we’ll take a look at how to create Vue apps with the Quasar UI library.

Rating Control

We can add a rating control into our Vue app with the Quasarq-rating component.

For instance, we can write:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <q-rating v-model="ratingModel" size="1.5em" icon="thumb_up">
        </q-rating>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          ratingModel: 4.5
        }
      });
    </script>
  </body>
</html>

We bind the value to display to the ratingModel reactive property with v-model

size sets the size of the icon.

icon sets the name of the icon.

We can set the max rating score with the max prop:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <q-rating v-model="ratingModel" size="2em" :max="10" color="primary">
        </q-rating>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          ratingModel: 4.5
        }
      });
    </script>
  </body>
</html>

The icon for the rating display can also be an image:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <q-rating
          v-model="ratingModel"
          size="2em"
          color="primary"
          icon="img:https://cdn.quasar.dev/logo/svg/quasar-logo.svg"
        >
        </q-rating>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          ratingModel: 4.5
        }
      });
    </script>
  </body>
</html>

We can set an icon to display when the rating icon is selected with the icon-selected prop:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <q-rating
          v-model="ratingModel"
          size="3.5em"
          color="green-5"
          icon="star_border"
          icon-selected="star"
        >
        </q-rating>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          ratingModel: 4.5
        }
      });
    </script>
  </body>
</html>

We can also set each icon to a different one with the icon prop:

<!DOCTYPE html>
<html>
  <head>
    <link
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"
      rel="stylesheet"
      type="text/css"
    />
    <link
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"
      rel="stylesheet"
      type="text/css"
    />
  </head>
  <body class="body--dark">
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>
    <div id="q-app">
      <q-layout
        view="lHh Lpr lFf"
        container
        style="height: 100vh;"
        class="shadow-2 rounded-borders"
      >
        <q-rating
          v-model="ratingModel"
          size="3.5em"
          color="green-5"
          :icon="icons"
        >
        </q-rating>
      </q-layout>
    </div>
    <script>
      new Vue({
        el: "#q-app",
        data: {
          ratingModel: 4.5,
          icons: [
            "sentiment_very_dissatisfied",
            "sentiment_dissatisfied",
            "sentiment_satisfied",
            "sentiment_very_satisfied"
          ]
        }
      });
    </script>
  </body>
</html>

We set icon to an array of icon names.

Conclusion

We can add a rating control into our Vue app with Quasar.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *