How to create routes in tables with Vuejs?

Asked

Viewed 584 times

0

My page is working perfectly, it today behaves as follows;

The user clicks on the textSearch to perform a search, and the table will only load the search requested by the user.

Look at my page!

Listing Records

Exactly what I need?

For example, if the user type Park will appear this result below;

inserir a descrição da imagem aqui

I wanted to put a rotate to a URL linked to this record, it could be www.google.com, but the problem that this record is not fixed in the HTML code it is coming from a Data array in json as you can see below;

{   
                endereco: 'R. Gen. de Divisão Pedro Rodrigues da Silva, 400, Barueri',
                localizacao: 'Parque shopping Barueri',
                celular: '(11) 9.8521-2560',
                email: '[email protected]'
       },

I just need a hint to start trying to implement, accept suggestions!

  • I’m not sure I understand the question, that?

  • i made a video to explain my question: https://www.youtube.com/watch?v=JO_-H1kY0Ho&t=4s

  • 1

    It’s gonna take me a while to get the video, back from work at 7:00

  • quiet, I am very grateful for your help.

1 answer

2


You can add a link inside the td and make a bind with the url

The line where the link is would look like this:

<td>
    <a :href="'http://www.google.com/?search&q=' + bancodedados.localizacao">Buscar</a>
</td>

The important part of this code is the use of :, with it you can pass information dynamically for the element, in the example, I passed the string http://www.google.com/?search&q= concatenated with the location

var app = new Vue({
    el: '#app',
    data: {
        bancodedados: [{
                endereco: 'Rua Pamplona, 1704 - 1º Andar - Loja 1.15, Jardim Paulista - São Paulo',
                localizacao: 'Jardim Pamplona Shopping',
                celular: '(11) 94581-8286',
                email: '[email protected]'
            },
            {
                endereco: 'Rua Pamplona, 1704 - 1º Andar - Loja 1.15, Jardim Paulista - São Paulo',
                localizacao: 'Bourbon Shopping',
                celular: '(11) 94581-8286',
                email: '[email protected]'
            },
            {
                endereco: 'R. Gen. de Divisão Pedro Rodrigues da Silva, 400, Barueri',
                localizacao: 'Parque shopping Barueri',
                celular: '(11) 9.8521-2560',
                email: '[email protected]'
            }, {
                endereco: 'Praça dos Cravos, 34, Barueri',
                localizacao: '',
                celular: '(11) 9.7518-2515',
                email: '[email protected]'
            }, {
                endereco: 'Avenida Leão Machado, 100, Osasco',
                localizacao: 'Continental Shopping',
                celular: '(11) 3768-0386',
                email: '[email protected]'
            }, {
                endereco: 'Av. dos Autonomistas, 1400 – loja 217, Osasco',
                localizacao: 'Shopping União',
                celular: '(11) 9.7026-5165',
                email: '[email protected]'
            }, {
                endereco: 'Av. Autonomistas, 1.828, Osasco',
                localizacao: 'Super Shopping Osasco',
                celular: '(11) 9.4388-0000',
                email: '[email protected]'
            }, {
                endereco: 'Rodovia Raposo Tavares, 23, Cotia',
                localizacao: 'Shopping Granja Vianna',
                celular: '(11) 9.9477-0752',
                email: '[email protected]'
            }, {
                endereco: 'Rua Jacy Teixeira de Camargo, 940, Campinas',
                localizacao: 'Campinas Shopping',
                celular: '19) 9.9553-7908',
                email: '[email protected]'
            }, {
                endereco: 'Rod. Dom Pedro I, 131.5 - Jardim Nilópolis, Campinas',
                localizacao: 'Galeria Campinas',
                celular: '(19) 9.8199-7751',
                email: '[email protected]'
            }
        ],
        MySearch: ''

    },

    methods: {

    },
    computed: {
        filteredBancodedaos() {
            return this.bancodedados
                .filter((bancoDeDado) => {
                    return (
                        bancoDeDado.endereco.match(this.MySearch) ||
                        bancoDeDado.localizacao.match(this.MySearch) ||
                        bancoDeDado.celular.match(this.MySearch) ||
                        bancoDeDado.email.match(this.MySearch)
                    )
                })
        }
    }
});
.table td, .table th {
  font-size: 12px;
}


#contato{
	width: 115px;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8" />
    <title>Star War</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>
</head>

<body>
    <div class="container" id="app">
        <div class="row"></div>
        <div class="well">
            <input type="search" v-model="MySearch" class="form-control" placeholder="Digite a sua busca" />
        </div>
        <div class="row">
            <table class="table">
                <thead>
                    <tr>
                        <th>Localização</th>
                        <th id="contato">Contato</th>
                        <th>E-mail</th>
                        <th>Endereço</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-if="MySearch" v-for="bancodedado in filteredBancodedaos">
                        <td>{{ bancodedado.localizacao }}</td>
                        <td>{{ bancodedado.celular }}</td>
                        <td>{{ bancodedado.email }}</td>
                        <td>{{ bancodedado.endereco }}</td>
                        <td>
                            <a :href="'http://www.google.com/?search&q=' + bancodedados.localizacao">Buscar</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>

