【问题标题】:Meteor app with DataTables: how to capture the value of selected table row带有 DataTables 的 Meteor 应用程序:如何捕获选定表行的值
【发布时间】:2014-04-10 14:06:17
【问题描述】:

在我的流星应用程序中,我有下表,它包含在数据表中):

<template name="IntroductionWizard_Step_2">
   <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title">Introduction Wizard Step 2: </h3>
                        <h3>Select an Application to describe</h3>
                    </div>
                    <table class="table table-hover table-bordered" id="apptable">
                        <thead>
                            <tr>
                            <th>App ID</th>
                                <th>App Name</th>
                                <th>In Scope</th>
                                <th>App Owner</th>
                                <th>BU</th>
                            </tr>
                        </thead>

                        <tbody>
                            {{#each appList}}
                                {{>appRow}}
                            {{/each}}
                        </tbody>
                    </table>
                    <br />

                </div>
                <br/>
            </div>
        </div>
    </div>
</template>

<template name="appRow">
     <tr>
     <td>{{AppId}} </td>
        <td>{{AppName}} </td>
        <td>{{InScope}} </td>
        <td>{{AppOwner}}</td>
        <td>{{Bu}}</td>
     </tr>
</template> 

这是这个数据表的处理程序:

Template.IntroductionWizard_Step_2.rendered = function(){
  console.log('wizard.js: IntroductionWizard_Step_2 is rendered');

  /* Init the table */
    oTable = $('#apptable').dataTable( );

$("#apptable tbody tr").on('click',function(event) {
    $("#apptable tbody tr").removeClass('row_selected');    
    $(this).addClass('row_selected');
  });  
}

我的问题是:如何捕获选定/点击的表格行的值?

【问题讨论】:

    标签: jquery meteor datatables


    【解决方案1】:

    首先,您可能想要使用Meteor's own template helpers 而不是jQuery 事件处理程序。但无论哪种方式,在您的 click 事件处理程序中,event.target 对象应该引用被单击的 tr

    所以您需要做的就是更新您的appRow 模板,使每个tr 标记都有一个iddata-someIdentifierOfYourChoosing 属性,该属性包含您尝试跟踪的_id 或其他标识符那一排。然后在处理程序中,$(event.target).prop('id')$(event.target).data('someIdentifierOfYourChoosing') 应该检索它。

    编辑这是一个例子(未经测试):

    <template name="appRow">
      <tr data-mongoId="{{_id}}">
        <td>{{AppId}}</td>
        <td>{{AppName}}</td>
        <td>{{InScope}}</td>
        <td>{{AppOwner}}</td>
        <td>{{Bu}}</td>
      </tr>
    </template>
    

    和:

    Template.appRow.events({
      "click tr": function (event) {
        var theRowThatWasClicked = event.target;
        var mongoIdOfThatRow = $(event.target).data("mongoId");
        // Then do whatever you want with those values; update the database, etc.
    
        // Copying/updating your code from your comment for completeness:
        var aPos = oTable.fnGetPosition(event.target);
        var aData = oTable.fnGetData(aPos[0]);
        var value = fnGetSelected( oTable ).AppName;
        console.log(aData);
        $("#apptable tbody tr").removeClass('row_selected');
        $(event.target).addClass('row_selected');
      });
    

    另请参阅 Meteor 文档的 Live HTML templates 部分。

    【讨论】:

    • 现在,我得到了这样的结果: Template.IntroductionWizard_Step_2.rendered = function(){ console.log('wizard.js: IntroductionWizard_Step_2 被渲染'); /* 初始化表 */ oTable = $('#apptable').dataTable( ); $("#apptable tbody tr").on('click',function(event) { var aPos = oTable.fnGetPosition(event.target); var aData = oTable.fnGetData(aPos[0]); var value = fnGetSelected ( oTable ).AppName; console.log(aData); $("#apptable tbody tr").removeClass('row_selected'); $(this).addClass('row_selected'); });我可以用流星的方式做到这一点(任何例子)
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2020-09-26
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多