我在使用datatables.net时我的DataTables有问题

时间:2019-07-03 07:41:31

标签: php jquery html ajax

我使用datatables.net时我的DataTables出现了问题,我现在遇到的问题是“正在显示#个条目中的#个”无法正常工作,并且不计算在表和分页中显示的数据不起作用,搜索栏不起作用,“显示10/100个条目”不起作用。

PS:我对它们加了引号,因为我不知道你怎么称呼它们。

这是我的html和ajax代码

<div class="card card-dark">
    <h6 class="card-header">User Status Details</h6>
    <div class="card-body">
     <span id="message"></span>
     <div class="table-responsive">
     <table class="table table-striped" id="table-1">
    </div>
   </table>
     <script>
     $(document).ready(function(){

      load_user_data();

      function load_user_data()
      {
       var action = 'fetch';
       $.ajax({
        url:'action',
        method:'POST',
        data:{action:action},
        success:function(data)
        {
         $('#table-1').html(data);
        }
       });
      }

      $(document).on('click', '.action', function(){
       var id = $(this).data('id');
       var user_status = $(this).data('user_status');
       var action = 'change_status';
       $('#message').html('');
       if(confirm("Change User Status"))
       {
        $.ajax({
         url:'action',
         method:'POST',
         data:{id:id, user_status:user_status, action:action},
         success:function(data)
         {
          if(data != '')
          {
           load_user_data();
           $('#message').html(data);
          }
         }
        });
       }
       else
       {
        return false;
       }
      });

     });
     </script>
    </div>
   </div>

我的操作代码

if(isset($_POST['action']))
{

    if($_POST["action"] == 'fetch')
    {
        $output = '';
        $query = "SELECT * FROM users WHERE user_type = 'user' ORDER BY username ASC";
        $statement = $connect->prepare($query);
        $statement->execute();
        $result = $statement->fetchAll();
        $output .= '
            <table class="table table-striped" id="table-1">
            <tr>
                <td>ID #</td>
                <td>Username</td>
                <td>Status</td>
                <td>Action</td>
            </tr>
        ';
        foreach($result as $row)
        {
            $status = '';
            if($row["user_status"] == 'Active')
            {
                $status = '<span class="badge badge-primary">Active</span>';
            }
            else
            {
                $status = '<span class="badge badge-primary">Inactive</span>';
            }
            $output .= '
            <tr>
                <td>'.$row['id'].'</td>
                <td>'.$row['username'].'</td>
                <td>'.$status.'</td>
                <td><button type="button" name="action" class="btn btn-info btn-xs action" data-id="'.$row["id"].'" data-user_status="'.$row["user_status"].'">Action</button></td>
            </tr>
            ';
        }
        $output .= '</table>';
        echo $output;
    }

    if($_POST["action"] == 'change_status')
    {
        $status = '';
        if($_POST['user_status'] == 'Active')
        {
            $status = 'Inactive';
        }
        else
        {
            $status = 'Active';
        }
        $query = '
        UPDATE users SET user_status = :user_status WHERE id = :id
        ';
        $statement = $connect->prepare($query);
        $statement->execute(
            array(
                ':user_status'          => $status,
                ':id'              => $_POST['id']
            )
        );
        $result = $statement->fetchAll();
        if(isset($result))
        {
            echo '<div class="alert alert-info">User status set to <strong>'.$status.'</strong><div>';
        }
    }
}

?>

我无法弄清楚代码中缺少什么,但是ajax代码可以正常工作,并且数据显示在DataTable中

1 个答案:

答案 0 :(得分:0)

HTML

