在asp.netJquery Datatable中获取Id 'undefined' C#

6uxekuva  于 12个月前  发布在  jQuery
关注(0)|答案(1)|浏览(129)

当我试图通过jquery datatable获取特定行的ID时,我收到了未定义的消息警报。
这是我最新的代码,

<table id="gv_Datatable" class="table table-responsive table-hover">
    <thead>
        <tr>
            <th>Id</th>
            <th>Date</th>
            <th>Name</th>
            <th>Receipt</th>
            <th>Payment</th>
            <th>Description</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
          <th>Id</th>
            <th>Date</th>
            <th>Name</th>
            <th>Receipt</th>
            <th>Payment</th>
            <th>Description</th>
        </tr>
    </tfoot>
</table>
<!-- jQuery -->
<script src="Plugins/jquery/jquery.min.js"></script>
<script type="text/javascript">  
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "FillGridMethod.asmx/GetCashBookList",
            dataType: "json",
            success: function (data) {
                var datatableVariable = $('#gv_Datatable').DataTable({
                    dom: 'Bfrtip',      
                    data: data,
                    columns: [
                        { 'data': 'Id', visible: false },
                        {
                            'data': 'cashbookdate', 'render': function (date) {
                                var date = new Date(parseInt(date.substr(6)));
                                var month = date.getMonth() + 1;
                                return date.getDate() + "/" + month + "/" + date.getFullYear();
                            }
                        },
                        { 'data': 'cashbookaccname' },
                        { 'data': 'cashbookreceipt' },
                        { 'data': 'cashbookpayment' },
                        { 'data': 'cashbookdescription' },
                        {
                            "render": function (data, row) { return "<a href='#' id='" + row.Id +"' class='btn btn-success' onclick=DeleteCustomer(this); >Delete</a>"; }
                        }]
                });             
            }
        });

    }); 
    function DeleteCustomer(obj) {
        var rowID = $(obj).data("Id");
        alert(rowID);
    }
</script>

字符串
下面是GetCashBookList方法(从aplogurl调用)

[WebMethod]
public void GetCashBookList()
{
    var cashBook = new List<CashBookModel>();
    string constr = cn.ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        qryFillGrid = " select cashbookid, cashbookdate, cashbookaccname, cashbookreceipt, cashbookpayment, cashbookdescription from tbl_cashbook ";
        var cmd = new SqlCommand(qryFillGrid, con);
        con.Open();
        var dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            var cashBookModel = new CashBookModel
            {
                Id = Convert.ToInt32(dr[0]),
                cashbookdate = Convert.ToDateTime(dr[1]),
                cashbookaccname = dr[2].ToString(),
                cashbookreceipt = Convert.ToDecimal(dr[3]),
                cashbookpayment = Convert.ToDecimal(dr[4]),
                cashbookdescription = dr[5].ToString()
            };
            cashBook.Add(cashBookModel);
        }
    }
    var js = new JavaScriptSerializer();
    Context.Response.Write(js.Serialize(cashBook));
}

q3aa0525

q3aa05251#

首先,render函数有3个参数(data,type,row)。
然后你只需要知道你要删除的数据的Id
有了这两点,我们可以将render函数的代码改为

"render": function (data, type, row) { return "<a href='#' class='btn btn-success' onclick=DeleteCustomer('" + row.Id + "');>Delete</>";}

字符串
DeleteCustomer函数:

function DeleteCustomer(id) {
  // Do your thing...
}


我创建了一个简单的JSFiddle作为演示。

相关问题