How to open a new page by clicking on the table row?

Asked

Viewed 638 times

1

I created a page that displays database data in a table, generated through the plugin dataTables (Jquery). It works as follows: when the user clicks on the table row, it is redirected to an edit page.

The line of code that does this redirect is:

$("#tabela-estacoes td").click(function() {
    window.location = "editar-estacoes-trabalho.php?id=" + $(this).parent("tr").attr('identifier');
});

My question is this: how do I stop instead of redirecting the page, open this data in a new tab?

3 answers

2

I do it this way:

  <button onclick="myFunction()">Abrir em outra aba</button>

  <script>
  function myFunction() {
    window.open('http://www.terra.com.br', '_newtab');
  }
  </script>

0

I’m not very good with Javascript but I usually use the following

<td onclick="location.href='$linkQueVcQuerCarregar'">CONTEUDO DA CELULAR</td>

It usually works as requested, but I’d still do a DRY function so it doesn’t get ugly, like this

$("#tabela-estacoes td").click(function() { window.open("editar-estacoes-trabalho.php?id=" + $(this).parent("tr").attr('identifier');", '_blank'); });

so would look beautiful and would be already applied to all <td> table #tabela-estacoes; I hope I’ve helped.

  • Hananiamizrahi, thanks for the reply. I use this code in my service and now I have no way to test... But only by changing the window.Location to Location.href the current page will remain displayed and the line data I selected will open in a new tab?

  • To open in a new page you exchange Location.href and boot window.open('LINK', '_Blank') ai funciona como vc quer.

  • I’ve done it this way and it didn’t work $("#table-stations td"). click(Function() { window.open = ("edit-stations-work.php? id=" +," _Blank") $(this). Parent("tr"). attr('Identifier');

  • Neither $("#table-stations td"). click(Function() { window.open = ("edit-stations-work.php? id=" +," _Blank");

  • Friend, the function window.open() requires 2 parameters, which are the first one you want to open, and the second one is the way this link will be opened, in the case _Blank... type like this -> window.open('http://meulink.com.br','_Blank');

  • I edited the answer above, try to see if it works, here it ran bacaninha! In addition, make sure that the jquery framework is being called...

Show 1 more comment

0


Try to do it this way:

$("#tabela-estacoes td").click(function() {
    var identifier = $(this).parent("tr").attr('identifier');
    var $form = $("<form></form>");
    $form.attr({
        action: "http://endereco/editar-estacoes-trabalho.php",
        target: 'blank'
    });

    $form.append('<input type="hidden" name="id" value="'+identifier+'">');

    $('body').append($form);
    $form.submit().remove();
});
  • 1

    Fernando, that’s almost it! I added your code and tested, opens a new page the way I need, mounts the page layout but does not display the line content that I clicked pq is not setting the id. The end of the link on the new page is: edit-posts-work.php? id= Do you have any idea what it might be??

  • I’m not sure what it might be, but I think it’s related to the construction of the URL by the browser, I modified the example, now passes the value of dentifier

  • It worked! That’s exactly what I needed!!! Thank you so much for your help, Fernando.

Browser other questions tagged

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