Dim cpRS As New ADODB.Recordset, RS As DAO.Recordset, cb As checkBox, addr As String
'Creating copy of previously displayed result table
Set cpRS = New ADODB.Recordset
With cpRS
'
    .Fields.Append "val0", adInteger
    .Fields.Append "val1", adInteger
    .Fields.Append "val2", adVarChar, 80
    .Fields.Append "val3", adVarChar, 80
    .Fields.Append "val4", adVarChar, 30
    .Fields.Append "val5", adVarChar, 30
    .Fields.Append "val6", adVarChar, 10
    .Fields.Append "val7", adVarChar, 10
    .Fields.Append "val8", adVarChar, 50, adFldIsNullable
    .Fields.Append "val9", adDBTimeStamp
    .Fields.Append "val10", adVarChar, 50
    .Fields.Append "val11", adSmallInt
    .Fields.Append "val12", adVarChar, 100
    .Fields.Append "val13", adVarChar, 255, adFldIsNullable
    .Fields.Append "val14", adVarChar, 255, adFldIsNullable
    .Fields.Append "val15", adInteger
    .Fields.Append "val16", adInteger
    .Fields.Append "val17", adSmallInt
    .Fields.Append "val18", adSmallInt

    'new Fields for temporary purposes
    .Fields.Append "val19", adBoolean
    .Fields.Append "val20", adVarChar, 50

    .CursorLocation = adUseClient
    .Open , , adOpenDynamic, adLockOptimistic, 8
End With

'get result set of previous window by applying filter to the same query used before
Dim argv() As String
Dim argRest As String
Dim qdef As DAO.QueryDef
Dim restrictedQuery As String

'When opening this form I hand over OpenArgs which i restore here
'took the code out but "argv" and "argRest" will be used later    

'this is the query that is used in the previous form. i need an extra where clause though so i had to rewrite it.
restrictedQuery = "some very long SQL statement I feel I don't need to put here because it doesn't contribute to the problem." & _
    "If I'm incorrect, please let me know and I will rewrite it to protect the data in it"

Set qdef = CurrentDb.CreateQueryDef("")
qdef.SQL = restrictedQuery
Set RS = qdef.OpenRecordset
Set RS = CurrentDb.OpenRecordset(restrictedQuery, dbOpenSnapshot)
RS.MoveLast
RS.MoveFirst
If RS.RecordCount = 0 Then
    MsgBox "some error text", vbOKOnly, "error title"
    DoCmd.Close acForm, Me.Name
    Exit Sub
End If

'populate new recordset with data from table in previous form
Do Until RS.EOF
'putting the data from the "old" recordset into the new one, shortened again, you get the idea
    cpRS.AddNew
    cpRS.Fields("val1") = RS("some_value")
    cpRS.Fields("val2") = RS("some_value2")
    '...
    'fill the two columns that don't come from the query with default values
    cpRS.Fields("val19") = False
    cpRS.Fields("val20") = ""
    cpRS.Update
    RS.MoveNext
Loop

Set Me.Recordset = cpRS

RS.Close
Set RS = Nothing
'cpRS.Close  - I removed this
Set cpRS = Nothing

JS

<div class="card card-dark">
    <h6 class="card-header">User Status Details</h6>

    <div class="card-body">
        <span id="message"></span>

            <div id="tableDiv"></div>

    </div>

</div>

PHP

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){

      load_user_data();
 // initialize DT 
 var table = $('#table-1').DataTable();

 $(document).on('click', '.action', function(){
       var id = $(this).data('id');
       var user_status = $(this).data('user_status');
       var action = 'change_status';
       $('#message').html('');
       if(confirm("Change User Status"))
       {
        $.ajax({
         url:'action',
         method:'POST',
         data:{id:id, user_status:user_status, action:action},
         success:function(data)
         {
          if(data != '')
          {
           load_user_data();
           $('#message').html(data);
           // re-load data and re-initialize DT
           $('#tableDiv').html(data); 
           table.rows().invalidate().draw(); // or table.rows().invalidate();
          }
         }
        });
       }
       else
       {
        return false;
       }
 });
});

function load_user_data()
 {
       var action = 'fetch';
 $.ajax({
        url:'action',
        method:'POST',
        data:{action:action},
        success:function(data)
        {
         $('#tableDiv').html(data);
        }
 });
 }
</script>

请尝试全部操作,我希望它会起作用,因为我无法自己检查它。从这一刻开始,在控制台上查看错误。给他们看。