DataTables(CRUD) - 服务器端添加新记录

时间:2013-05-09 11:11:00

标签: php javascript ajax datatables server-side

我正在尝试使用可在DataTables Data Manager (CRUD) Add-on

上找到的DataTables(CRUD)插件

到目前为止,除非我尝试添加新记录,否则一切正常。 我已按照此链接Adding RecordsWiKi Example

上的示例说明进行操作

这是我用来插入新记录的代码

  // Open the db connection
  $link = dbConnect('dbname');
  $params = array();
  $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
  // Prepare the insert 
  $updateQuery = "INSERT INTO $tblname ($columns) VALUES ($valuesTemp)";
  $id = 0;
  $entity_id = array();

  // Run the insert and if it's been succesful enter the if condition
  if(sqlsrv_query( $link, $updateQuery ) == true){
      // Get the last inserted ID
      $getId = "SELECT MAX(Entity_Id) AS 'Id' FROM HoldingTable WHERE Client_Id = '$client_id' AND Project_Id = '$project_id'";  
      $result = sqlsrv_query( $link, $getId, $params, $options );
      while ( $aRow = sqlsrv_fetch_array( $result, SQLSRV_FETCH_NUMERIC ) )
      {   
          $entity_id = $aRow;   
      }
      $id = $entity_id[0];
      // Close the connection.
      sqlsrv_close( $link );
      // return the id
      echo $id;
  }else{
      // If the insert failed display an error
      echo 'There\'s been an error: ';
      if( ($errors = sqlsrv_errors() ) != null) {
          foreach( $errors as $error ) {
              echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
              echo "code: ".$error[ 'code']."<br />";
              echo "message: ".$error[ 'message']."<br />";
      }
  }
}

如果运行firebug并检查网络,我会正确获取数字ID,就像网站上的示例一样,但是在插入后尝试刷新表时,我仍然收到一条弹出消息,出现以下错误。

