【问题标题】:Reselect highlighted row in datatable after page reload页面重新加载后重新选择数据表中突出显示的行
【发布时间】:2015-07-30 02:22:25
【问题描述】:

我正在使用一个数据表,点击一行后,页面会重新加载附加的帖子参数。我想要实现的是,在页面重新加载后,所选行再次突出显示。

片段:

jquery:

gTable = $('#goals').dataTable( { 
    "bProcessing": true,
    "bServerSide": true,
    "bLengthChange": false,
    "iDisplayLength":10,
    "sAjaxSource": "json_goals.php",
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "aoColumns" : [  null,null,null],
    "fnServerData": function ( sSource, aoData, fnCallback ) {
         aoData.push( { "name": "missionId", "value": <?php echo $id; ?> } );
                $.ajax( {
                "dataType": 'json',
                "type": "GET",
                "url": sSource,
                "cache": false,
                "data": aoData,
                "success": fnCallback
            } );
        },
    "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
        if ( aData[0] == <?PHP echo $goalid; ?> ) { $('tr', nRow).addClass('highlight');} // this succeeds, tested with alert.
        return nRow;
        }
    });

CSS:

tr.highlight td{font-weight: bold;background-color: Yellow;}

HTML:

<table id="goals" class="display"><col style="width: 10%"/><col style="width: 20%"/><col style="width: 70%"/>
          <thead><tr>
          <th>goalId</th>
          <th>Goal Type</th>
          <th>Display Text</th>
          </tr></thead><tbody><tr><td colspan="3">Loading....</td></tr></tbody>
       </table>

重新加载:

$("#goals tbody tr").live('click',function(event) { $(gTable.fnSettings().aoData).each(function (){ $(this.nTr).removeClass('row_selected');}); $(event.target.parentNode).addClass('row_selected'); var aData = gTable.fnGetData(this); window.location.replace("missions.php?goalid=" + aData[0] + "&id=" + <?PHP echo $id; ?> + "#tabs-5");});

一切正常,只是该行没有突出显示。 关于如何解决这个问题的任何想法? 谢谢。

【问题讨论】:

  • 我没有看到任何重新加载页面的代码。我没有看到任何发送或接收附加 POST 参数的代码。我没有看到任何将点击事件添加到表格行的代码。为什么要重新加载页面?您在其他地方使用 AJAX,所以您不能使用 AJAX 代替重新加载页面吗?
  • 我添加了重新加载代码。我做 POST 的原因是因为根据选择的行,显示了 17 种不同形式中的 1 种。这些表单都是根据数据库中的数据动态加载的。以这种方式编写代码对我来说要容易得多,主要是因为我以前从未使用过 ajax ;)

标签: jquery datatable highlight


【解决方案1】:

您的 jQuery $() 选择器似乎没有处理您想要的元素。尝试使用$('#goals tr').eq(nRow).addClass('highlight');

更多信息请参见this post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多