Leverage Vue.js to toggle the enable/disable functionality of a record within a

Just starting out with vue js and looking for some guidance.

I have a blade view that displays a list of modules, each with a button to enable or disable the module using an ajax call.

I want to achieve this functionality using VUE JS.

Here is my current setup:

<div class="content">
   <table id="moduleTable">
      <tr>
         <th>Module Name</th>
         <th>Status</th>
         <th>Update</th>
      </tr>
      @foreach($modules as $module)
      <tr>
         <td >{{$module->name}}</td>
         @if($module->enabled())
         <td>Enabled</td>
         <td><button @click="toggleModule('{{$module->name}}')" >Disable</button></td>
         @else
         <td>Disabled</td>
         <td><button @click="toggleModule('{{$module->name}}')">Enable</button></td>
         @endif
      </tr>
      @endforeach
   </table>
</div>

Below is the js code I have so far:

var buttons = new Vue({
    el: '#moduleTable',
    data:  {}
    ,
    methods: {
        toggleModule: function (moduleName) {
                console.log(moduleName);
        }
    }
});

Now I'm unsure about what steps to take next (I do understand the axios part for making the call).

I would like to change the text of the enabling/disabling button when clicked. How can I reference the specific button that was clicked?

Also, am I passing the module value correctly to the click event or is there a better way to handle this?

Answer №1

Creating a Vue component named '<module>', you can then utilize the directive and combine it with blade foreach to generate a '<module>' component for each identified module using a v-key.

For example:

@foreach($modules as $module)
  <module v-key="{{$module->id}}" name="{{$module->name}}" is-enabled="{{$module->enabled()}}"> 
@endforeach

Inside the '<module>' component, you can use either v-if or v-show to enable or disable each individual component.

For more information on Conditional Rendering, refer to the documentation at: https://v2.vuejs.org/v2/guide/conditional.html

Answer №2

Here is a helpful solution:

<div class="content">
   <table id="moduleTable">
      <tr>
         <th>Module Name</th>
         <th>Status</th>
         <th>Update</th>
      </tr>
      <tr v-for="(module,index) in modules">
         <td >@{{module.name}}</td>
         <td><span v-if="module.enabled"> Enabled
         <span v-else>Disabled</span></td>
         <td><span v-if="module.enabled"><button @click="toggleModule(module.name,index)">Disable</button></span>
            <span v-else><button @click="toggleModule(module.name,index)">Disable</button></span>
         </td>
      </tr>
   </table>
</div>

var buttons = new Vue({
    el: '#moduleTable',
    data:  {
        modules:{{ $modules}}
    },
    methods: {
        toggleModule: function (moduleName,index) {
            var self = this;
               axios.post('url',{'name':moduleName})
               .then(response){
                   self.modules[index].enabled = false; // false true whatever
               }
        }
    }
});

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

The act of composing a message for the alert

Can you provide some assistance with this particular issue? I am attempting to input text into a prompt alert, however, my current method involving switch_to.alert and send_keys is not working as intended. base_URL = "https://www.seleniumeasy.com/tes ...

Vanilla JS solution for closing dropdown button

Is there a way to close the dropdown button using Vanilla JS? I have successfully implemented a method to open the dropdown button, but am struggling to find a way to close it. I would like to maintain the for loop for functionality but need guidance on ho ...

Do we really need Renderer2 in Angular?

Angular utilizes the Renderer2 class to manipulate our view, acting as a protective shield between Angular and the DOM, making it possible for us to modify elements without directly interacting with the DOM ourselves. ElementRef provides another way to al ...

