Showing Data Attributes from Selected Radio Buttons with Knockout.js
As a developer new to Knockout.js, you may find it challenging to work with this JavaScript library compared to jQuery. However, once you understand its core concepts, you can leverage its powerful features for dynamic data binding and view-model management.
In your Magento 2 shop scenario, you want to display summarized data from the selected shipping method using Knockout.js. You have already implemented the checked binding for setting the shipping method to the quote. Now, you need to access custom data attributes of the selected radio button elements (e.g., Shipping Name, Shipping Timerange) and display them in a “Summarize” box.
Accessing Custom Data Attributes
To achieve this, you can create an observable object that holds the selected shipping method’s properties. When a new shipping method is selected, update the observable object with the corresponding data attributes using the event.target property.
// Create an observable object for selected shipping method
let selectedMethod = ko.observable({ name: '', timerange: '', price: 0 });
// Register click event on radio buttons
$('.radio').click(function (e) {
// Update selectedMethod with data attributes of clicked element
selectedMethod({
name: $(this).attr('data-shipping-name'),
timerange: $(this).attr('data-timerange'),
price: $(this).attr('data-price')
});
});
Displaying Summarized Data
Now that you have the selectedMethod observable object, you can use it to bind data attributes in your summary box. Update the HTML structure and add the necessary bindings:
<div id="selected-method-summary">
<span class="method-name" data-bind="text: selectedMethod.name"></span>
<span class="method-timerange" data-bind="text: selectedMethod.timerange"></span>
<span class="method-price" data-bind="text: selectedMethod.price"></span>
</div>
Extending the isSelected Computed Observable
To avoid destroying Magento functionality while extending the isSelected computed observable, you can create a new computed observable that checks if the current shipping method matches the selected one. Update your JavaScript code accordingly:
// Create a new computed observable for selectedMethod
let isSelected = ko.computed(function () {
const quoteShippingMethod = quote.shippingMethod();
return (
quoteShippingMethod &&
selectedMethod().name === quoteShippingMethod.method_title &&
selectedMethod().timerange === quoteShippingMethod.extension_attributes.time_range
);
});
Conclusion
By following the steps outlined above, you can successfully show data attributes from selected radio buttons using Knockout.js in your Magento 2 shop. Create an observable object for selected shipping method properties and update it on click events. Bind these properties to display summarized data in a summary box.
Remember that understanding the core concepts of Knockout.js is essential for working with view models, observers, and computed observables effectively. With practice, you will find it easier to work with this powerful library compared to jQuery.
For more tips on working with Magento 2 development, consider exploring our blog posts at LaravelCompany.com. We offer practical guides on the latest technologies and best practices in e-commerce development.