如何重新初始化jquery Datatable [英] How to reinitialize jquery Datatable

查看:275
本文介绍了如何重新初始化jquery Datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重新初始化一个jQuery datatable?我甚至试图删除表格元素。还是那个表正在显示。我的代码是这样的:

How to reinitialize a jQuery datatable? I even tried to remove table element. Still that table is displaying. My code is like this:

function removeExistingDataTableReference(tableid)
{
    if(oTable !=null)
    {
        oTable.fnDestroy();
    }

    if(document.getElementById(tableid)){
      document.getElementById(tableid).innerHTML="";
    }

    oTable=null;

    try
    {
        if(oTable !=null)
        {
            //oTable.fnDestroy();
            alert("error in fndestroy");
        }

        oTable=null;

        if(document.getElementById(tableid)){
            document.getElementById(tableid).innerHTML="";
        }

        if(document.getElementById("FTable"))
        {
            removeElement(document.getElementById("FTable"));   
        }

    }
    catch(e)
    {
        alert("Error happend:"+e.message);
    }

}

function removeElement(element) 
{   
  try
  {
       var elem = document.getElementById('FTable');
       elem.parentNode.removeChild(elem);
       //ert(element.parentNode.id);
       //element.parentNode.removeChild(element);
       alert("removed");
       return true;
   }
   catch(e)
   {
      alert(e.message);
   }

   return false;

}

我该怎么做?搜索按钮后单击表格被加载。再次,当我用另一个搜索参数搜索时,表应该加载新的数据。没有发生。如何解决?

How can I do that? After Search button click table is loaded. Again, when I search with another search parameter, table should load with new data. That is not happening. How to fix it??

表初始化如下:

function createDataTable()
{
    try
    {
         oTable = $('#FTable').dataTable( {
             "bDestroy":true,
             "bJQueryUI": true,
            "sScrollX": "100%",
            "sScrollXInner": tablewidth+"px",
            "bScrollCollapse": true,
            "bSort":false,
            "iDisplayLength" : 50,
            "sPaginationType" : "full_numbers",
            "aLengthMenu": [[10, 18, 50, -1], [10, 18, 50, "All"]]
        } );

        new FixedColumns( oTable, {
            "iLeftColumns": 1,
            "iRightColumns": 1
        } );
    }
    catch (e)
    {
    alert(e.message);
    }   
}


推荐答案

你可以通过清除datatable来重新初始化,然后使用 fnAddData()添加元素。

You can reinitialize the datatable by clearing it and then adding the element using fnAddData().

首先检查 dataTable 存在与否。函数 fnClearTable()将从表中清除数据。

First check whether the dataTable exists or not. The function fnClearTable() will clear the data from table.

在代码中, dataTable 是datatable变量,结果是表的 id

In the code, dataTable is datatable variable and results is the id of table.

if(typeof dataTable === 'undefined'){
    dataTable = $('#results').dataTable({ 
       "aLengthMenu": [
           [25, 50, 100, 200],
           [25, 50, 100, 200]
        ], 
        "iDisplayLength" : 25,
        "sPaginationType": "full_numbers",
    }); 
}else dataTable.fnClearTable();

然后再次使用fnAddData添加数据。

Then again add the data using fnAddData.

dataTable.fnAddData( [key, assignee, summary, status, days]);

这篇关于如何重新初始化jquery Datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