How To

How to create Custom UI Component using knockout.js in Magento 2

Hello, Magento folks,

Last time, we talked about how you can remove disabled products from the cart page in Magento 2. Today we are going to learn about how to create custom UI components using knockout.js in Magento 2.

Knockout or KO is a library of JavaScript files. It is mostly used in the frontend part of the Magento 2 especially in checkout pages. With the introduction of this library, it has been made possible to create responsive frontend components like date picker, popup, and other UI components.

UI components are a large and complex topic in Magento 2. They are used to create elements like tables, buttons, tabs, dialogs, and more. Let me be more specific, a UI component is used to implement a part of the MVVM model. For today, I will let you know how we can create a custom UI Component template with the help of codes.

First, let’s look at the Flow:

Step 1: We will create one PHTML file

Step 2: Create custom component over there using “Magento_UI/js/core/app”

Step 3: Register New Component within Components

Step 4: Call out custom component template file and pass the variable.

Step 5: In js file, we will put the KO library for knockout js.

Step 6: Return function is compulsory, so we put it and put our logic out there

Step 7: Now, data will bind with our HTML tag with the help of the “data-bind” attribute.

Let us see an example.

1. First, create PHTML file at the below path,

app\code\Vendor\Extension\view\frontend\templates\uicomponent.phtml

Now use the below code:

<script type="text/x-magento-init">
    {
        "*":{
            "Magento_UI/js/core/app": {
                "components":{
                    "mycomponent":{
                        "component": "Vendor_Extension/js/uicomponentjs",
                        "modulename ": "<?= $block->escapeJs($block->getModuleName()); ?>"
                    }
                }
            }
        }
    }
</script>
<div data-bind="scope: 'mycomponent'">
    <div data-bind="text: title"></div>
</div>

2. Now, create js file at the below path,

app\code\Vendor\Extension\view\frontend\web\js\uicomponentjs.js

Use the below code,

define(['ko'],function (ko) {
    'use strict';
    return function (config) {
        return {
            title: ko.observable("Module Name will return"),
            getModulename : function () {
                return config;
            }
        }
    }
});

Using the above codes, you will successfully implement custom UI Components with your PHTML file. If you had any problem while implementing the codes, then contact us at our support portal. We will be happy to help you.

Lastly, if you liked this article, then let us know in the comments section below. Also, share it with your colleagues and friends.

Happy coding!

Click to rate this post!
[Total: 13 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

19 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago