简体   繁体   English

在进行超链接之前进行数据表验证

[英]Data tables validation before going on hyperlink

I have the following datatable: 我有以下数据表:

$('#tabelOferte').DataTable({
    "lengthMenu": [[10, 25, -1], [10, 25, "All"]],
    "aaSorting": [[0,'desc'], [0,'desc']],
    processing: true,
    serverSide: true,
    ajax: {
        "url":  'ajaxTabelOferte',
        "type": "GET",
    },
    columns: [
        {data:'id', name:'id', "visible": false,  "bSearchable": false },
        {data: 'number' ,name: 'numar'},  
        {data: 'actions', name: 'Actions', "bSearchable": false,
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html("<a href=edit/"+oData.id+">" + "<Edit>" + </a>"+
                    "&nbsp"+
                    "<a href=print/"+oData.id+">" + "Print" + "</a>"
                )
            },
        }
    ],
});

In the last column there are 2 links. 在最后一列中有2个链接。

I want if the user click on the fist link (Edit) to first launch a java script function to make some validations and only if validations are OK to get the link. 我想如果用户单击拳头链接(编辑)首先启动Java脚本函数以进行一些验证,并且仅在验证可以的情况下才能获取链接。

It would be OK to put the links in separate cells if this could make the solution easier. 如果可以使链接更容易解决,则可以将链接放在单独的单元格中。

How do I insert the call for the java script function? 如何插入对Java脚本函数的调用?

Thank you for your time! 感谢您的时间!

You want to check some of validation while user click on the fist link (Edit) 您想在用户单击拳头链接时检查一些验证(编辑)

Solution: 解:

  • First, Remove the hyperlink href edit link 首先,删除超链接href编辑链接
  • Second, Apply a function on edit link click event to check validation 其次,对编辑链接click事件应用功能以检查验证

Check below code: 检查以下代码:

 $(document).ready(function () { redraw_exceptions(4) }) function redraw_exceptions(week_limit) { var testdata = [{ "col1": "1", "col2": "9908", "col3": "171.74", }, { "col1": "2", "col2": "9959", "col3": "156.83", }, { "col1": "3", "col2": "457", "col3": "153.83", }, { "col1": "4", "col2": "452", "col3": "147.73", }, { "col1": "5", "col2": "9927", "col3": "141.90", }]; $('#testTable').DataTable({ "aaData": testdata, columns: [ { data: 'col1', name: 'col1', "visible": false, "bSearchable": false }, { data: 'col2', name: 'col2' }, { data: 'col3', name: 'col3', "bSearchable": false, "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) { $(nTd).html("<a href='javascript:void(0)' onclick='editValidate(" + oData.col1 + ")'>" + "Edit" + "</a>" + "&nbsp" + "<a href=print/" + oData.col1 + ">" + "Print" + "</a>" ) }, } ] }); } function editValidate(editID) { alert('Checking some validations here for : ' + editID); } 
 p{ color:red; } 
 <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <p>Click on "edit" link to check validation function below</p> <table class="table" id="testTable"> <thead> <tr> <th>Col1</th> <th>Col2</th> <th>Col3</th> </tr> </thead> </table> 

Add a click handler to the anchor tag that calls the validation function and returns the result 将一个单击处理程序添加到锚标记中, 标记将调用验证函数并返回结果

"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html("<a href=edit/"+oData.id+
                    "onClick='return validate();'>" + "<Edit>" + </a>"+
                    "&nbsp"+
                    "<a href=print/"+oData.id+">" + "Print" + "</a>"
                )
            }

function validate(){
  // ...add validation logic here...
  // return as boolean
  return result
}

Working example: https://jsfiddle.net/4vkne52u/ 工作示例: https : //jsfiddle.net/4vkne52u/

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

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