每点击一次按钮就重新加载数据表

时间:2019-03-11 07:43:33

标签: jquery datatable datatables

我正在重新加载我的数据表。我想在每次搜索按钮单击时重新加载我的数据表。我尽一切努力解决了这个问题,但我做不到。 有人可以帮助我吗?

<script>
function getData(){
    var URL_PREFIX="http://localhost:8983/solr/archiveCore/select?q=strSO:";
    var URL_MIDDLE="AND PackName:";
    var URL_SUFFIX="AND DocType:";
    var strSO="\"" + $("#ngramBoxstrSO").val() + "\"";
    var PackName="\"" + $("#ngramBoxPackName").val() + "\"";
    var DocType="\"" + $("#ngramBoxDocType").val() +"\"";
    var URL=URL_PREFIX + strSO + URL_MIDDLE + PackName + URL_SUFFIX + DocType;
    $.ajax({
        url:URL,
        dataType:'jsonp',
        jsonp : 'json.wrf',
        type :'get',
        cache :false,
        success: function(data){
            var docs=JSON.stringify(data.response.docs);
            var jsonData=JSON.parse(docs);
            var html='';
            $.each(jsonData,function(key,value){
                html+='<tr>';
                html+='<td>'+value.id+'</td>';
                html+='<td>'+value.strSO+'</td>';
                html+='<td>'+value.PackName+'</td>';
                html+='<td>'+value.DocType+'</td>';
                html+='<td>'+value.DocName+'</td>';
                html+='<td class="text-center"><button id="'+value.FilePath+""+'type="button onclick="openDocument(this.id)" class="btn btn-sm" >OPEN</td>';
                html+='</tr>';
            });
            $('#example').append(html);

            var table=$('#example').DataTable({
                "aaSorting" : [],
            });

        },
    });


};
</script>

这是我的跑步机

view

当我单击搜索按钮后,它可以正常工作,但是我单击了不止一次,这给了我数据表重新初始化错误。

<label for="strSO">Sales-Order: </label> <input id="ngramBoxstrSO"></input>
        <label for="PackName">PackName: </label> <input id="ngramBoxPackName"></input>
        <label for="DocType">DocType: </label> <input id="ngramBoxDocType"></input>
        <input type="button" value="SEARCH" onclick="getData()">
        <br><br>

        <table id="example" class="table table-primary table-bordered table-sm table-hover">

            <thead>
                <tr>
                    <th class="text-center" style="width: 4%">Id</th>
                    <th class="text-center" style="width: 7%">SalesOrder</th>
                    <th class="text-center" style="width: 15%">Document Name</th>
                    <th class="text-center" style="width: 15%">Package Type</th>
                    <th class="text-center" style="width: 20%">Document Type</th>
                    <th class="text-center" style="width: 5%">Open</th>

                </tr>
            </thead>

            <tbody id="tbody">

            </tbody>


            <tfoot>
                <tr>
                    <th class="text-center">Id</th>
                    <th class="text-center">SalesOrder</th>
                    <th class="text-center">Document Name</th>
                    <th class="text-center">Package Type</th>
                    <th class="text-center">Document Type</th>
                    <th class="text-center">Open</th>
                </tr>
            </tfoot>
        </table>

4 个答案:

答案 0 :(得分:0)

getData()函数外部添加此

if($.fn.dataTable.isDataTable( '#example' )){
    table = $('#example').DataTable();
}

将此内容添加到ajax success函数中

table.destroy();
$('#example').append(html);
table=$('#example').DataTable({
    "aaSorting" : []
});

答案 1 :(得分:0)

如果您给了html示例,那么解决起来会更好。但是您可以尝试以下方法:

if (typeof table !== 'undefined') {    // write this before your datatable statement
        table.fnClearTable();
    }

var table=$('#example').DataTable({
         "aaSorting" : [],
});

没有什么适合您,因此请尝试以下解决方案:

HTML只是没有表标签而已:

<label for="strSO">Sales-Order: </label> <input id="ngramBoxstrSO"></input>
        <label for="PackName">PackName: </label> <input id="ngramBoxPackName"></input>
        <label for="DocType">DocType: </label> <input id="ngramBoxDocType"></input>
        <input type="button" value="SEARCH" onclick="getData()">
        <br><br>
        <div class="tableDiv">
       </div>

