1

I have the following code which I am using to populate a datatable. For each row in the datatable, I have an update link. clicking the update should call the ShowModal() method.

How can I call ShowModal() from this <a>?

   <template>
     <table id="buildings" class="mdl-data-table" width="100%"></table>
    </template>

    methods: {
        ShowModal(buildingId){
          // do something here...
        }, 
        listBuildings() {                      
         axios
            .get("https://localhost:44349/api/Building/List", headers)
            .then(response => {
              response.data.forEach(el => {            
                this.dataset.push([
                  el.buildingId,
                  el.projectName,
                  el.city,                 
                  `<a click='${this.ShowModal(el.buildingId)}'>Update</a>`  // I was trying this...        
               ]);

            $("#buildings").DataTable({
                  data: this.dataset});

           });
         }
6
  • how are you rendering that data table inside template? Commented Dec 28, 2018 at 16:19
  • I am using the above array (this.dataset) and use it as the datatable data. in the template I have the <table> tag Commented Dec 28, 2018 at 16:23
  • 1
    please provide that part from your template that contains table element Commented Dec 28, 2018 at 16:25
  • <table id="buildings" class="mdl-data-table" width="100%"></table> Commented Dec 28, 2018 at 16:26
  • 1
    in this way that will not work properly i recommend to use a data table built using vue js Commented Dec 28, 2018 at 16:33

1 Answer 1

1

I think what u are trying to do its not Vue way, but JQuery way. So let me propose to u more correct code (IMHO)

<template>
     <table id="buildings" class="mdl-data-table" width="100%">
        <tr
            v-for="(item, index) in dataset"
            :key=(index) // or building id if u like
        >
            <td>{{ item.buildingId }}</td>
            <td>{{ item.projectName }}</td>
            <td>{{ item.city }}</td>
            <td
                @click="showModal(item.buildingId)"
            >
            click me! A not required here. U can use div if u want to have not cell clicked and style it with css
            </td>
        </tr>
     </table>
</template>
    methods: {
        showModal(buildingId){
          // do something here...
        }, 
        listBuildings() {                      
         axios
            .get("https://localhost:44349/api/Building/List", headers)
            .then(response => {
                // I suppose u have as responce here array of objects?
                this.dataset = response.data
            })
            .catch(e => console.warn(e));
        }
Sign up to request clarification or add additional context in comments.

4 Comments

Also I'm supossing u are trying to use Jquery DataTable in vue. Please avoid to do so. There is I belive many packages for datatables in vue. But if you dying to use this, well read this datatables.net/forums/discussion/25111/hyperlink-in-td
But take also look here onewaytech.github.io/vue2-datatable/doc/#/en and here vuetable.com No need for JQuery in Vue
it's materialized datatable for vue.js
Where and how you initialize the table ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.