简体   繁体   English

jQuery Datatables更新行以使搜索工作

[英]Jquery Datatables update row for search to work

i'm first time working on DataTables and have a table where one column contains links (which acts like a toggle Add player / Remove Player ). 我是第一次使用DataTables,并且有一个表,其中一列包含链接(其作用类似于切换Add player / Remove Player )。 When clicked, the link changes it's data attribute and innerHTML value. 单击后,链接将更改其data attributeinnerHTML值。 Also on link click there's a server side change, posted with AJAX so i wouldn't have to load the table over and over again all the time. 同样在链接上单击,有一个服务器端更改,用AJAX发布,所以我不必一直重复加载表。 After clicking continue button the user is shown only players who're currently added to the tournament. 单击继续按钮后,仅向用户显示当前已添加到锦标赛中的玩家。

It appears that if i reload the page everything is fine, but once a value in row gets changed with JS the Datatables.search function will not pick the update up. 看来,如果我重新加载页面,一切都很好,但是一旦使用JS更改了行中的值, Datatables.search函数将无法进行更新。 I suspect that i have to change the row value not by editing the innerHTML but change it with Datatables own row editing functions, however i'm having trouble figuring out how can i change the link text (so an element which is inside a <td></td> element. 我怀疑我不必通过编辑innerHTML来更改行值,而是使用Datatables自己的行编辑功能对其进行更改,但是我在弄清楚如何更改链接文本时遇到了麻烦(因此, <td></td>内的元素<td></td>元素。

Some pseudocode how my table looks like: 一些伪代码,我的表是什么样的:

<table id="test">
    <trow>
        <td id='1'>Something again</td>
        <td id='2'>Something</td>
        <td id='3'><a href='#' id='link'>Add Player</a></td>
    </trow>
</table>
<button id='continue' type='button'>Continue</button>

Currently i'm updating element <a> in <td id=3> with code: 目前,我正在使用代码更新<td id=3>元素<a>

//link.click function
var currentId = e.target.id;
$("#"+currentId).text("Remove Player");
//button.click function
table.column(4).search("Remove").draw();

After the update with JS ( .text function) Datatables.search does not pick up the change in table. 使用JS( .text函数)更新后, Datatables.search不会获取Datatables.search中的更改。 Upon refresh the change is picked up by the search function (So table gets redrawn with refresh). 刷新后,搜索功能将获取更改(因此,表将通过刷新来重绘)。 Any ideas? 有任何想法吗?

So i was correct with my guess that table did not get updated and i used the DataTables method to change the value in cell: 所以我猜对了,表没有更新,并且我使用了DataTables方法来更改单元格中的值,这是正确的:

var currentId = e.target.id;
var parentCell = table.cell(document.getElementById(currentId).parentElement);
parentCell.data("<a href='#' class='modifyplayer' id='removeModify"+pid+"' data-actiontype='remove' data-playerid='"+pid+"'>Remove player</a>").draw();

And works like a charm :) 并且像魅力一样工作:)

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

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