本文介绍了在引导数据表中创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,正在创建新行,但是某些值是错误的.

For me the new row is being created, but some values are wrong.

我的jQuery DataTables列表:

My jQuery DataTables list:

 $.ajax({
    url: '@Url.Action("LoadEventChargesByUtility", "Vendor")',
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: "{'stayiD':'" + stayi + "','datetime':'" + datetime + "','Groupid':'" + Groupid + "'}",
    type: "POST", // 'GET' or 'POST' ('GET' is the default)
    success: function (data) {
        data = JSON.parse(data);

        if (data != "No Items") {

            $('#tblevent').dataTable({
                "sPaginationType": "full_numbers",
                "aaData": data,
                "bAutoWidth": false,
                "bDeferRender": true,
                "destroy": true,
                "aoColumns": [


                    { "mData": "SERVICETEXT", 'sTitle': 'Service Name' },
                    { "mData": "ITEMNAME", 'sTitle': 'Item Name' },
                    { "mData": "USGDATE", 'sTitle': 'Usage Date', 'bSortable': false, 'bSearchable': false, 'mRender': function (data, type, full) {
                        return moment(data).format('DD/MM/YYYY');
                        }
                    },
                    //{
                    // "mData": "INSDATE", 'sTitle': 'Insert Date', 'bSortable': false, 'bSearchable': false, 'mRender': function (data, type, full) {
                    //         return moment(data).format('ll');
                    //     }
                    //},

                    { "mData": "QUANTITY", 'sTitle': 'Quantity' },
                    { "mData": "UNITTEXT", 'sTitle': 'Unit' },
                    { "mData": "RATE", 'sTitle': 'RATE' },
                    { "mData": "AMOUNT", 'sTitle': 'AMOUNT' },
                    { "mData": "SID", 'sTitle': 'Edit', 'bSortable': false, 'bSearchable': false, 'mRender': function (data, type, full) {
                        //return ' <div class="hidden-sm hidden-xs action-buttons"><a class="green" href="#"><i class="ace-icon fa fa-pencil bigger-130" onclick="return GetServicecharge(\'' + data + '\');"></i></a></div>';
                        return ' <div class="hidden-sm hidden-xs action-buttons"><a class="green" href="#"><i class="ace-icon fa fa-pencil bigger-130" onclick="return EditEventDetails(this);"></i></a></div>';
                       }
                    }
                ]
            });

            $('#EventDeatils').modal();
            }
            else {

                $('#tblevent >thead').remove();
                $('#tblevent >tbody').remove();
                var tr = '<tr><td>No Records</td></tr>';
                $('#tblevent >tbody').remove();
                $('#tblevent').append(tr);
            }

    },
    error: function (xhr, status, error) {
        var err = eval("(" + xhr.responseText + ")");
        alert(err.Message);
    }
});

在那个使用日期USGDATE和上次编辑SID不能正常工作.创建新行时,它显示默认值.

In that usage date USGDATE and last edit SID are not working correctly. It's showing default value when I am creating a new row.

我的新行添加如下:

var t = $('#tblevent').DataTable();

t.row.add({
    "SERVICETEXT": "<td >service</td>",
    "ITEMNAME": "<td>item</td>",
    "USGDATE": "<td>date</td>",
    "QUANTITY": "<td><input type=\"text\" class=\"txtDtEdit\" style=\"width: 52px;\" value=\"\" /></td>",
    "UNITTEXT": "<td><select id=\"ddlupdateunit\" style=\"width: 73px;\" ></select></td>",
    "RATE": "<td><input type=\"text\" class=\"txtDtEdit\" style=\"width: 52px;\" value=\"\" /></td>",
    "AMOUNT": "<td><input type=\"text\" class=\"txtDtEdit\" style=\"width: 52px;\" value=\"\" /></td>",
    "SID": "<td style=\"width:80px\"><div class=\"hidden-sm hidden-xs action-buttons\"><a class=\"green\" href=\"#\"><i class=\"ace-icon fa fa-check bigger-120\"  onclick=\"return updateRowData(this);\"></i></a></div></td>"
}).draw().node();

最后,需要显示SID详细信息,但它看起来是错误的.如果我触发此功能,则将新行添加到中间,并将新行添加到顶部.

In that last SID details need to appear, but it appears wrong. And if I trigger this function the new row is added in the middle, and this new row needs to be added at the top.

我的html:

<table id="tblevent" class="table table-condensed table-hover table-striped table-bordered">
</table>

三个细节需要更改:

  1. 使用日期列
  2. 最后一个SID需要更改
  1. usage date column
  2. last SID need to change

推荐答案

您应该添加实际数据,而不是<td></td>元素.例如,

You should add actual data and not <td></td> elements. For example,

var t = $('#tblevent').DataTable();

t.row.add({
    "SERVICETEXT": "Service Name",
    "ITEMNAME": "Item name",
    "USGDATE": "1995-12-25",
    "QUANTITY": "1",
    "UNITTEXT": "2",
    "RATE": "3",
    "AMOUNT": "4",
    "SID": "5"
}).draw();

要呈现单元格内容并在单元格中显示<input>元素,请对这些列使用mRender回调,类似于对SID列使用它的方式.

To render cell content and display <input> elements in the cell, use mRender callback for those columns similarly to how you use it for SID column.

这篇关于在引导数据表中创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:47