使用带有jquery& ajax的数据表,DT给出了未知的参数错误

时间:2015-04-24 05:24:42

标签: jquery ajax datatable

这是Datatable结构

function loadseries(){
    seriesTab = $('#series_table').dataTable({   
            "bProcessing": true,
            "bDestroy"  : true,
            "bFilter": false,
            "bSort" : false,
            "sDom": "rt",
             "iDisplayLength": -1,
            "sAjaxSource": "${pageContext.request.contextPath}/sam/relative/loadchild?id="+$('#id').val(),
                "aoColumns": [ 
                              { "mData": "index"}, 
                              { "mData": "childName" },
                              { "mData": "sex" },
                              { "mData": "child_dob" },
                              { "mData": "childId","sClass": "center",
                               "mRender": function () {
                                   return ' <a href="javascript:myDel();" tabindex="-1" id="delete" class="editor_remove"><img  alt="" src="${pageContext.request.contextPath}/images/delete.png" /></a>';
                                    },

                              } 
                                  ]    


    });
}

这是我添加动态行

的功能
$('#add').click(function(e){

    e.preventDefault();     
        if($("#addform").valid()){ 
            var anNodes = $("#series_table tbody tr");
            var tLen=anNodes.length;
            var rowData = seriesTab.fnGetData( anNodes[0] );
            if(rowData==null)
                var index=1;
            else
                index=tLen+1;
        fnClickAddRow(index,$('#childName').val(),$('#sex').val(),$('#child_dob').val());   
        e.preventDefault(); 
        }
    });

    function fnClickAddRow(index,childName,sex,child_dob) {
    var chk=0;
    var anNodes = $("#series_table tbody tr");
    alert("Function begining : "+index+" "+childName+" "+sex+ " "+child_dob);
    alert("length " +anNodes.length);
    for (var i = 0; i < anNodes.length; ++i){   
    var rowData = seriesTab.fnGetData( anNodes[i] );
    alert("In loop childName : "+rowData.childName);
    if(rowData!=null){
        //alert(rowData[0]+"  "+rowData[1]+"  "+rowData[2]+ " "+rowData[3]);
        if(rowData[1]==childName){
         alert("inside if ");  
    chk=1;
     break;
    }}}
    alert("After Foor Loop : "+index+" "+childName+" "+sex+ " "+child_dob);
        seriesTab.fnAddData( [
        index,
        childName,
        sex,
        child_dob,
        '<a href="javascript:myDel();" tabindex="-1" id="delete" class="editor_remove"><img  alt="" src="${pageContext.request.contextPath}/images/delete.png" /></a>'] );
        seriesClear();

} 
function seriesClear()
{
    $('#childName').val('');
    $('#childName').focus();
    }

当我要添加行时,它给出了series_table请求来自datasourse的未知参数索引

我是数据表的新手,请帮帮我..

1 个答案:

答案 0 :(得分:0)

根据您的代码,您的DT中有5列:

{ "mData": "index"}, 
{ "mData": "childName" },
{ "mData": "sex" },
{ "mData": "child_dob" },
{ "mData": "childId","sClass": "center",
  "mRender": function () {
        return ' <a href="javascript:myDel();" tabindex="-1" id="delete" class="editor_remove"><img  alt=""   src="${pageContext.request.contextPath}/images/delete.png" /></a>';
},

但您只在fnClickAddRow

中添加了4列
fnClickAddRow(index,$('#childName').val(),$('#sex').val(),$('#child_dob').val());

你注意到了???

您已在index

中声明if
if(rowData==null)
     var index=1;
else
     index=tLen+1;

为了更安全的一面把它放在外面并将其分配到内部!!