内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - I have a button in each row of a table: How can the OnClick handler tell which row it was called from? - Stack Over
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I have a button in each row of a table: How can the OnClick handler tell which row it was called from? - Stack Over

programmeradmin0浏览0评论

The goal: ,,

So I've got a Table (Which is initialized as a JQuery DataTable). Each row contains a 'remove me' button, and when that button is pressed, I want to delete the row from the current table.

What I've tried:

tr = $(this).closest('tr');
$('.my-table-class').dataTable().fnDeleteRow(tr);

What happens

No matter what row I click on, the last row is deleted from the table is deleted, except if there's only one row in the table, in this situation a javascript error: "TypeError: j is undefined" is thrown from Jquery.dataTable.min.js. Both baffle me.

I can get the attributes of the right row - for example, If do something like: alert($(this).attr("data-name")); I click on John Smith's row, I'll see 'John Smith' in an alert box... so $(this) is the a tag, so why doesn't the .closest() method grab the right trtag?

My Questions:

  • How do I get 'this' row (the one which contained the button which was pressed) in order to delete it?

  • Any idea what's causing the 'TypeError: j is undefined" error when there's only one row in the table?

Details:

Here's the rendered (from .jsp) HTML table:

<table class="table my-table-class">
<thead><tr><th>Name</th><th> </th></tr></thead> 
<tbody>
        <tr>
            <td>John Smith</td>
            <td><a href="javascript://" class="my-button-class" data-name="John Smith"><i class="icon-plus"></i></a></td>
        </tr>

        <tr>
            <td>Robert Paulson</td>
            <td><a href="javascript://" class="my-button-class" data-name="Robert Paulson"><i class="icon-plus"></i></a></td>
        </tr>

        <tr>
            <td>Juan Sanchez</td>
            <td><a href="javascript://" class="my-button-class" data-name="Juan Sanchez"><i class="icon-plus"></i></a></td>
        </tr>
</tbody>

Here's how I initialize the tables as a Jquery DataTable:

$('.st-my-table-class').dataTable( {
            "bInfo": true, 
            "aaSorting": [[ 0, "desc" ]], // sort 1st column 
            "bFilter": true, // allow search bar
            "bPaginate": false,  // no pagination 
            "sDom": '<"top"f>rt<"bottom"lp><"clear">' // (f)ilter bar on top, page (i)nfo omitted
        } );

And here's the whole event handler:

$('.my-button-class').on("click", function(){ 
tr = $(this).closest('tr'); 
$('.my-table-class').dataTable().fnDeleteRow(tr);
});

The goal: ,,

So I've got a Table (Which is initialized as a JQuery DataTable). Each row contains a 'remove me' button, and when that button is pressed, I want to delete the row from the current table.

What I've tried:

tr = $(this).closest('tr');
$('.my-table-class').dataTable().fnDeleteRow(tr);

What happens

No matter what row I click on, the last row is deleted from the table is deleted, except if there's only one row in the table, in this situation a javascript error: "TypeError: j is undefined" is thrown from Jquery.dataTable.min.js. Both baffle me.

I can get the attributes of the right row - for example, If do something like: alert($(this).attr("data-name")); I click on John Smith's row, I'll see 'John Smith' in an alert box... so $(this) is the a tag, so why doesn't the .closest() method grab the right trtag?

My Questions:

  • How do I get 'this' row (the one which contained the button which was pressed) in order to delete it?

  • Any idea what's causing the 'TypeError: j is undefined" error when there's only one row in the table?

Details:

Here's the rendered (from .jsp) HTML table:

<table class="table my-table-class">
<thead><tr><th>Name</th><th> </th></tr></thead> 
<tbody>
        <tr>
            <td>John Smith</td>
            <td><a href="javascript://" class="my-button-class" data-name="John Smith"><i class="icon-plus"></i></a></td>
        </tr>

        <tr>
            <td>Robert Paulson</td>
            <td><a href="javascript://" class="my-button-class" data-name="Robert Paulson"><i class="icon-plus"></i></a></td>
        </tr>

        <tr>
            <td>Juan Sanchez</td>
            <td><a href="javascript://" class="my-button-class" data-name="Juan Sanchez"><i class="icon-plus"></i></a></td>
        </tr>
</tbody>

Here's how I initialize the tables as a Jquery DataTable:

$('.st-my-table-class').dataTable( {
            "bInfo": true, 
            "aaSorting": [[ 0, "desc" ]], // sort 1st column 
            "bFilter": true, // allow search bar
            "bPaginate": false,  // no pagination 
            "sDom": '<"top"f>rt<"bottom"lp><"clear">' // (f)ilter bar on top, page (i)nfo omitted
        } );

And here's the whole event handler:

$('.my-button-class').on("click", function(){ 
tr = $(this).closest('tr'); 
$('.my-table-class').dataTable().fnDeleteRow(tr);
});
Share Improve this question edited Aug 18, 2014 at 13:22 mehdi lotfi 11.6k18 gold badges83 silver badges128 bronze badges asked Aug 15, 2014 at 16:01 PaulPaul 3,58210 gold badges40 silver badges63 bronze badges 9
  • Use "parent" instead of closest. – ron tornambe Commented Aug 15, 2014 at 16:05
  • Thanks Ron, I'll give that a try! Any insight into why my approach isn't working? – Paul Commented Aug 15, 2014 at 16:07
  • 1 .parent() gets you to the TD, parent().parent() gets you to the TR that contains it. – BReal14 Commented Aug 15, 2014 at 16:22
  • 3 these are both incorrect suggestions. Closet would work fine in this situation. – PaulBinder Commented Aug 15, 2014 at 16:25
  • 2 Might it be because you are sending it a jquery object and not the native element node? does fnDeleteRow(tr[0]) do the business? – Dawn Commented Aug 15, 2014 at 16:37
 |  Show 4 more ments

3 Answers 3

Reset to default 4

I think This JSFIDDLE is much closer to what you wanted. Here is the basic code

$(function() {
      var dataTable = $('.my-table-class').dataTable();
      $( ".test" ).click(function() {
           var row = $(this).closest('tr'); 
           var nRow = row[0];
           dataTable.dataTable().fnDeleteRow(nRow);
      });     
});

which I pulled from this resource here that explains in full detail on how it works. In short you need to select the node itself not the jQuery object. You can also use .firstlike so.

$( ".test" ).click(function() {
     var row = $(this).closest('tr').first(); 
     dataTable.dataTable().fnDeleteRow(row);

Note: I added the "This" text as I don't have the button style/icon.

As @Dawn suggested, you're passing a jQuery element to fnDeleteRow, which is expecting a HTML node.

Simply try:

$('.my-button-class').on("click", function () {
    tr = $(this).closest('tr').get(0); // gets the HTML node
    $('.my-table-class').dataTable().fnDeleteRow(tr);
});

Working JSFiddle: http://jsfiddle/so4s67b0/

First of all in the function it is need to declare a variable and assign our data table to it. Then take the selected row's index into another variable. After that remove the selected row from the data table. .draw(false) will be manage the pagination and no of rows properties in jquery DataTable.

    $('.my-button-class').click(function () {

        var tbl = $('.my-table-class').DataTable(); 
        var index = tbl.row(this).index();
        var row = $(this).closest("tr").get(index);
        tbl.row(index).remove().draw(false);

    });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论