</html>

Or you can run a method that opens the link by clicking on the line:

var app = new Vue({
    el: '#app',
    data: {
        bancodedados: [{
                endereco: 'Rua Pamplona, 1704 - 1º Andar - Loja 1.15, Jardim Paulista - São Paulo',
                localizacao: 'Jardim Pamplona Shopping',
                celular: '(11) 94581-8286',
                email: '[email protected]'
            },
            {
                endereco: 'Rua Pamplona, 1704 - 1º Andar - Loja 1.15, Jardim Paulista - São Paulo',
                localizacao: 'Bourbon Shopping',
                celular: '(11) 94581-8286',
                email: '[email protected]'
            },
            {
                endereco: 'R. Gen. de Divisão Pedro Rodrigues da Silva, 400, Barueri',
                localizacao: 'Parque shopping Barueri',
                celular: '(11) 9.8521-2560',
                email: '[email protected]'
            }, {
                endereco: 'Praça dos Cravos, 34, Barueri',
                localizacao: '',
                celular: '(11) 9.7518-2515',
                email: '[email protected]'
            }, {
                endereco: 'Avenida Leão Machado, 100, Osasco',
                localizacao: 'Continental Shopping',
                celular: '(11) 3768-0386',
                email: '[email protected]'
            }, {
                endereco: 'Av. dos Autonomistas, 1400 – loja 217, Osasco',
                localizacao: 'Shopping União',
                celular: '(11) 9.7026-5165',
                email: '[email protected]'
            }, {
                endereco: 'Av. Autonomistas, 1.828, Osasco',
                localizacao: 'Super Shopping Osasco',
                celular: '(11) 9.4388-0000',
                email: '[email protected]'
            }, {
                endereco: 'Rodovia Raposo Tavares, 23, Cotia',
                localizacao: 'Shopping Granja Vianna',
                celular: '(11) 9.9477-0752',
                email: '[email protected]'
            }, {
                endereco: 'Rua Jacy Teixeira de Camargo, 940, Campinas',
                localizacao: 'Campinas Shopping',
                celular: '19) 9.9553-7908',
                email: '[email protected]'
            }, {
                endereco: 'Rod. Dom Pedro I, 131.5 - Jardim Nilópolis, Campinas',
                localizacao: 'Galeria Campinas',
                celular: '(19) 9.8199-7751',
                email: '[email protected]'
            }
        ],
        MySearch: ''

    },

    methods: {
      showLink: function(bancodedado) {
        alert('abrindo: http://www.google.com/?q=' + bancodedado.localizacao);
        //Comentei essa parte por quê não funciona no snippet
        //window.open('http://www.google.com/?q=' + bancodedado.localizacao);
      }
    },
    computed: {
        filteredBancodedaos() {
            return this.bancodedados
                .filter((bancoDeDado) => {
                    return (
                        bancoDeDado.endereco.match(this.MySearch) ||
                        bancoDeDado.localizacao.match(this.MySearch) ||
                        bancoDeDado.celular.match(this.MySearch) ||
                        bancoDeDado.email.match(this.MySearch)
                    )
                })
        }
    }
});
.table td, .table th {
  font-size: 12px;
}


#contato{
	width: 115px;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8" />
    <title>Star War</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>
</head>

<body>
    <div class="container" id="app">
        <div class="row"></div>
        <div class="well">
            <input type="search" v-model="MySearch" class="form-control" placeholder="Digite a sua busca" />
        </div>
        <div class="row">
            <table class="table">
                <thead>
                    <tr>
                        <th>Localização</th>
                        <th id="contato">Contato</th>
                        <th>E-mail</th>
                        <th>Endereço</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-if="MySearch" v-for="bancodedado in filteredBancodedaos" v-on:click="showLink(bancodedado)">
                        <td>{{ bancodedado.localizacao }}</td>
                        <td>{{ bancodedado.celular }}</td>
                        <td>{{ bancodedado.email }}</td>
                        <td>{{ bancodedado.endereco }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>

</html>

The key points in this case are the method definition:

showLink: function(bancodedado) {
    //Aqui você abre uma nova janela com o link
    window.open('http://www.google.com/?q=' + bancodedado.localizacao);
}

And the method assignment in the click event tr:

v-on:click="showLink(bancodedado)"
  • 1

    You came close to the answer, but with the condition you showed I can already manage to at least try to do alone what I want, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.