简体   繁体   English

单击一行必须将页面重定向到另一个URL

[英]Clicking on a row must redirect the page to another URL

I'm using a Datatable in my .Net MVC application. 我在.Net MVC应用程序中使用了数据表。 Currently I have implemented the following: 目前,我已经实现了以下内容:

  • The first column of the table is 'ID' and its data is clickable. 该表的第一列是“ ID”,其数据是可单击的。
  • Clicking on the data redirects the user to another page. 单击数据会将用户重定向到另一个页面。
  • The URL where the user is redirected is dependent on the ID being clicked. 重定向用户的URL取决于所单击的ID。 Hence each row has a unique URL. 因此,每一行都有一个唯一的URL。

Atop of this I have one condition applied which is as follows: 在此之上,我应用了一个条件,如下所示:

  • There's another row named Status. 还有另一行名为状态。 It contains one of these values: Draft or Saved. 它包含以下值之一:草稿或已保存。
  • So based on the value of Draft the URL to be redirected is decided. 因此,根据“草稿”的值确定要重定向的URL。

Here's my current script: 这是我当前的脚本:

"ajax":
   {
     "url": "/Request/Search/LoadData",
     "type": "POST",
     "datatype": "json",
     "data": function (d) {
       d.obj = searchFilters();
     },
   },
   "columns":
     [
       {
         "data": "RequestNo",
         "render": function (data, type,row, full, meta) {
           if (row.Status == "Draft") {
             return '<a href="/ChopRequest/Request?RequestId=' + data + '">' + data + '</a>';
           } else {
             return '<a href="/ChopRequest/ViewRequest/Index?id=' + data + '">' + data + '</a>';
           }
         }
       }
     ]

So in this case the only option the user has is to click the data in the very first column. 因此,在这种情况下,用户唯一的选择是单击第一列中的数据。 Instead I want the entire row to be clickable while preserving the above mentioned conditions. 相反,我希望整个行在保留上述条件的情况下都可单击。

Can anyone tell me how can I achieve this? 谁能告诉我如何做到这一点?

You can detect clicks in other cells and redirect browser to the link located in the first column of the same row. 您可以检测其他单元格中的点击,然后将浏览器重定向到同一行第一列中的链接。

For example: 例如:

$('#example').on('click', 'tbody td', function(){
   window.location = $(this).closest('tr').find('td:eq(0) a').attr('href');
});

 $(document).ready(function (){ var table = $('#example').DataTable(); $('#example').on('click', 'tbody td', function(){ window.location = $(this).closest('tr').find('td:eq(0) a').attr('href'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.js"></script> <link href="https://cdn.datatables.net/r/dt/dt-1.10.9/datatables.min.css" rel="stylesheet"/> <table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tfoot> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </tfoot> <tbody> <tr> <td><a href="//www.datatables.net">Tiger Nixon</a></td> <td>System Architect</td> <td>Edinburgh</td> <td>61</td> <td>2011/04/25</td> <td>$320,800</td> </tr> </tbody> </table> 

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

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