现在,在您的getdata函数中:

    function getData(){
        var URL_PREFIX="http://localhost:8983/solr/archiveCore/select?q=strSO:";
        var URL_MIDDLE="AND PackName:";
        var URL_SUFFIX="AND DocType:";
        var strSO="\"" + $("#ngramBoxstrSO").val() + "\"";
        var PackName="\"" + $("#ngramBoxPackName").val() + "\"";
        var DocType="\"" + $("#ngramBoxDocType").val() +"\"";
        var URL=URL_PREFIX + strSO + URL_MIDDLE + PackName + URL_SUFFIX + DocType;

    $('.tableDiv').html('');
    tableContent = '<table id="example" class="table table-primary table-bordered table-sm table-hover">

            <thead>
                <tr>
                    <th class="text-center" style="width: 4%">Id</th>
                    <th class="text-center" style="width: 7%">SalesOrder</th>
                    <th class="text-center" style="width: 15%">Document Name</th>
                    <th class="text-center" style="width: 15%">Package Type</th>
                    <th class="text-center" style="width: 20%">Document Type</th>
                    <th class="text-center" style="width: 5%">Open</th>

                </tr>
            </thead>

            <tbody id="tbody">

            </tbody>


            <tfoot>
                <tr>
                    <th class="text-center">Id</th>
                    <th class="text-center">SalesOrder</th>
                    <th class="text-center">Document Name</th>
                    <th class="text-center">Package Type</th>
                    <th class="text-center">Document Type</th>
                    <th class="text-center">Open</th>
                </tr>
            </tfoot>
        </table>';
$('.tableDiv').append(tableContent);
        $.ajax({
            url:URL,
            dataType:'jsonp',
            jsonp : 'json.wrf',
            type :'get',
            cache :false,
            success: function(data){
                var docs=JSON.stringify(data.response.docs);
                var jsonData=JSON.parse(docs);
                var html='';
                $.each(jsonData,function(key,value){
                    html+='<tr>';
                    html+='<td>'+value.id+'</td>';
                    html+='<td>'+value.strSO+'</td>';
                    html+='<td>'+value.PackName+'</td>';
                    html+='<td>'+value.DocType+'</td>';
                    html+='<td>'+value.DocName+'</td>';
                    html+='<td class="text-center"><button id="'+value.FilePath+""+'type="button onclick="openDocument(this.id)" class="btn btn-sm" >OPEN</td>';
                    html+='</tr>';
                });
                $('#example tbody').append(html);

                var table=$('#example').DataTable({
                    "aaSorting" : [],
                });

            },
        });


    };

答案 2 :(得分:0)

尝试通过此功能更改 getData()函数。

function getData(){
var URL_PREFIX="http://localhost:8983/solr/archiveCore/select?q=strSO:";
var URL_MIDDLE="AND PackName:";
var URL_SUFFIX="AND DocType:";
var strSO="\"" + $("#ngramBoxstrSO").val() + "\"";
var PackName="\"" + $("#ngramBoxPackName").val() + "\"";
var DocType="\"" + $("#ngramBoxDocType").val() +"\"";
var URL=URL_PREFIX + strSO + URL_MIDDLE + PackName + URL_SUFFIX + DocType;
$.ajax({
    url:URL,
    dataType:'jsonp',
    jsonp : 'json.wrf',
    type :'get',
    cache :false,
    success: function(data){
        var docs=JSON.stringify(data.response.docs);
        var jsonData=JSON.parse(docs);

        $('#example').DataTable().destroy();
        $('#example').DataTable({
            data: jsonData,
            columns: [
                { data: 'id' },
                { data: 'strSO' },
                { data: 'PackName' },
                { data: 'DocType' },
                { data: 'DocName' },
                { data: 'FilePath',
                  render: function (data, type, full, meta) {
                              return '<button type="button" onclick="openDocument(' + data + ')" class="btn btn-sm" >OPEN</button>';
                  } 
                }
            ]
        })
    },
});

答案 3 :(得分:0)

尝试bDestroy

$('#example').DataTable({
   "aaSorting" : [],
   "bDestroy": true
});