简体   繁体   English

如何获取被点击的行的ID并使用该ID显示该行的信息-使用ribs.js

[英]How to get the id of the clicked row and using that id display that row's information-Using backbone.js

I have a table and every tr has a button inside it. 我有一张桌子,每个tr里面都有一个按钮。 Now when I clicked that button I want to listen to the click event and display that tr's information on the page. 现在,当我单击该按钮时,我想听一下click事件并在页面上显示tr的信息。 here is the html: 这是HTML:

<table>
    <tbody>
        <tr>
            <td>Date</td>
            <div class="cc-manage"> 
                <a class="button manage-cc">Manage</a>
            </div>
        </tr>
        <tr>
            <td>expiry</td>
            <div class="cc-manage"> 
                <a class="button manage-cc">Manage</a>
            </div>
        </tr>
    </tbody>
</table>

Js: JS:

$('.manage-cc').on("click", function() {
  var clicked = $(this).closest('tr'); //getting the tr clicked
   var manageCC = new manageCCModel([],{ id: id });
   manageCC.fetch({
    url: 'fetch/account/list',
    success: function(managecc){
        var manageCCTemplate = Mustache.render(manageCCTmpl, managecc.attributes.data); 
        $("#account").html(manageCCTemplate);
});

Above im trying to fetch the information of the tr clicked, however in my case,no matter which 'tr' i click, it always displays the information of the first 'tr'. 在我试图获取被点击的tr信息之上,但是,在我的情况下,无论我点击哪个“ tr”,它总是显示第一个“ tr”的信息。 Also I'm able to listen to the click event above, but I'm not sure where to store that 'id' so that when the click occurs, the event will look for which 'tr' has been clicked and will display that information on the page. 另外,我还可以听上面的click事件,但是我不确定在哪里存储该“ id”,以便在发生单击时,该事件将查找已单击了哪个“ tr”并显示该信息。在页面上。 http://jsfiddle.net/tLen4o7L/ http://jsfiddle.net/tLen4o7L/

Please find below the o/p of my html table: 请在我的html表的o / p下面找到:

在此处输入图片说明

Below is the place where, the button clicks and above is the image shown of the info page displayed on the click..... 下面是单击按钮的位置,上面是单击该按钮时显示的信息页面的图像。

在此处输入图片说明

here is the source: 这是来源:

<tr>
 <td>
   <div class="cc-manage">
      <a class="button manage-cc">Manage</a>
    </div>
 </td>
</tr>

Please let me know of needed more details. 请让我知道所需的更多详细信息。 Any ideas how to achieve this?? 任何想法如何实现这一目标? Thanks 谢谢

First you need to fix the html 首先,您需要修复html

 <table>        
      <tbody>
        <tr>
           <td>Expire Date:</td> 
           <td>10 March 2015</td> 
           <td> 
              <div class="cc-manage">
                    <button class="button manage-cc">Manage</button>

              </div>
            </td>
         </tr>
         <tr>
           <td>Card number:</td> 
           <td>1111222233334444</td> 
           <td> 
              <div class="cc-manage">
                    <button class="button manage-cc">Manage</button>                        
              </div>
           </td>
         </tr>
     </tbody>
  </table>

  <div id='account'></div>

With this html, you can do something like: 使用此html,您可以执行以下操作:

 $('.manage-cc').on("click", function() {
      var clickedrow = $(this).closest('tr');
      var descriptionCell = clickedrow.firstChild();
      var valueCell = descriptionCell.next();

      $("#account").html(descriptionCell.text() + " : " + valueCell.text() );
  });

Here is a simple demo : https://jsfiddle.net/tLen4o7L/1/ 这是一个简单的演示: https : //jsfiddle.net/tLen4o7L/1/

DEMO DEMO

Each of the button-containing divs need to be inside a td element. 每个包含按钮的div必须位于td元素内。

To get the index of the row clicked use: 要获得单击的行的index ,请使用:

var index = $(this).closest('table').find('tr').index(clicked);

To get the id of the row clicked use: 要获得单击的行的id ,请使用:

var id = clicked.prop('id');

Bear in mind that your tr elements do not have ids. 请记住,您的tr元素没有ID。

in html you can have a data attribute on any tag. 在html中,您可以在任何标签上使用data属性。 you can use this to associate data with the element like this: 您可以使用它来将数据与这样的元素相关联:

<table>     
<tbody>
   <tr>
     <td> Date </td>
     <td>
       <div class="cc-manage">
         <a class="button manage-cc" data-rowtype="date">Manage</a>
       </div>
     </td>
     </tr>
    <tr>
      <td> expiry </td>
      <td>
        <div class="cc-manage">
          <a class="button manage-cc" data-rowtype="expiry">Manage</a>
        </div>
      </td>
    </tr>
</tbody>
</table>

now you can just look at the data of the "this" element in your event and know which row you are on. 现在,您只需查看事件中“ this”元素的数据,即可知道您所在的行。

some documentation: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes 一些文档: https : //developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes

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

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