如何在Jquery数据表中使用带有参数的href调用javascript函数添加链接?

时间:2019-11-08 12:20:02

标签: javascript jquery ajax spring-mvc datatables

我想从datatable列中调用editEmployee / deleteEmployee函数Edit / Delete Link with Parameter IdId value。

如何在通过“编辑和删除”链接调用的editEmployee / deleteEmployee函数中传递empId列值。

下面是我的JavaScript代码:

table = $('#employeesTable').DataTable(
                {
                    "sAjaxSource" : "/SpringDemo/employees",
                    "sAjaxDataProp" : "",
                    "order" : [ [ 0, "asc" ] ],
                    "aoColumns" : [
                            {
                                "className": "dt-center",
                                "sClass" : "center",
                                "mData" : "empId"
                            },
                            {
                                "orderable": false,
                                data: null,
                                className: "dt-center",
                                defaultContent: '<a href="javascript:editEmployee(empId);" class="glyphicon glyphicon-pencil text-primary"></a>'
                            } ,
                            {
                                "orderable": false,
                                data: null,
                                className: "dt-center",
                                defaultContent: '<a href="javascript:deleteEmployee(empId);" class="glyphicon glyphicon-remove text-danger"></a>'
                            } ]
                })

2 个答案:

答案 0 :(得分:0)

table = $('#employeesTable').DataTable(
{
    "sAjaxSource" : "/SpringDemo/employees",
    "sAjaxDataProp" : "",
    "order" : [ [ 0, "asc" ] ],
    "aoColumns" : [
            {
                "className": "dt-center",
                "sClass" : "center",
                "mData" : "empId"
            },
            {
                "orderable": false,
                data: null,
                className: "dt-center",
                defaultContent: '<a data-emp_id="[add emp id value here]" class="glyphicon glyphicon-pencil text-primary edit-link"></a>'
            } ,
            {
                "orderable": false,
                data: null,
                className: "dt-center",
                defaultContent: '<a data-emp_id="[add emp id value here]" class="glyphicon glyphicon-remove text-danger delete-link"></a>'
            } ]
})

 $('.edit-link').on('click', function () {
  var empid= $(this).data('emp_id')
  window.location.href="edit url ";
  });

 $('.delete-link').on('click', function () {
   var empid= $(this).data('emp_id')
   window.location.href="delete url ";
 });

基本上,每个类都需要具有onclick事件。然后获取存储在被单击按钮的data-emp_id属性中的empID,然后适当地重定向

答案 1 :(得分:0)

感谢所有评论。

我已经解决了问题,方法是将ID存储在隐藏的输入中,然后像这样在编辑中访问它。

$(document).on('click', '#employeesTable tr', function(e) {
    var row_object = table.row(this).data();
    $("#hdnID").val(row_object.empId);
});

function editEmployee() {
    $.ajax({
        url : 'edit/' + $("#hdnID").val(),
        type : 'GET',
        success : function(result) {
            // Do something with the result
        }
    });
}