首页 > 解决方案 > 插入数据后是否可以在不插入codeigniter的情况下立即显示表格行?

问题描述

插入数据后是否可以在不插入 codeigniter 的情况下立即显示表格行?我不想使用套接字,但我也找不到解决方案,因为当我使用 ajax 时,div 会自我复制。

标签: phpajaxdatabasecodeigniterreal-time

解决方案


您可以通过使用 ajax 来做到这一点。

我们在这里需要 2 个 ajax 调用。1. 用于填充数据。2.用于插入数据。

成功插入数据后,您可以启动 List 功能。下面给出了一个代码示例。

function populateList(){

   var resultHtml = '';

    $.ajax({
        type: "POST",
        dataType: "json",
        url: 'api/for/getting/list/of/data',
        data:{},
        cache: false,

    success:function(response) {


        $.each(response.list,function(key,val){
            resultHtml += '<tr>';
            resultHtml += val.name;
            resultHtml += '</tr>';
            resultHtml += '<tr>';
            resultHtml += val.mobile;
            resultHtml += '</tr>';
            resultHtml += '<tr>';
            resultHtml += val.address;
            resultHtml += '</tr>';
        })

        //Populate in in the <tbody> of the table
        $('#list_table').html(resultHtml);

    }
  });
}




 $('#insert_buttom').click(function(){
        $.ajax({
        type: "POST",
        dataType: "json",
        url: 'api/for/inserting/data',
        data:{},// pass all the data here
        cache: false,

    success:function(response) {
       populateList();
    }
  });
 })

推荐阅读