【问题标题】:Get id of clicked row获取点击行的id
【发布时间】:2019-09-12 07:12:05
【问题描述】:

我正在使用"jquery": "^3.4.0"DataTables 1.10.18

我有下表:

$(document).ready(() => {
  var table = $('.datatable-responsive').DataTable();
});

$("#edit-row").click(() => {
  var c = this.id
  console.log(c)
});
<!DOCTYPE html>
<html lang="en">

<head>
  <!-- Global stylesheets -->
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
  <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
  <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
  <!-- /global stylesheets -->
  <!-- Core JS files -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
  <!-- /core JS files -->
  <!-- Load plugin -->
  <script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
  <!-- /load plugin -->
  <!-- Theme JS files -->
  <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/sliders/ion_rangeslider.min.js"></script>
  <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/ui/moment/moment_locales.min.js"></script>
</head>

<body class="navbar-top">
  <!-- Page content -->
  <div class="page-content pt-0">
    <!-- Default ordering -->
    <div class="card">
      <div class="card-body">
        <table class="table datatable-responsive dataTable" style="width:100%">
          <thead>
            <tr>
              <th>#</th>
              <th>Status</th>
              <th>Title</th>
              <th>Image</th>
              <th>Profile</th>
              <th class="text-center">Actions</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>-2</td>
              <td><span class="badge badge-success">Posted</span></td>
              <td>Awesome Post</td>
              <td><img src="./assets/img/baby.jpg" alt="" srcset="" style='width:20%;'></td>
              <td>Joe</td>
              <td class="text-center">
                <div class="list-icons">
                  <div class="dropdown">
                    <a href="#" class="list-icons-item" data-toggle="dropdown">
                      <i class="icon-menu9"></i>
                    </a>

                    <div class="dropdown-menu dropdown-menu-right">
                      <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                      <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                    </div>
                  </div>
                </div>
              </td>
            </tr>
            <tr>
              <td>99</td>
              <td><span class="badge badge-secondary">Queued</span></td>
              <td>Cool Post</td>
              <td><img src="./assets/img/diy.jpg" alt="" srcset="" style='width:20%;'></td>
              <td>Brad</td>
              <td class="text-center">
                <div class="list-icons">
                  <div class="dropdown">
                    <a href="#" class="list-icons-item" data-toggle="dropdown">
                      <i class="icon-menu9"></i>
                    </a>

                    <div class="dropdown-menu dropdown-menu-right">
                      <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                      <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                    </div>
                  </div>
                </div>
              </td>
            </tr>
            <tr>
              <td>10</td>
              <td><span class="badge badge-secondary">Queued</span></td>
              <td>Awesome Post</td>
              <td><img src="./assets/img/infographic.jpg" alt="" srcset="" style='width:20%;'></td>
              <td>Tom</td>
              <td class="text-center">
                <div class="list-icons">
                  <div class="dropdown">
                    <a href="#" class="list-icons-item" data-toggle="dropdown">
                      <i class="icon-menu9"></i>
                    </a>

                    <div class="dropdown-menu dropdown-menu-right">
                      <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                      <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                    </div>
                  </div>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- /default ordering -->
      </div>
    </div>
  </div>
</body>

</html>

我正在尝试在点击的编辑按钮上放置一个点击监听器。

但是,我目前只收到undefined 回复。相反,我想取回第一列中的 id。

