简体   繁体   English

如何查找动态表的行索引并基于该获取列文本

[英]How to find row index for dynamic table and on basis of that get column text

This is the table rows created dynamically.这是动态创建的表行。 I want to find the first column text on click of row.我想在单击行时找到第一列文本。 How to find row index for dynamic table and on basis of that get column text?如何查找动态表的行索引并基于该获取列文本?

// table row
if (tableType == "campaignData") {
    var newCampaignRow = `
        <tr aria-selected="false" class="campaign-table-row slds-hint-parent">
        <td role="gridcell" style="display:none;">
        <div class="slds-truncate" title="">CMP` + resultRowsJson[i]["Campaign_CD"] + `</div>
        </td>
        <td role="gridcell">
        <div class="slds-truncate" title="">` + resultRowsJson[i]["Campaign_NM"] + `</div>
        </td>
        <td role="gridcell">
        <div class="slds-truncate" title="">` + resultRowsJson[i]["Send_Type"] + `</div>
        </td>
        <td role="gridcell">
        <div class="slds-truncate" title="">` + resultRowsJson[i]["Status"] + `</div>
        </td>
        <td role="gridcell">
        <div class="slds-truncate" title="">` + resultRowsJson[i]["Sends"] + `</div>
        </td>
        <td role="gridcell">
        <div class="slds-truncate" title="">` + formatDateCreated + `</div>
        </td>
        </tr>
    `;
    $("#campaign_rows").append(newCampaignRow);


    // click function for row and get the row index and column text.
    $("#tbl").click("tr", function () {
        var index = $(this).index();
        console.log("Row index is: " + index);
        var myValue = $('#tbl tr').eq(index).find("div[id=code]").html();
        console.log(myValue);
    });
}

I hope this will help you to solving your problem;我希望这可以帮助您解决问题;

 var resultRowsJson = [ { "Campaign_CD":1,"Campaign_NM":"Sample","Send_Type":"e-mail", "Status":"booked","Sends":"none"} ]; var globalRowCounter = 0; var formatDateCreated = "temp variable will change after problem solved"; function AddNew(){ var i = 0; // I think you forgot this variable, guess codes from in a loop :) var newCampaignRow = ` <tr aria-selected="false" onclick="rowClick(this)" class="campaign-table-row slds-hint-parent" data-row-index="`+globalRowCounter+`"> <td role="gridcell" style="display:none;"> <div id="code" class="slds-truncate" title=""> CMP` + resultRowsJson[i]["Campaign_CD"] + ` </div> </td> <td role="gridcell"> <div class="slds-truncate" title="">` + resultRowsJson[i]["Campaign_NM"] + `</div> </td> <td role="gridcell"> <div class="slds-truncate" title="">` + resultRowsJson[i]["Send_Type"] + `</div> </td> <td role="gridcell"> <div class="slds-truncate" title="">` + resultRowsJson[i]["Status"] + `</div> </td> <td role="gridcell"> <div class="slds-truncate" title="">` + resultRowsJson[i]["Sends"] + `</div> </td> <td role="gridcell"> <div class="slds-truncate" title="">` + formatDateCreated + `</div> </td> </tr> `; $("#campaign_rows").append(newCampaignRow); globalRowCounter++; } // click function for row and get the row index and column text. function rowClick(ths) { var index = $(ths).attr("data-row-index"); console.log("Row index is: " + index); var myValue = $('#campaign_rows tr').eq(index).find("div[id='code']").html(); console.log(myValue); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="campaign_rows" class="table table-bordered"> <table> <button type="button" onclick="AddNew()">Add new tr</button>

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

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