Adding personalized local JavaScript files to Gatsby the reliable way(Note: I have made the

As a newcomer to React and Gatsby, I am currently working on a small project for practice and encountering an issue. I am trying to add a custom JS file to the project with functions for a calculator on the index page. I used Helmet to import them, and it ...

Is there a PostgreSQL Node.js server equivalent to the each() function in SQLite3?

Currently, I am in the process of transitioning my Node.js application's database from SQLite3 to PostgreSQL. Originally, I initialized my SQLite3 database like this: let db = new sqlite3.Database('./dbName.db', (err) => { if (err) { ...

Accessing HTML DOM Element ID from outside a for loop in JavaScript

Is there a way to create individual checkboxes for each element in data.contents, set their IDs as the respective titles, and trigger a specific function when clicked without running into the "b is not defined" error? So far, my approach involves using cre ...

What sets the statements of ( for... in ) and ( for... of ) apart from each other?

Although I am familiar with the for... in loop (which iterates over keys), I recently came across the for... of loop for the first time, which apparently iterates over values. I find myself confused by this for... of loop. var arr = [3, 5, 7]; arr.foo = & ...

What is the best way to retrieve parameter values while using NavigationActions.reset?

Main.js const AppStack = StackNavigator({ Home: { screen: HomeScreen}, Profile: { screen: ProfileScreen}, Settings: { screen: SettingsScreen } }) Login.js const resetHandler = NavigationActions.reset({ index: 0, actions: [ NavigationAction ...

Building with Nuxt is dragging on for over an hour

I'm currently working on a Nuxt project and have been looking into potential solutions, but haven't come across a definitive answer yet. Step 6/8 : EXPOSE 8080 ---> Running in e5d36d6e86fe Removing intermediate container e5d36d6e86 ...

Revise the background-image to a different one that is generated dynamically through an ajax script request

Is there a way to use the GD function ImagePng without storing the image, but instead using it as a background-image property on an element? I have set up an ajax call to a php script that generates the image, and I want to use the response from this call ...

How to set up Jest in your JavaScript project using npm

I have been attempting to install jest using npm, both locally and globally, but it fails to install. My npm version is 7.5.2 and my node version is 12.22.5. I am using ubuntu. I have tried the following commands: npm i -D jest npm i -g jest npm i jest ...

"Troubleshooting a 404 Error with AngularJS PUT Method

I encountered a 404 error when using AngularJS's "$http.put" for a PUT request. Below is my relevant AngularJS code: /* toggles whether or not the link is disabled */ toggleLinkStatus: function (isActive, linkGUID) { $http.put(' ...

Tips for adding the same HTML element repeatedly

I've encountered an issue with the event I added to my app (clicking & entering). The main objective of this event is to add an input value each time the user interacts with it. Initially, everything works as expected but the code seems to malfunction ...

Filter arrays in Vue.js using checkboxes

I'm currently working with vuejs and I need to implement a filtering feature for my array using checkboxes. I attempted to use v-model to filter the array based on three specific options: "Truck," "Van," or "Tx". However, I haven't been successfu ...

Keep sending ajax request until the page is closed

Is it possible to resend an ajax request continuously using pure javascript without any framework? Here is an example of how I am currently attempting to do it: xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","demo_post2.asp",true); xmlhttp.setRequest ...

Having trouble with Ajax parsing the data accurately?

Looks like I've made a mistake somewhere. I was experimenting with parsing data from JavaScript to PHP using AJAX and everything seemed fine, but for some reason, the data isn't getting sent through to update my database. All I get is an empty d ...

Is there a way to automatically populate the default country, state, and city in the dependent dropdown when the page loads?

I am working on an ASP.NET MVC4 application where I have 3 dependent dropdowns: country, state, and city. I would like to set the default values for these dropdowns to USA, Pennsylvania, and Bethlehem respectively during the page load. These values are b ...

The functionality of $(window).load doesn't appear to be functioning correctly in Firefox

I have a piece of code that needs to run only after the website content is fully loaded, so I have placed it inside the window load function like this: $(window).load(function() { //some stuff happens }); This code works perfectly in Safari and Chrom ...

What is the best way to retrieve all objects from this data model?

I am looking to collect all the Model Objects from the data structure provided below. const PRODUCTS = [ { brand: 'Audi', allSeries: { serie: 'A3', allModels: [ { model: ' ...

When using Object.getOwnPropertyNames(window), the setTimeout function is not included in the resulting array

The issue at hand is that when using Object.getOwnPropertyNames(window), the setTimeout value is not included in the returned array. Why is this happening? In an attempt to further investigate, I tried checking the property descriptor object of the setTi ...