简体   繁体   English

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

[英]How to add link with href calling javascript function with parameter in Jquery datatables?

I want to call editEmployee / deleteEmployee function from datatable column Edit / Delete Link With Parameter empId value.我想从数据表列中调用 editEmployee / deleteEmployee function Edit / Delete Link With Parameter empId value。

How can i pass empId column value in editEmployee / deleteEmployee function called from Edit And Delete links.如何在从编辑和删除链接调用的 editEmployee / deleteEmployee function 中传递 empId 列值。

Below is my javascript code:下面是我的 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>'
                            } ]
                })
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 ";
 });

Basically You need to have onclick events for each based on the class.. then fetch the empID stored in the data-emp_id attribute of the button thats been clicked and then redirect appropriately基本上,您需要根据 class 为每个事件提供 onclick 事件。然后获取存储在已单击按钮的 data-emp_id 属性中的 empID,然后适当地重定向

Thanks All For Comments.谢谢大家的意见。

I have solved issue by storing id in hidden input and accessing it in edit like this.我通过将 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
        }
    });
}

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

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