创建表后,数据表不起作用搜索功能

时间:2019-03-16 22:38:32

标签: javascript

创建表行的js脚本运行完毕后,数据表的搜索功能不适用于表行数据

    var geturl = 'http://joinernsg.com/pos/api.php?categories=get';
var tbody = document.getElementById("tr-cl");
    axios.get(geturl).then(function (res) {
        for(var i=0; i<res.data.length; i++){
            var row = tbody.insertRow(i);
            row.insertCell(0).innerHTML = res.data[i].id;
            row.insertCell(1).innerHTML = res.data[i].name; 
        }
    });

$('#datatable').dataTable();

1 个答案:

答案 0 :(得分:0)

这有效。

    let data = [{"id":"18","name":"Breakfast"},{"id":"29","name":"buff"},{"id":"27","name":"cake"},{"id":"26","name":"coffee"},{"id":"17","name":"Dessert"},{"id":"20","name":"Dinner"},{"id":"19","name":"Lunch"},{"id":"28","name":"tea"}];
    var tbody = document.getElementById("tr-cl");
    for(var i=0; i<data.length; i++){
        var row = tbody.insertRow(i);
        row.insertCell(0).innerHTML = data[i].id;
        row.insertCell(1).innerHTML = data[i].name;
    }
<table id="tr-cl"></table>

<table id="tr-cl"></table>

<script>
    var tbody = document.getElementById("tr-cl");
    fetch(`http://joinernsg.com/pos/api.php?categories=get`)
    .then((response) => {
        return response.json();
    })
    .then((data) => {
        data = JSON.parse(data)
        for(var i=0; i<data.length; i++){
            var row = tbody.insertRow(i);
            row.insertCell(0).innerHTML = data[i].id;
            row.insertCell(1).innerHTML = data[i].name;
        }
    })
</script>