任何建议我做错了什么?

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    我只是以你的代码为例,在 jQuery 中做一些修改。

    请试试这个,希望对你有用。

    $(document).ready(() => {
        var table = $('.datatable-responsive').DataTable();
    
        $(document).on("click", "#edit-row", function(){
            console.log('Id : ', $(this).closest('.text-center').siblings('.sorting_1').text());
            alert('Id : '+ $(this).closest('.text-center').siblings('.sorting_1').text());
        });
    });
    <!DOCTYPE html>
    <html lang="en">
    
      <head>
        <!-- Global stylesheets -->
        <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
        <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
        <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
        <!-- /global stylesheets -->
        <!-- Core JS files -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
        <!-- /core JS files -->
        <!-- Load plugin -->
        <script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
        <!-- /load plugin -->
        <!-- Theme JS files -->
        <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/sliders/ion_rangeslider.min.js"></script>
        <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/ui/moment/moment_locales.min.js"></script>
      </head>
    
      <body class="navbar-top">
        <!-- Page content -->
        <div class="page-content pt-0">
          <!-- Default ordering -->
          <div class="card">
            <div class="card-body">
              <table class="table datatable-responsive dataTable" style="width:100%">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Status</th>
                    <th>Title</th>
                    <th>Image</th>
                    <th>Profile</th>
                    <th class="text-center">Actions</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>-2</td>
                    <td><span class="badge badge-success">Posted</span></td>
                    <td>Awesome Post</td>
                    <td><img src="./assets/img/baby.jpg" alt="" srcset="" style='width:20%;'></td>
                    <td>Joe</td>
                    <td class="text-center">
                      <div class="list-icons">
                        <div class="dropdown">
                          <a href="#" class="list-icons-item" data-toggle="dropdown">
                            <i class="icon-menu9"></i>
                          </a>
    
                          <div class="dropdown-menu dropdown-menu-right">
                            <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                            <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                          </div>
                        </div>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td>99</td>
                    <td><span class="badge badge-secondary">Queued</span></td>
                    <td>Cool Post</td>
                    <td><img src="./assets/img/diy.jpg" alt="" srcset="" style='width:20%;'></td>
                    <td>Brad</td>
                    <td class="text-center">
                      <div class="list-icons">
                        <div class="dropdown">
                          <a href="#" class="list-icons-item" data-toggle="dropdown">
                            <i class="icon-menu9"></i>
                          </a>
    
                          <div class="dropdown-menu dropdown-menu-right">
                            <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                            <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                          </div>
                        </div>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td>10</td>
                    <td><span class="badge badge-secondary">Queued</span></td>
                    <td>Awesome Post</td>
                    <td><img src="./assets/img/infographic.jpg" alt="" srcset="" style='width:20%;'></td>
                    <td>Tom</td>
                    <td class="text-center">
                      <div class="list-icons">
                        <div class="dropdown">
                          <a href="#" class="list-icons-item" data-toggle="dropdown">
                            <i class="icon-menu9"></i>
                          </a>
    
                          <div class="dropdown-menu dropdown-menu-right">
                            <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                            <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                          </div>
                        </div>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
              <!-- /default ordering -->
            </div>
          </div>
        </div>
      </body>
    
    </html>

    【讨论】:

      【解决方案2】:

      点击处理程序中的this 指向全局Window 对象,因为您使用的是箭头函数,将其更改为常规回调,它将按预期工作。

      查看this answer了解更多信息。

      $(document).ready(() => {
        var table = $('.datatable-responsive').DataTable();
      });
      
      $("#edit-row").click(function(event) {
        var c = this.id;
        console.log(c)
      });
      <!DOCTYPE html>
      <html lang="en">
      
      <head>
        <!-- Global stylesheets -->
        <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
        <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
        <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
        <!-- /global stylesheets -->
        <!-- Core JS files -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
        <!-- /core JS files -->
        <!-- Load plugin -->
        <script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
        <!-- /load plugin -->
        <!-- Theme JS files -->
        <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/sliders/ion_rangeslider.min.js"></script>
        <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/ui/moment/moment_locales.min.js"></script>
      </head>
      
      <body class="navbar-top">
        <!-- Page content -->
        <div class="page-content pt-0">
          <!-- Default ordering -->
          <div class="card">
            <div class="card-body">
              <table class="table datatable-responsive dataTable" style="width:100%">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Status</th>
                    <th>Title</th>
                    <th>Image</th>
                    <th>Profile</th>
                    <th class="text-center">Actions</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td>-2</td>
                    <td><span class="badge badge-success">Posted</span></td>
                    <td>Awesome Post</td>
                    <td><img src="./assets/img/baby.jpg" alt="" srcset="" style='width:20%;'></td>
                    <td>Joe</td>
                    <td class="text-center">
                      <div class="list-icons">
                        <div class="dropdown">
                          <a href="#" class="list-icons-item" data-toggle="dropdown">
                            <i class="icon-menu9"></i>
                          </a>
      
                          <div class="dropdown-menu dropdown-menu-right">
                            <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                            <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                          </div>
                        </div>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td>99</td>
                    <td><span class="badge badge-secondary">Queued</span></td>
                    <td>Cool Post</td>
                    <td><img src="./assets/img/diy.jpg" alt="" srcset="" style='width:20%;'></td>
                    <td>Brad</td>
                    <td class="text-center">
                      <div class="list-icons">
                        <div class="dropdown">
                          <a href="#" class="list-icons-item" data-toggle="dropdown">
                            <i class="icon-menu9"></i>
                          </a>
      
                          <div class="dropdown-menu dropdown-menu-right">
                            <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                            <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                          </div>
                        </div>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td>10</td>
                    <td><span class="badge badge-secondary">Queued</span></td>
                    <td>Awesome Post</td>
                    <td><img src="./assets/img/infographic.jpg" alt="" srcset="" style='width:20%;'></td>
                    <td>Tom</td>
                    <td class="text-center">
                      <div class="list-icons">
                        <div class="dropdown">
                          <a href="#" class="list-icons-item" data-toggle="dropdown">
                            <i class="icon-menu9"></i>
                          </a>
      
                          <div class="dropdown-menu dropdown-menu-right">
                            <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                            <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                          </div>
                        </div>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
              <!-- /default ordering -->
            </div>
          </div>
        </div>
      </body>
      
      </html>

      【讨论】:

        【解决方案3】:

        您正在使用将this 的范围更改为window 的箭头函数,因此要引用元素的范围,最简单的方法是避免这些处理程序的箭头函数并简单地使用传统函数:

        $(document).ready(function() {
          var table = $('.datatable-responsive').DataTable();
        });
        
        $("#edit-row").click(function() {
          var c = this.id
          console.log(c)
        });
        <!DOCTYPE html>
        <html lang="en">
        
        <head>
          <!-- Global stylesheets -->
          <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
          <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
          <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
          <!-- /global stylesheets -->
          <!-- Core JS files -->
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
          <!-- /core JS files -->
          <!-- Load plugin -->
          <script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
          <!-- /load plugin -->
          <!-- Theme JS files -->
          <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/sliders/ion_rangeslider.min.js"></script>
          <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/ui/moment/moment_locales.min.js"></script>
        </head>
        
        <body class="navbar-top">
          <!-- Page content -->
          <div class="page-content pt-0">
            <!-- Default ordering -->
            <div class="card">
              <div class="card-body">
                <table class="table datatable-responsive dataTable" style="width:100%">
                  <thead>
                    <tr>
                      <th>#</th>
                      <th>Status</th>
                      <th>Title</th>
                      <th>Image</th>
                      <th>Profile</th>
                      <th class="text-center">Actions</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td>-2</td>
                      <td><span class="badge badge-success">Posted</span></td>
                      <td>Awesome Post</td>
                      <td><img src="./assets/img/baby.jpg" alt="" srcset="" style='width:20%;'></td>
                      <td>Joe</td>
                      <td class="text-center">
                        <div class="list-icons">
                          <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                              <i class="icon-menu9"></i>
                            </a>
        
                            <div class="dropdown-menu dropdown-menu-right">
                              <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                              <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                            </div>
                          </div>
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td>99</td>
                      <td><span class="badge badge-secondary">Queued</span></td>
                      <td>Cool Post</td>
                      <td><img src="./assets/img/diy.jpg" alt="" srcset="" style='width:20%;'></td>
                      <td>Brad</td>
                      <td class="text-center">
                        <div class="list-icons">
                          <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                              <i class="icon-menu9"></i>
                            </a>
        
                            <div class="dropdown-menu dropdown-menu-right">
                              <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                              <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                            </div>
                          </div>
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td>10</td>
                      <td><span class="badge badge-secondary">Queued</span></td>
                      <td>Awesome Post</td>
                      <td><img src="./assets/img/infographic.jpg" alt="" srcset="" style='width:20%;'></td>
                      <td>Tom</td>
                      <td class="text-center">
                        <div class="list-icons">
                          <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                              <i class="icon-menu9"></i>
                            </a>
        
                            <div class="dropdown-menu dropdown-menu-right">
                              <a href="#" id="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                              <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                            </div>
                          </div>
                        </div>
                      </td>
                    </tr>
                  </tbody>
                </table>
                <!-- /default ordering -->
              </div>
            </div>
          </div>
        </body>
        
        </html>

        但是,如果您仍然需要箭头函数,则将 event 参数传递给箭头函数并使用 event.target 获取元素引用。

        1. 要获取第一列中的id,您可以找到最近的表格行,然后选择表格的第一列以获取该文本值。
        2. 您需要对所有这些行使用类选择器,而不是 id 选择器 #edit-row,因为 id 在 HTML 页面中应该是唯一的。

        $(document).ready(() => {
          var table = $('.datatable-responsive').DataTable();
        });
        
        $(".edit-row").click((e) => {
          var c = e.target.id
          var hashId = $(e.target).closest('tr').find('td:eq(0)').text().trim();
          console.log(hashId)
        });
        <!DOCTYPE html>
        <html lang="en">
        
        <head>
          <!-- Global stylesheets -->
          <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,100,500,700,900" rel="stylesheet" type="text/css">
          <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/css/icons/icomoon/styles.css" rel="stylesheet" type="text/css">
          <link href="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css">
          <!-- /global stylesheets -->
          <!-- Core JS files -->
          <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
          <!-- /core JS files -->
          <!-- Load plugin -->
          <script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
          <!-- /load plugin -->
          <!-- Theme JS files -->
          <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/sliders/ion_rangeslider.min.js"></script>
          <script src="https://gitcdn.link/repo/marcpre/demo_cryptoscreener/master/_other/layout_html/global_assets/js/plugins/ui/moment/moment_locales.min.js"></script>
        </head>
        
        <body class="navbar-top">
          <!-- Page content -->
          <div class="page-content pt-0">
            <!-- Default ordering -->
            <div class="card">
              <div class="card-body">
                <table class="table datatable-responsive dataTable" style="width:100%">
                  <thead>
                    <tr>
                      <th>#</th>
                      <th>Status</th>
                      <th>Title</th>
                      <th>Image</th>
                      <th>Profile</th>
                      <th class="text-center">Actions</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <td>-2</td>
                      <td><span class="badge badge-success">Posted</span></td>
                      <td>Awesome Post</td>
                      <td><img src="./assets/img/baby.jpg" alt="" srcset="" style='width:20%;'></td>
                      <td>Joe</td>
                      <td class="text-center">
                        <div class="list-icons">
                          <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                              <i class="icon-menu9"></i>
                            </a>
        
                            <div class="dropdown-menu dropdown-menu-right">
                              <a href="#" class="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                              <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                            </div>
                          </div>
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td>99</td>
                      <td><span class="badge badge-secondary">Queued</span></td>
                      <td>Cool Post</td>
                      <td><img src="./assets/img/diy.jpg" alt="" srcset="" style='width:20%;'></td>
                      <td>Brad</td>
                      <td class="text-center">
                        <div class="list-icons">
                          <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                              <i class="icon-menu9"></i>
                            </a>
        
                            <div class="dropdown-menu dropdown-menu-right">
                              <a href="#" class="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                              <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                            </div>
                          </div>
                        </div>
                      </td>
                    </tr>
                    <tr>
                      <td>10</td>
                      <td><span class="badge badge-secondary">Queued</span></td>
                      <td>Awesome Post</td>
                      <td><img src="./assets/img/infographic.jpg" alt="" srcset="" style='width:20%;'></td>
                      <td>Tom</td>
                      <td class="text-center">
                        <div class="list-icons">
                          <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                              <i class="icon-menu9"></i>
                            </a>
        
                            <div class="dropdown-menu dropdown-menu-right">
                              <a href="#" class="edit-row" class="dropdown-item"><i class="icon-pencil"></i> Edit</a>
                              <a href="#" class="dropdown-item"><i class="icon-bin"></i> Delete</a>
                            </div>
                          </div>
                        </div>
                      </td>
                    </tr>
                  </tbody>
                </table>
                <!-- /default ordering -->
              </div>
            </div>
          </div>
        </body>
        
        </html>

        【讨论】:

        • 感谢您的回复!如果我点击编辑,有什么建议如何获取第一列中的 id?
        【解决方案4】:

        DataTables 有自己的解决方案,你可以使用select extension 来获取选中的行数据,有一个example 对你很有用

        【讨论】:

          【解决方案5】:

          考虑到您的最终目标(可编辑的数据表),我将允许自己对您的应用提出一些改进建议,以便您不需要真正需要获取点击行的 id 并执行所有HTML 的繁重工作由您自己承担。

          • 建议使用嵌入式 ajax 选项,而不是使用外部 AJAX 调用来填充数据表
          $("#posts").DataTable({
              //specify URL for AJAX-call that retrieves table data in format of array of arrays or array of objects
              //where each element/property corresponds to table columns, ajax.dataSrc property should be set to ''
              //if data array encompassed into 'data' property (default format), 'dataSrc' can be ommitted
              ...
              ajax: {
                url: "/getdata"
                dataSrc: ''
              }
          })
          
          • 使用columns.render 选项修改单元格内容,使其显示为徽章或下拉菜单
          dataTable = $("#posts").DataTable({
              ...
              columns: [
                { data: "id", title: "Id" },
                {
                  data: "status",
                  title: "Status",
                  //render status as a badge, having class, based on status value
                  render: data => `<span class="badge ${data == "posted" ? "badge-success" : "badge-secondary"}">${data}</span>`
                }
                ...
              ]
          })
          
          • essential 点 - 渲染时在下拉元素中留下行号标记,以便您稍后可以在需要编辑/删除表格行时访问此值
          $("#posts").DataTable({
              ...
              columns: [
                ...
                {
                  data: null,
                  title: "Actions",
                  //render 'Actions' column as a drop-down menu wrapper,
                  //appending 'row' attribute with corresponding row number
                  //as a value
                  render: (data, type, row, meta) => `
                  <div class="dropdown" row="${meta.row}">
                  <a href="#" class="list-icons-item" data-toggle="dropdown">
                    <i class="icon-menu9"></i>
                  </a>
                  <div class="dropdown-menu dropdown-menu-right">
                    <a href="#" class="dropdown-item edit-row"><i class="icon-pencil"></i>Edit</a>
                    <a href="#" class="dropdown-item delete-row"><i class="icon-bin"></i>Delete</a>
                  </div>
                </div>
                  `
                }
              ]
          });
          
          • 使用上面的行号来修改目标表行,使用DataTables API方法(例如row().remove()用于删除),我将使用最简单的例子(在前端删除行),因为你没有' t 分享有关您是否要同时修改后端和前端数据或只修改后者的详细信息
          //delete-row callback as an example of how to use row attribute to modify necessary table row
          $("#posts").on("click", ".delete-row", function () {
              //grab dropdown wrapper and obtain row number
              const rowNumber = $(this)
                  .closest(".dropdown")
                  .attr("row");
              //delete corresponding table row and re-draw the table
              dataTable.row(rowNumber).remove();
              dataTable.draw();
          });
          

          您的示例项目的完整工作DEMO可以在over here找到,或者您可以使用link在浏览器的开发工具中检查该演示。

          PS 如果由于某种原因您仍然决定继续您的方法,您可以查询 this 帖子(与您的非常相似)以获取获取数据模型属性的方法( 'id',特别是)被点击的行

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-30
            相关资源
            最近更新 更多