DataTables warning (table id = 'dataTable'): Requested unknown parameter 'Entity_Id' from the data source for row 2 (This number changes depending on how many rows I've got)

该消息有两个记录,插入第三个记录后。

我已经尝试移动在插入之前获取id的select,但仍然会得到相同的错误并且无法找出原因。

这是DataTables的初始化:

oTable = $('#dataTable').dataTable({
        "bJQueryUI": true,
        "sScrollX": "100%",
        "sScrollXInner": "400%",
        "sScrollY": calcDataTableHeight(),
        "bScrollCollapse": false,
        "bAutoWidth": false,
        "bDeferRender": true,
        "bPaginate": true,
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/scripts/jmevent/dbhelper.php?Client_Id=<?php echo $client_id; ?>&Project_Id=<?php echo $project_id; ?>&System_Id=<?php echo $system_id; ?>&TableName=<?php echo $tblname;?>",
        "sServerMethod": "POST",
         "oLanguage": {
            "sProcessing": "<img src='/content/images/spinner_squares_circle.gif' height='15px' width='15px' alt='' /> Processing"
        },
        "aaSorting": [[ 1, "desc" ]],
        "aoColumns": [
            { "mDataProp": "Entity_Id" , "bSortable":true, "bVisible": false, "sWidth" : "1%"  },
            { "mDataProp": "External_Id" ,"bSortable":true, "sWidth" : "1%"  },
            { "mDataProp": "Family_Name" ,"bSortable":true, "sWidth" : "5%"  },
            { "mDataProp": "First_Name" ,"bSortable":true, "sWidth" : "5%"  },
            { "mDataProp": "Prefix_Title" ,"bSortable":true, "sWidth" : "1%"  },
            { "mDataProp": "Suffix_Title" ,"bSortable":true, "sWidth" : "1%"  },
            { "mDataProp": "Company" ,"bSortable":true, "sWidth" : "2%"  },
            { "mDataProp": "Address_1" ,"bSortable":true, "sWidth" : "2%"  },
            { "mDataProp": "Address_2" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "Address_3" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "Address_4" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "Zip_Code" ,"bSortable":true, "sWidth" : "1%" },
            { "mDataProp": "City" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "Country_Code" ,"bSortable":true, "sWidth" : "1%" },
            { "mDataProp": "Telephone" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "Telefax" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "EMail" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "EMail2" ,"bSortable":true, "sWidth" : "2%" },
            { "mDataProp": "Session_Id" ,"bSortable":true, "bVisible": false, "sWidth" : "2%"  },
            { "mDataProp": "Gender" ,"bSortable":true, "sWidth" : "1%" },
            { "mDataProp": "JMUser_Id" ,"bSortable":true, "bVisible": false, "sWidth" : "1%"  },
            { "mDataProp": "Password" ,"bSortable":true, "bVisible": false, "sWidth" : "1%"  },
            { "mDataProp": "ExtraField01" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField02" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField03" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField04" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField05" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField06" ,"bSortable":true, "bVisible": true, "sWidth" : "2%" },
            { "mDataProp": "ExtraField07" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField08" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField09" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField10" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Mobile" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "URL" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Passport_Number" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Nationality" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ID_Number" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Date_Of_Birth" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Special_Needs_Physical" ,"bSortable":true, "bVisible": true, "sWidth" : "3%"  },
            { "mDataProp": "Special_Needs_Dietary" ,"bSortable":true, "bVisible": true, "sWidth" : "3%"  },
            { "mDataProp": "First_Name_For_Badge" ,"bSortable":true, "bVisible": true, "sWidth" : "5%"  },
            { "mDataProp": "Family_Name_For_Badge" ,"bSortable":true, "bVisible": true, "sWidth" : "5%"  },
            { "mDataProp": "Biography" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "VAT_Number" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Membership_No" ,"bSortable":true, "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Participant_Template" ,"bSortable":true, "bVisible": true, "sWidth" : "1%"  },
            { "mDataProp": "Language_Code" ,"bSortable":true, "bVisible": true, "sWidth" : "1%"  }
            ],
        "sPaginationType": "two_button", //full_numbers,two_button
        "bInfo": true,
        "sSearch" : true,
        "iDisplayStart": 0,
        "iDisplayLength": 50,
        "bLengthChange": true,
        "aLengthMenu": [[10, 25, 50, 100, 250, 500, -1], [10, 25, 50, 100, 250, 500, "All"]],
        "sDom": '<"H"hflip>rt<"F"ip>',
         "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
            $(nRow).attr("id",aData['Entity_Id']);
            return nRow;
         }
    }).makeEditable({sUpdateURL: "updatedata.php?Client_Id=<?php echo $client_id; ?>&Project_Id=<?php echo $project_id;?>&System_Id=<?php echo $system_id; ?>&TableName=<?php echo $tblname;?>",
            sAddURL: "adddata.php?Client_Id=<?php echo $client_id; ?>&Project_Id=<?php echo $project_id;?>&System_Id=<?php echo $system_id; ?>&TableName=<?php echo $tblname;?>",
            sDeleteURL: "deletedata.php?Client_Id=<?php echo $client_id; ?>&Project_Id=<?php echo $project_id;?>&System_Id=<?php echo $system_id; ?>&TableName=<?php echo $tblname;?>",
            fnShowError: function (message, action) {
                switch (action) {
                    case "update":
                        break;
                    case "delete":  
                        break;
                    case "add":     
                        $("#lblAddError").html(message);
                        $("#lblAddError").show();
                        break;
                }
            },
            fnStartProcessingMode: function () {
                $("#processing_message").dialog();
            },
            fnEndProcessingMode: function () {
                $("#processing_message").dialog("close");
            },
        "aoColumns":[
                        {}, //External_Id
                        {}, //Family_Name
                        {}, //First_Name
                        {}, //Prefix_Title
                        {}, //Suffix_Title
                        {}, //Company
                        {}, //Address_1
                        {}, //Address_2
                        {}, //Address_3
                        {}, //Address_4
                        {}, //Zip_Code
                        {}, //City
                        {
                            tooltip: 'Click to select country code', // Country_Code drop down
                            loadtext: 'Loading...',
                            data: '<?php print json_encode($country, JSON_HEX_APOS); ?>',
                            type: 'select',
                            submit: 'Ok'
                        },
                        {}, //Telephone
                        {}, //Telefax
                        {
                            cssclass: "email"
                        }, //Email
                        {}, //Email2
                        {}, //Gender
                        {}, //ExtraField01
                        {}, //ExtraField02                      
                        {}, //ExtraField03
                        {}, //ExtraField04
                        {}, //ExtraField05
                        {}, //ExtraField06
                        {}, //ExtraField07
                        {}, //ExtraField08
                        {}, //ExtraField09
                        {}, //ExtraField10
                        {}, //Mobile
                        {}, //URL
                        {}, //Passport_Number
                        {}, //Nationality
                        {}, //ID_Number
                        {

                        }, 
                        { //Date_Of_Birth
                            type: 'text',
                            cssclass: 'datefield'
                        }, //Special_Needs_Physical
                        {}, //Special_Needs_Dietary
                        {}, //First_Name_For_Badge
                        {}, //Family_Name_For_Badge
                        {}, //Biography
                        {}, //VAT_Number
                        {}, //Membership_No
                        {}, //Participant_Template
                        {
                            tooltip: 'Click to select participant template', // Participant Template drop down
                            loadtext: 'Loading...',
                            data: '<?php print json_encode($template); ?>',
                            type: 'select',
                            submit: 'Ok'
                        },
                        {
                            tooltip: 'Click to select a language', // Language Code drop down
                            loadtext: 'Loading...',
                            data: '<?php print json_encode($language); ?>',
                            type: 'select',
                            submit: 'Ok'                                
                        } //Language_Code

        ]

    });

我还没有找到解决方案。我一直在谷歌搜索和大多数帖子谈论JSON中的空值,所以我修改了aoColumns并添加了这个sDefaultContent:“”有空值而不是空值。但我仍然得到同样的错误。

1 个答案:

答案 0 :(得分:0)

似乎JSON出现了问题。

我已经为aoColumns中的每个列声明添加了sDefaultContent:“”,现在错误已经消失。

所以我的aoColumns现在看起来像这样

        "aoColumns": [
            { "mDataProp": "checkbox" ,"bSortable":false ,sDefaultContent:"", "sWidth" : "1%"},
            { "mDataProp": "Entity_Id" , "bSortable":true, "bVisible": false, sDefaultContent: "", "sWidth" : "1%"  },
            { "mDataProp": "External_Id" ,"bSortable":true, sDefaultContent: "", "sWidth" : "1%"  },
            { "mDataProp": "Family_Name" ,"bSortable":true, sDefaultContent: "", "sWidth" : "5%"  },
            { "mDataProp": "First_Name" ,"bSortable":true, sDefaultContent: "", "sWidth" : "5%"  },
            { "mDataProp": "Prefix_Title" ,"bSortable":true, sDefaultContent: "", "sWidth" : "1%"  },
            { "mDataProp": "Suffix_Title" ,"bSortable":true, sDefaultContent: "", "sWidth" : "1%"  },
            { "mDataProp": "Company" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%"  },
            { "mDataProp": "Address_1" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%"  },
            { "mDataProp": "Address_2" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "Address_3" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "Address_4" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "Zip_Code" ,"bSortable":true, sDefaultContent: "", "sWidth" : "1%" },
            { "mDataProp": "City" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "Country_Code" ,"bSortable":true, sDefaultContent: "", "sWidth" : "1%" },
            { "mDataProp": "Telephone" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "Telefax" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "EMail" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "EMail2" ,"bSortable":true, sDefaultContent: "", "sWidth" : "2%" },
            { "mDataProp": "Session_Id" ,"bSortable":true, sDefaultContent: "", "bVisible": false, "sWidth" : "2%"  },
            { "mDataProp": "Gender" ,"bSortable":true, sDefaultContent: "", "sWidth" : "1%" },
            { "mDataProp": "JMUser_Id" ,"bSortable":true, sDefaultContent: "", "bVisible": false, "sWidth" : "1%"  },
            { "mDataProp": "Password" ,"bSortable":true, sDefaultContent: "", "bVisible": false, "sWidth" : "1%"  },
            { "mDataProp": "ExtraField01" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField02" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField03" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField04" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField05" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField06" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%" },
            { "mDataProp": "ExtraField07" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField08" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField09" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ExtraField10" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Mobile" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "URL" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Passport_Number" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Nationality" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "ID_Number" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Date_Of_Birth" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "2%"  },
            { "mDataProp": "Special_Needs_Physical" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "3%"  },
            { "mDataProp": "Special_Needs_Dietary" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "3%"  },
            { "mDataProp": "First_Name_For_Badge" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "5%"  },
            { "mDataProp": "Family_Name_For_Badge" ,"bSortable":true, sDefaultContent: "", "bVisible": true, "sWidth" : "5%"  },
            { "mDataProp": "Biography" ,"bSortable":true, "bVisible": true, sDefaultContent: "", "sWidth" : "2%"  },
            { "mDataProp": "VAT_Number" ,"bSortable":true, "bVisible": true, sDefaultContent: "", "sWidth" : "2%"  },
            { "mDataProp": "Membership_No" ,"bSortable":true, "bVisible": true, sDefaultContent: "", "sWidth" : "2%"  },
            { "mDataProp": "Participant_Template" ,"bSortable":true, "bVisible": true, sDefaultContent: "", "sWidth" : "1%"  },
            { "mDataProp": "Language_Code" ,"bSortable":true, "bVisible": true, sDefaultContent: "", "sWidth" : "1%"  }
            ],

全部排序!