0

I'm trying to make links inside <td> in the second column. When clicked, I want MonitoringTable function to be invoked with the value I supplied.

Vue.Js:

 const app = new Vue({
    el: '#page-wrapper',
    data: {
        loading: false,
        resultsTable: [],
        defaultTable: '*some name*',
        columnsFilter: ['*some name*','*some name*','*some name*']
    },
    methods: {
          /** Get Chosen Table Results From DB **/
          MonitoringTable: function(table) {
            var self = this;
            self.loading = true;

            /** Erase Previous Data From Table **/
            if ($.fn.DataTable.isDataTable('#monitoringTable')) {
                $('#monitoringTable').DataTable().destroy();
            }

            /** Get Data From DB **/
            Services.getMonitoringTable(table).then(function(response) {
                self.resultsTable = response.data;

                $('#monitoringTable thead tr').empty();
                $('#monitoringTable tbody').empty();

                if (self.resultsTable.length) {
                    /** Build Array of Table Heads **/
                    var columns = [];
                    $.each(self.resultsTable[0], function (index, value) {
                        columns.push({ data: index });
                        /** Append Filters to Table Heades **/
                        $('#monitoringTable thead tr').append(
                            '<th>' +
                            index +
                            ((self.columnsFilter.includes(index) )? '<select class="hidden"></select>' : '') +
                            '</th>');
                    });


                    /** Construct Table **/
                    setTimeout(function() {
                        Global.CustomTable('#monitoringTable', [ 0, 'asc' ], 25, false, response.data, columns, self);
                        $('#monitoringTable').find('[data-toggle="tooltip"]').tooltip('fixTitle');
                        if(table == self.defaultTable){
                            $.each(self.resultsTable, function (index, value) {
                                $('#monitoringTable tbody tr td.sorting_1').eq(index).next().html(
                                    `<a @click="MonitoringTable(` + *table's name* + `)" style="cursor: pointer;">` + *table's name* + '</a>');
                            });
                        }
                    }, 0);
                } else {
                    $('#monitoringTable thead tr').append('<th>Table results</th>');

                    /** Construct Table **/
                    setTimeout(function() {
                        Global.CustomTable('#monitoringTable');
                        $('#monitoringTable').find('[data-toggle="tooltip"]').tooltip('fixTitle');
                        self.loading = false;
                    }, 0);
                }
            }).catch(function(error) {
                var error = Object.assign({}, error);
                Global.ErrorMessages(error.response.data);
            });
        }
    },
    mounted: function() {
        var self = this;

        self.MonitoringTable(self.defaultTable);
        Global.ShowMounted();
    }
});

Element before mount:

@extends('*some name*')

@section('*some name*', '*some name*')
@section('*some name*', '*some name*')

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <loading v-if="loading"></loading>
            <table id="monitoringTable" class="table table-bordered dataTable" width="100%" cellspacing="0">
                <thead>
                    <tr></tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>
@stop

I added the HTML after table is constructed (with setTimeout). The view is as expected... what am I missing? How can I invoke MonitoringTable with a click?

5
  • Just tried it with a console.log instead of your code inside MonitoringTable, it worked. What isn't working for you? Commented Jun 21, 2020 at 13:54
  • I tried console.log at the first line of MonitoringTable but saw nothing in console.@Daniel_Knights Commented Jun 22, 2020 at 4:49
  • Must be somewhere else in your code then. Could you post the full file? Commented Jun 22, 2020 at 4:55
  • @Daniel_Knights that is the entire JS and HTML code Commented Jun 22, 2020 at 6:34
  • Something else must be wrong somewhere, there's no reason you'd see nothing in the console Commented Jun 22, 2020 at 6:43

1 Answer 1

1

You are mixing 2 paradigms - the Vue reactivity and the jQuery imperativity.

You can not use anything Vue-related in your jQuery-based code. You seem to think that @click will work in a string literal like <a @click which you are injecting into the DOM through jQuery - but it simply won't work because it is not part of any Vue-based template. You will have to either use an inline event handler (<a onclick="....") or use addEventListener to attach your handler after the A tag is already present in the DOM tree.

I highly recommend you to get rid of jQuery (and anything based on it) and switch completely to Vue.JS

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. so how can I access the vue instance from Js (because obviously just onclick="MonitoringTable('*tebleName*')" wouldn't work)
Well, you can assign the root Vue instance to a global variable, e.g. var myVue = new Vue({.....}).$mount('#app');

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.