【问题标题】:Add New Row Datatables JQuery: Requested unknown parameter 'PainSite' for row 2, column 1添加新行数据表 JQuery:为第 2 行第 1 列请求未知参数“PainSite”
【发布时间】:2020-11-18 22:24:16
【问题描述】:

我正在尝试使用 row.add() 在此数据表中添加新行。但后来我得到这个错误:

DataTables 警告:表 id=tblPainAssessChart - 请求第 2 行第 1 列的未知参数“PainSite”。

这是数据表的初始化:

      gblPainChartTable = $('#tblPainAssessChart').DataTable({
            responsive: true,
            "paging": true,
            "lengthChange": true,
            "searching": true,
            "ordering": true,
            "info": true,
            "autoWidth": true,
            language: { search: "",
                searchPlaceholder: "Search",
                sLengthMenu: "_MENU_items"

            },
            processing: true,
            serverSide: true,
            ajax: '{!! route('getAjaxPainAssessChartData', ['id' =>  $T_Patient->id]) !!}',
            columns: [
                {
                    render: function(data, type, row, meta) {
                        data = moment(row.Date, 'YYYY-MM-DD').format('DD/MMM/YYYY');
                       return data;
                    }
                },
                { data: 'PainSite', name:'PainSite'},
                { data: 'TypeofPain', name: 'TypeofPain'},
                { data: 'PainDegree', name: 'PainDegree'},
                {
                    render: function(data, type, row, meta) {
                       
                        data = ''
                        // + '<a onclick="editPA(' + meta.row + ')" class="blue-text btn-edit" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Edit"><i class="fas fa-stop action-icon-img"></i></a>'
                        // + '<a onclick="removePA(' + meta.row + ')" class="red-text btn-remove" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Remove"><i class="fa fa-times action-icon-img"></i></a>'
                        // +'';
                        
                        return data;
                    }
                }
            ],
            "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            "columnDefs": [
                {
                    "targets": 0, // Date
                    "className": "text-center"
                },
                {
                    "targets": 1, // PainSites
                    "className": "text-center",
                    "orderable": false
                },
                {
                    "targets": 2, // Type of Pain
                    "className": "text-center",
                    "orderable": false
                },
                {
                    "targets": 3, // PainDegree
                    "className": "text-center",
                    "orderable": false
                },
                {
                    "targets": 4, // Action 
                    "className": "text-center",
                    "orderable": false
                },
            ]
        });

这是向数据表添加新行的函数。

function addPA(){

     var DatePA = $("#Date").val();
     var PainSite = $("#PainSite").val();
     var TypeofPain = $("#TypeofPain").val();
     var PainDegree = $("#PainDegree").val();
     var DatePAF = moment(DatePA).format("DD-MMM-YYYY"); 
           
      gblPainChartTable.row.add( [
            DatePAF, 
            PainSite,
            TypeofPain,
            PainDegree, 
           '<a onclick="editPA(' + lastPANo + ')" class="blue-text btn-edit" data-toggle="tooltip" data-placement="bottom" title="Edit"><i class="fas fa-pencil-alt action-icon-img"></i></a>' +
           '<a onclick="removePA(' + lastPANo + ')" class="red-text btn-delete" data-toggle="tooltip" data-placement="bottom" title="Delete"><i class="fas fa-trash action-icon-img"></i></a>'

       ]).draw();
}

我错过了什么?

编辑:

现在没有显示错误,但仍然没有将新行添加到表中

【问题讨论】:

    标签: jquery laravel datatable


    【解决方案1】:

    您正在使用 ajax,所以如果您在 columns 中定义了这些列:

    {
        render: function(data, type, row, meta) {
            data = moment(row.Date, 'YYYY-MM-DD').format('DD/MMM/YYYY');
           return data;
        }
    },
    { data: 'PainSite', name:'PainSite'},
    { data: 'TypeofPain', name: 'TypeofPain'},
    { data: 'PainDegree', name: 'PainDegree'},
    {
        render: function(data, type, row, meta) {
           
            data = ''
            // + '<a onclick="editPA(' + meta.row + ')" class="blue-text btn-edit" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Edit"><i class="fas fa-stop action-icon-img"></i></a>'
            // + '<a onclick="removePA(' + meta.row + ')" class="red-text btn-remove" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Remove"><i class="fa fa-times action-icon-img"></i></a>'
            // +'';
            
            return data;
        }
    }
    

    您必须在 add 或 update 方法中传递一个与 columns 中使用的字段相同的对象,如下所示:

    gblPainChartTable.rows.add([{
        "Date": DateValue,
        "PainSite": PainSiteValue,
        "TypeofPain": TypeofPainValue,
        "PainDegree": PainDegreeValue
    }]).draw()
    

    编辑 1

    然后,您可以根据需要对带有新行的表格进行排序。 默认情况下,行添加在最后一个位置(我认为),如果你想在其他位置,你必须做类似的事情:

    gblPainChartTable.order([0, 'asc'], [3, "asc"]).draw()
    

    编辑 2

    thisserverSide...:

    如果要添加新行,则必须在数据源处添加 (在服务器端处理的情况下,即在服务器端 - 通常是一个数据库表)

    上面的代码适用于没有serverSide 的表格,但我看到你有它。

    解决方案是将数据添加到数据库(或 ajax 源),然后类似于:

    gblPainChartTable.clear();
    gblPainChartTable.draw();
    gblPainChartTable.ajax.reload();
    

    还有一件事...你确定要使用serverSide吗?

    【讨论】:

    • 我已将其更改为您显示的内容,它不再显示错误,但它仍然没有将新行添加到数据表中
    • @littlebean 已编辑,我认为该行已添加到最后位置...
    • 我不认为它被添加到表中。因为当我检查它时,表中只有原来的 2 行,没有添加新行。我试过按照你说的重新调整位置,新行不存在。
    • 嗯...所以,我希望它工作的方式是,我有一个添加按钮,它将像上面显示的那样调用 addPA() 函数并向数据表添加一个新行,(我还不想把它保存到数据库表中),在该行中,还会有一个编辑按钮来编辑该行。点击页面底部的保存按钮后,它只会将数据发布到数据库。
    • 目前,我使用 ajax 在服务器端发布值的保存按钮。然后 draw() 并更新数据表。所以我想我需要服务器端吗?或者还有其他方法可以实现吗?
    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多