Right menu in each Row of a table

Asked

Viewed 440 times

4

I have a table and am trying to create an action menu by right clicking on each Row of the table.

It’s partially working the problem that I can’t make the script select the context menu for each row it’s always picking up the last one.

I created a fiddle to facilitate the testing of the script and I will post the code below as well:

<style type="text/css">
    .skills {
        background: #A6A6A5;
        padding: 20px;
    }
</style>

<script type="text/javascript">
    $(document).ready(function () {
        $("table").contextmenu({
            delegate: "tbody tr",
            menu: ".table tbody tr .skills:first-child"
        });
    });
</script>

<h1>Shao Kan Game World</h1>

<!-- Os dados na tabela são ficticios e nao tem intencao de insultar os personagens -->

<table class="table table-bordered table-hover table-striped" style="width: 500px; margin: 0 auto;">
    <thead>
        <tr>
            <th>Personagem</th>
            <th>Poder</th>
            <th>Actions</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td>Luke</td>
            <td>Espadinha</td>
            <td class="skills">
                <ul class="skills" style="display: none; z-index: 4000;">
                    <li>SABRE DE LUZ</li>
                </ul>
            </td>
        </tr>

        <tr>
            <td>Robocop</td>
            <td>Metralhadora</td>
            <td class="skills">
                <ul class="skills" style="display: none; z-index: 4000;">
                    <li>BAZUCAAAA</li>
                </ul>
            </td>
        </tr>
    </tbody>
</table>

What I need is to click on the Robocop it display the UL containing Bazuca and when clicking on the Luke he display the Sabre de Luz.

  • 1

    vlw by @brasofilo edition!

4 answers

4


Using the example from @Peoplee, I adapted to automate the process so:

$('td.ativar').each(function(){
    $(this).contextmenu({
        menu: $(this).next().next().children('ul') // podemos usar jQuery aqui
    });
});

I put the class ativar us <td>'s Luke and Robocop, and the menu will be set via a jQuery object using the <ul> son of the second next(td).

Obs.: the class skills is being used in the <td> and in the <ul> and this seems problematic. I removed from <td> in my case.

$('td.ativar').each(function(){
  $(this).contextmenu({
    menu: $(this).next().next().children('ul')
  });
});
.skills {
    background: #A6A600;
    padding: 20px;
}
<script type='text/javascript' src='//code.jquery.com/jquery-2.1.0.js'></script>
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">  
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script type='text/javascript' src="https://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script>
<script type='text/javascript' src="http://wwwendt.de/tech/demo/jquery-contextmenu/jquery.ui-contextmenu.js"></script>
    
  <h1>Shao Kan Game World</h1>

<table class="table table-bordered table-hover table-striped" style="width: 500px; margin: 0 auto;">
    <thead>
        <tr>
            <th>Personagem</th>
            <th>Poder</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class='ativar'>Luke</td>
            <td>Espadinha</td>
            <td>
                <ul class="skills" style="display: none; z-index: 4000;">
                    <li>SABRE DE LUZ</li>
                </ul>
            </td>
        </tr>
        <tr>
            <td class='ativar'>Robocop</td>
            <td>Metralhadora</td>
            <td>
                <ul class="skills" style="display: none; z-index: 4000;">
                    <li>BAZUCAAAA</li>
                </ul>
            </td>
        </tr>
    </tbody>
</table>

  • Thanks for the help!

  • Could put $(this).parent().find('ul.skills'); to be more automated as more columns are added (td) it will not be necessary to update the Javascript.

3

2

Improving @brasofilo’s response, the process does not need additional classes beyond the class .table-context in the table (answer Peoplee) and by row, always taking the last column as context menu (answer Brasofilo).

$(".table-context tbody tr").each(function() {
    var element = $(this);

    element.contextmenu({
       menu: element.children("td:last").children("ul")
    });
});
  • Perfect, I knew there was a way to simplify :)

-1

Simply remove :first-child in menu:

$(document).ready(function () {
    $("table").contextmenu({
        delegate: "tbody tr",
        menu: ".table tbody tr .skills"
    });
});

Jsfiddle updated

  • 1

    Not sure, it’s 1 on the other already tried too!

Browser other questions tagged

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