简体   繁体   English

使用AJAX或jQuery为动态表中的每一行创建一个超链接

[英]Create a hyperlink for every row in dynamic table using AJAX or jQuery

I need to create the hyperlink for every row of my table created using AJAX. 我需要为使用AJAX创建的表的每一行创建超链接。 Pass the value through the URL and fetch it in the other webpage using jQuery or AJAX. 通过URL传递值,并使用jQuery或AJAX在其他网页中获取。

<script>
 $(document).ready(function() {

            $.getJSON("url here", function(data) {
                    console.log("data table set");
                    console.log("json fetching");

                    for (var i = 0; i < data.length; i++) {
                        var tName = data[i].tName;
                        var pName = data[i].Name;
                        var pType = data[i].prType;
                        tr = $('<tr/>');
                        tr.append("<td>" + data[i].tName + "</td>");
                        tr.append("<td>" + data[i].tName + "</td>");
                        tr.append("<td>" + data[i].prType + "</td>");

                        $('#table1').append(tr);
                        console.log("json fetch complete");

                        $('#table1').DataTable({
                            paginate: true,
                            searching: true,
                            ordering: true,
                            pageLength: 10,
                            select: true

                        });
                    });

            });       
</script>

I need a hyperlink for every row in the table and pass one of the table values in the hyperlink. 我需要为表中的每一行提供超链接,并在超链接中传递表值之一。

Hyperlinks are specified using anchor tag: 使用锚标记指定超链接:

<a href="url_here">Display Name</a>

so in your table, inside a row put this as data in the cell, like 因此在表格中,将其作为数据放入单元格中,例如

<table>
  <tr>
   <td><a href="url_here">Display Name</a></td>
  </tr>
</table>

this works with JQuery-datatable also. 这也适用于JQuery-datatable。 Use loops to generate this html dynamically if necessary. 如有必要,使用循环动态生成此html。

If you wish to pass data, use query string to format the url -- this will be a get request. 如果您希望传递数据,请使用查询字符串设置网址格式-这将是一个get请求。 If you want to make a post request, you will have to add a function to click event and make post request explicitly. 如果要发出发布请求,则必须添加一个函数来单击事件并显式发出发布请求。

Hope this helps !! 希望这可以帮助 !!

Wrap tr with anchor tag generate invalid HTML. anchor tag包裹tr生成无效的HTML。 You can add onclick on tr like below. 您可以在tr上添加onclick ,如下所示。

<tr onclick="window.location='http://www.yoururl.com?param='+ data[i].tName +';'" > //Or what ever you want to pass on next page

</tr>

Include underscore Js in your project 在项目中包含下划线Js

<script type="text/template" id="scriptDataTableTemplate">
    <tr>
        <td><%= tName %></td>
        <td><%= pName %></td>
        <td><%= pType %></td>
        <td class="text-center">
            <a href="/Home/List/<%= id %>">Edit</a>
        </td>
    <tr>
</script>

Create Template like this and give proper id 像这样创建模板并提供正确的ID

In your js code use this 在您的js代码中使用此

var tmpl = _.template($('#scriptDataTableTemplate').html());
for (var i=0; i<data.length; i++){
    var id = data[i].Id;
    var tName = data[i].tName;
    var pName = data[i].Name;
    var pType = data[i].prType;
    var obj = {"id": id , "tName": tName, "pName": pName, "pType": pType};
    var html = tmpl(obj);
    $('#table1').append(html );
}
$('#table1').DataTable({
       paginate: true,
      searching:true,
      ordering: true,
      pageLength: 10,
      select: true

     });

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

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