简体   繁体   English

获取JQuery数据表中特定行的ID

[英]get the ID of specific row in JQuery datatable

I have JQuery table like following image 我有如下图的JQuery表

在此处输入图片说明

I want to alert the ProductID once I click V link text in Action column 单击“操作”列中的“ V链接文本后,我想提醒ProductID

    <table id="productTable">

        <thead>
            <tr>
                <th>ProductID</th>
                <th>TitleEN</th>
                <th>TypeEN</th>
                <th>ModifiedDate</th>
                <th>Actions</th>
            </tr>
        </thead>

    </table>  


    @* Load datatable css *@
    <link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="stylesheet" />

@section Scripts{
    @* Load DataTable js here *@
    <script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
    <script>
                $(document).ready(function () {
                    $("#productTable").DataTable({
                        "info": false,
                        "processing": true, 
                        "serverSide": true, 
                        "filter": false, 
                        "orderMulti": false, 
                        "ajax": {
                            "url": "/Home/LoadProductData",
                            "type": "POST",
                            "datatype": "json"
                        },
                        "columns": [
                                { "data": "Product_ID", "name": "Product_ID", "autoWidth": true },
                                { "data": "Product_Title_EN", "name": "Product_Title_EN", "autoWidth": true },
                                { "data": "Product_Type_EN", "name": "Product_Type_EN", "autoWidth": true },
                                { "data": "ModifiedDate", "name": "ModifiedDate", "autoWidth": true },
                                {
                                   data: null,
                                   className: "center",
                                   defaultContent: '<a href="" class="editor_view"> V </a>'
                                }
                                  ]
                    });
                });

                $('#editor_view').on('click', 'a.editor_view', function (e) {

                    alert($("data-Product_ID").val());

                });

    </script>

} }

currently I alerted its like this 目前我这样提醒

alert($("data-Product_ID").val());

but it cant get the ID of that, hw can I do this ? 但它无法获取该ID,为什么我可以这样做?

this is the html for table row 这是表格行的html

<tr class="even" role="row">
<td class="sorting_1">02</td>
<td>Current Accounts</td>
<td>Assets</td>
<td></td>
<td class=" center">
<a class="editor_view" href="#"> V </a>
</td>

Change your script to 将脚本更改为

$("#productTable").on('click', '.editor_view', function() {
    var ID = $(this).closest('tr').find('td').eq(0).text();
    alert(ID);
});

Note that your dynamically generating the table rows so you need event delegation attached to an element that exists in the DOM when the view is first generated (ie the element with id="productTable" ). 请注意,您是动态生成表行的,因此需要在第一次生成视图时将事件委托附加到DOM中存在的元素(即,具有id="productTable"的元素)。

$('#editor_view').on('click', 'a.editor_view', function (e) {
   alert($("data-Product_ID").val());
});

Here you have used "editor_view" as ID and I can view it is set as a class, so it will not work. 在这里,您已将“ editor_view”用作ID,并且我可以查看将其设置为类,因此它将无法使用。 It should be like 应该像

$('.editor_view').on('click', 'a.editor_view', function (e) {
  alert($("data-Product_ID").val());
});

Below is my Data table. 以下是我的数据表。

table_low_stocks.dataTable({
            "bLengthChange": true,
            "processing": false,
           // "serverSide": true,
            "bPaginate": false,
            "bInfo": false,
            "iDisplayLength" : 5,
            "bSort" : true,
            "ajax": {
                'type': 'POST',
                'url': "<?=action('AdvertiserproductsController@postLowstockproducts')?>"
            },
"columns": [
                /* {"data": "sr_no"}, */
                {"data": "product_name"},
                {"data": "inventory"},
                {"data": "update"} 
            ] // set first column as a default sort by asc
        });

This is how i am rendering the HTML (Datatable from Controller ) 这就是我渲染HTML(Controller的Datatable)的方式

return Datatables::of($data)
                        ->add_column('action','<a data-productid="{{$sr_no}}" class="label label-sm label-messages-btn update-product"> <i class="fa fa-pencil-square-o"></i> Update Stock </a>')
                        ->make(true);

If you have seen above i have given data-productid="{{$sr_no}}" to a tag 如果您在上面看到过,我已经将data-productid =“ {{$ sr_no}}”赋予了标签

so ultimately that is what you have to give anyhow,. 所以最终这就是你必须付出的一切。

Now from the jquery part.what have done is . 现在从jquery部分开始。

$(document).on("click",".update-product", function(e){
        e.preventDefault();


                var getProductId = $(this).data("productid");
                alert(getProductId);
        });

and i got the product Id :-) 我得到了产品ID :-)

to give data attribute do like 给数据属性做喜欢

data-productid="{{$sr_no}}"

and to get the data attribute u have to use 并获取数据属性,您必须使用

$(selector).data("productid");

Hope this is helpfull to you. 希望这对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM