twitter-bootstrap 如何获取数据表中选定的表格单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18109821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:52:27  来源:igfitidea点击:

How to get the selected Table cell value in datatable

asp.net-mvcjquerytwitter-bootstrapdatatables

提问by vcs

I am using jquery-2.0.3.min.js, bootstrap.min.js, jquery-ui-1.10.3.min.js, DataTables-1.9.4 with tabletools, datatables.net/blog/Twitter_Bootstrap_2

我正在使用 jquery-2.0.3.min.js、bootstrap.min.js、jquery-ui-1.10.3.min.js、DataTables-1.9.4 和 tabletools、datatables.net/blog/Twitter_Bootstrap_2

My View

我的观点

  <div id="windowDepartment" title="Departments"></div>


 <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered table-hover table-condensed" id="DepartmentTable">
        <thead>
            <tr>
                <th>departmentID</th>
                <th>departmentName</th>
                <th>description</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

Datatable initialisation Script

数据表初始化脚本

    $(document).ready(function () {
    oDepartmentTable = $('#DepartmentTable').dataTable(
            {

                "sDom": "T<'clear'>lfrtip",
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                "bServerSide": true,
                "sAjaxSource": "Department/AjaxList",
                "aaSorting": [[2, 'asc'], [3, 'asc']],
                "aoColumns": [
                                { "mDataProp": "departmentID", "sType": "string", "bVisible": false, "bSearchable": false },
                                { "mDataProp": "departmentName", "sType": "string", "bVisible": true, "bSearchable": true },
                                { "mDataProp": "description", "sType": "string", "bVisible": true, "bSearchable": true },
                                { "mDataProp": null,"bSearchable": false,
                                 "sDefaultContent": '<div class="btn-group"><a class="btn btn-mini dropdown-toggle" data-toggle="dropdown" href="#"><span class="icon-circle-arrow-down"></span></a><ul class="dropdown-menu"><li><a class="editDepartment"> <i class="icon-pencil"></i> Edit</a></li><li><a class="deleteDepartment"> <i class="icon-trash"></i> Delete</a></li></ul></div>'
                                }
                            ],

                "oTableTools": {
                    "sSwfPath": "/Content/DataTables-1.9.4/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
                }

            });

        });

EDIT FORM SCRIPT

编辑表单脚本

       $(document).ready(function () {
            $('#DepartmentTable tbody').on('click', 'a.editDepartment', function (e) {
                e.preventDefault();

                //1. Dose not work shows "not available"
                var aData = oDepartmentTable.fnGetData(this)

                //2. Gets the correct ID if "bVisilble=true"
                var departmentid = $(this).parent().parent().parent().parent().parent().children()[0].innerHTML ;

                //goto Edit Controller. DepartmentID is required here  
                $.get('Department/Edit/' + departmentid , function (data) {
                    $('div#windowDepartment').html(data);

                    //Open Dialog box 
                    $("#windowDepartment").dialog().dialog({
                        resizable: true,
                        height: 500,
                        width: 500,
                        modal: true,
                        buttons:
                    {
                        Edit: function () {
                            editDepartment();   //Saves the data. Works fine
                        }, // end ok button

                        Cancel: function () {
                            $(this).dialog("close");
                        }
                    },  //end buttons
                        close: function () {
                            $(this).dialog("close");
                        }
                    }); //end modal edit
                });
            });
        });

My Problem. (in EDIT FORM SCRIPT)

我的问题。(在编辑表单脚本中)

I need the DepartmentID to pass to my controller ('Department/Edit/' + departmentid)

我需要将部门 ID 传递给我的控制器(“部门/编辑/”+部门 ID)

My observations 1) var aData = oDepartmentTable.fnGetData(this)always shows "not available" in chrome console.

我的观察 1)var aData = oDepartmentTable.fnGetData(this)在 chrome 控制台中总是显示“不可用”。

2) var departmentid = $(this).parent().parent().parent().parent().parent().children()[0].innerHTMLgets the correct departmentID if i use "bVisible": true in datatable initialisation.

2)$(this).parent().parent().parent().parent().parent().children()[0].innerHTML如果我在数据表初始化中使用 "bVisible": true,则var departmentid =获得正确的部门 ID。

(3) I dont want to show departmentID to the end user. if i make "bVisible": false in datatable initialisation then

(3) 我不想向最终用户显示部门 ID。如果我在数据表初始化中设置 "bVisible": false

var departmentid = $(this).parent().parent().parent().parent().parent().children()[0].innerHTMLreturns the departmentName

var departmentid = $(this).parent().parent().parent().parent().parent().children()[0].innerHTML返回部门名称

Thanks

谢谢

采纳答案by davidkonrad

Look at the discussion in the datatables forum hereand fnGetPosition

看看这里的数据论坛和fnGetPosition中的讨论

Try this clickhandler instead :

试试这个点击处理程序:

$(document).ready(function () {
    $('#DepartmentTable tbody tr').on('click', function (e) {

        // get the position of the current data from the node
        var aPos = oDepartmentTable.fnGetPosition( this );

        // get the data array
        var aData = oDepartmentTable.fnGetData( aPos[0] );

        // get departmentID for the row
        var departmentID = aData[aPos].departmentID;
        console.log(departmentID);

        // ... do your stuff here, eg
        //goto Edit Controller. DepartmentID is required here  
        //$.get('Department/Edit/' + departmentid , function (data) {
        //..
        //..
    });
});

It works for me. However, not quite as the docs says. Also, I first tried out with datatables 1.9.1 - it didnt work at all. Guess this area of datatables have had some bugs and have been refactored over the versions.

这个对我有用。然而,并不完全像文档所说的那样。另外,我首先尝试使用数据表 1.9.1 - 它根本不起作用。猜猜数据表的这个区域有一些错误,并且已经在版本中进行了重构。