Obtain the total sum and count of values stored in a VueJS checkbox data attribute

 <tr v-for='(women, index) in womenServices' :key="index">
                        <td class="text-center" style="width: 30px;">
                            <div class="custom-control custom-checkbox d-inline-block">
                                <input type="checkbox" class="custom-control-input"
                                       :id="'chk_wm' + index"
                                       :name="'chk_wm' + index"
                                       :data-price="women.price"
                                       :value="women.id" v-model="womenPackageServices">
                                <label class="custom-control-label" :for="'chk_wm' + index"></label>
                            </div>
                        </td>
                        <td>
                            !{women.title}!
                        </td>
                        <td>
                            !{women.price}!
                        </td>
                    </tr>
<input type="text" class="form-control" :v-model="packageServicesSum" placeholder="Total Price" readonly>
const app = new Vue({
            el: '#addVueServices',
            delimiters: ['!{', '}!'],
            data: {
                womenPackageServices: [],
                packageServicesSum: 0,
            },
            methods: {
                 
            }
            computed: {
                
            },
        })

v-model is functioning perfectly, successfully obtaining ids and updating womenPackagesServices object

However, an issue arises

I am aiming to retrieve the data attribute value to display the total SUM of Price and Count of selected Services.

If there is a simpler method to calculate the total price, please assist me as I intend to maintain womenPackageServices as this array will be sent to the database to save the selected checkbox ids.

I am not utilizing any components and do not wish to do so.

Answer №1

In order to tackle this issue without using standalone or component-based VUEJS, I opted to utilize jQuery methods. This allowed me to find a more efficient way to reach my desired outcome, even though it may have been a longer process if I had approached it solely with VUEJS.

Here is a concise solution to the problem at hand:

chkService: function (e) {
    const vue_ = this;
    vue_.packages.selectedServicesSum = 0;
    $(".packageServiceChk:checked").each(function () {
        var i = $(this).data('price')
        vue_.packages.selectedServicesSum += i;
    });
},

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Component re-rendering and initializing useReducer

I made some revisions to this post. Initially, I shared the entire problem with my architecture and later updated it to focus directly on the issue at hand in order to make it easier for the community to provide assistance. You can now jump straight to the ...

Verify changes in the Cross Domain RSS feed by utilizing Ajax technology

I have a website where I am trying to automatically reload an RSS news feed from every 60 seconds if it has been updated. I attempted to use Ajax for this purpose, but I seem to be facing some issues: <script type="text/javascript" src="../js/jquery.a ...

Experiment with utilizing dynamic imports within the useEffect hook

I've been working on testing a custom hook I developed that involves dynamically importing @vimeo/player. The key snippet from the useVideoPlayer hook is as follows: useEffect(() => { async function setup() { const { default ...

Do closures truly behave like objects, or are they something else entirely? (It appears not)

Let me clarify right off the bat that I am not inquiring about how closures function, as I already have a grasp on that concept. What I am curious about is what data type should be assigned to closures. Essentially, a closure can be thought of as a record ...

Switch the style sheets after the content

How can I create a toggle effect on the :after property, switching between + and - each time the link is clicked? I've attempted to achieve this using JavaScript, CSS, and HTML, but the code only changes + to - and doesn't switch back when the l ...

The issue persists with react-hook-form and Material UI RadioGroup as the selected value remains null

Having trouble with RadioGroup from Material UI when using react-hook-form Controller. I keep getting a null selected value, even though my code seems right. Can you help me figure out what's missing? import * as React from "react"; import { ...

Discovering if the array data is already present in Angular can be accomplished by using specific methods

Here is the snippet of code: data = [ { 'id': 'asdjxv', 'username': 'emma', }, { 'id': 'asqweja', 'username': 'adam', }, { ...

Can sweetalert2 be used as a tooltip?

I have a query, is it feasible to include a tooltip in the alert message? Alternatively, could there be another tooltip option available? Swal.fire({ title: '<strong>An example with HTML tags</strong>', icon: 'info', ...

The CORS middleware seems to be ineffective when used in the context of Node.js

I have set up my REST API on a Raspberry Pi server and connected it to the public using localtunnel. I am trying to access this API from a localhost Node.js application. The Node application is running on Express and includes some static JS files. However, ...

Prevent Nuxt from refreshing the page when the URL is modified

Currently, I am in the process of transitioning an application to Nuxt3 and encountering an issue regarding page reloading when the URL changes. Within my application, I have multiple links that all lead to the same page. To address this issue, I had to ma ...

Is there a restriction on the number of routes available from Google Maps Directions Service?

Utilizing the Google Maps Directions API, I am able to calculate the distance between the current user's location and various stores. Everything functions properly until I exceed 10 store locations, at which point the API ceases to operate due to its ...

Tips for utilizing parent index as a parameter in a nested *ngFor loop

Having trouble passing my parent index to a nested ngFor in order to access an array of names like "name1, name2, etc." <ion-item *ngFor="let b of banca ; let i = index"> <ion-select> <ion-select-option *ngFor="let n ...

I'm currently troubleshooting the code for the Gallery project. The gallery is supposed to have 4x4 grids, but for some reason, the grids are

It seems like I am struggling to identify the exact issue. The display on mobile looks fine but not on desktop. I attempted to tweak the isotope configuration without success. Even manipulating the server-side code didn't reveal any obvious problems. ...

Encountered an issue when attempting to send data using this.http.post in Angular from the client's perspective

Attempting to transfer data to a MySQL database using Angular on the client-side and Express JS on the server-side. The post function on the server side works when tested with Postman. Here is the code snippet: app.use(bodyParser.json()); app.use(bodyPa ...

What is the most effective method for generating dial images through programming?

Currently, I am in the process of creating a website that makes use of variously sized and styled dials to indicate progress. The filled portion of the dial represents how close an item is to being 100% complete. I am seeking a universal solution that wil ...

What is the best way to loop through a deeply nested JSON array structure?

I'm grappling with the challenge of iterating through a nested JSON array in my code. Here's what I have so far: <script src="https://unpkg.com/vue"></script> <div id="app"> <div v-for="agreg in mydata.aggregated"> ...

Connecting to the grunt server is currently not possible

const config = require('path/to/config'); module.exports = function(grunt) { // Project configuration. grunt.initConfig({ server: { port: config.port, base: config.base } }); }; C:\Program Files&bsol ...

AngularJS $http get isn't functioning properly, but surprisingly $.ajax works perfectly

Recently delving into the world of AngularJS, I am facing a hurdle with $http functionality. In my factory setup below: app.factory('employeeFactory', function ($http) { var factory = {}; // Retrieving data from controller var emplo ...

After successfully executing an AJAX request three times, it encountered a failure

I have implemented a script to send instant messages to my database asynchronously. Here is the code: function sendMessage(content, thread_id, ghost_id) { var url = "ajax_submit_message.php"; var data = { content: content, thread_id: thread_id }; ...

Tips for utilizing the beforeEach feature in node-tap?

Could someone please demonstrate how to utilize the beforeEach function? For more information, visit: . I am particularly interested in seeing an example using promises, although a callback version would also be appreciated. Below is a successfully functi ...