Jquery DataTable删除仅适用于第一行

时间:2017-11-23 10:44:12

标签: jquery asp.net asp.net-mvc asp.net-mvc-4 datatables

我有一个包含几行的表

enter image description here

我想删除所有包含' Rabat'作为一个城市,但什么都没发生。但是,当我改变第一行的值时,它起作用了。 那么为什么它只适用于第一行呢?

这是我的代码:

$("#btnSave").click(function() { 
       var table = $("#mainTable").DataTable({});
       var nodes = [];

       table.rows().every(function(rowIdx, tableLoop, rowLoop) {
          if (this.data()[3] == 'Rabat') 
              nodes.push(this.node())
       })
       nodes.forEach(function(node) {
          table.row(node).remove().draw()
       })
});

HTML code:

<table id="mainTable" class="table table-striped">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Country</th>
                                <th>City</th>
                            </tr>
                        </thead>
                        @foreach (var item in Model)
                        {
                            <tbody>
                                <tr>
                                    <td>@item.id</td>
                                    <td>@item.name</td>
                                    <td>@item.country</td>
                                    <td>@item.city</td>
                                </tr>
                            </tbody>
                        }
     </table>

1 个答案:

答案 0 :(得分:2)

您意外地创建了多个Try this code, public class ExpandableListMAdapter extends BaseExpandableListAdapter { Context context; public ArrayList<String> mPhraseList; private HashMap<Integer,ArrayList<String>> occurenceList; public ExpandableListMAdapter(Context context, ArrayList<String> mPhraseList, HashMap<Integer, ArrayList<String>> occurenceList) { this.context = context; this.mPhraseList = mPhraseList; this.occurenceList = occurenceList; } public Object getChild(int groupPosition, int childPosition){ return this.occurenceList.get(this.mPhraseList.get(groupPosition)).get(childPosition); } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ArrayList<String> childList = occurenceList.get(groupPosition); if(convertView==null){ LayoutInflater infalInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_item, null); } TextView txtchild; txtchild=(TextView)convertView.findViewById(R.id.txtchild); txtchild.setText(childList.get(childPosition)); /* TextView occurenceId,fullPhrase,pageNo; occurenceId=(TextView)convertView.findViewById(R.id.occurenceId); fullPhrase=(TextView)convertView.findViewById(R.id.fullPhrase); pageNo=(TextView)convertView.findViewById(R.id.pageNo); occurenceId.setText(childList.get(groupPosition).getOccurenceId()); fullPhrase.setText(childList.get(groupPosition).getFullPhrase()); pageNo.setText(childList.get(groupPosition).getPageNo());*/ return convertView; } public int getChildrenCount(int groupPosition) { return this.occurenceList.get(groupPosition).size(); } public Object getGroup(int groupPosition) { return this.mPhraseList.get(groupPosition); } public int getGroupCount() { return this.mPhraseList.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ArrayList<String> parentList = mPhraseList; if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.list_group, null); } TextView txtgroup; txtgroup=(TextView)convertView.findViewById(R.id.txtgroup); txtgroup.setText(parentList.get(groupPosition)); /* TextView docName,totalOccurences; docName=(TextView)convertView.findViewById(R.id.docName); totalOccurences=(TextView)convertView.findViewById(R.id.totalOccurences); docName.setText(parentList.get(groupPosition).getDocumentName()); totalOccurences.setText(parentList.get(groupPosition).getNoOfOccurences());*/ return convertView; } public boolean hasStableIds() { return false; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }

<tbody>

应该是

@foreach (var item in Model)
{
<tbody>
   <tr>
      <td>@item.id</td>
      <td>@item.name</td>
      <td>@item.country</td>
      <td>@item.city</td>
   </tr>
</tbody>
}

DataTables一次只能处理一个<tbody> @foreach (var item in Model) { <tr> <td>@item.id</td> <td>@item.name</td> <td>@item.country</td> <td>@item.city</td> </tr> } </tbody> ,这就是为什么它只适用于第一行。

此外,在检索dataTable实例时应使用<tbody>;如果您使用大括号,即传递一个对象,您也会意外地重新启动表格。