doulan9287 2015-03-25 09:57
浏览 84

数据表jquery - 默认分页,排序不起作用

I load a serverside processing, and data loads, but I cant paginate, ordering, or use search tab.

My code is like this:

HTML code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>admin</title>
      <!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">



    <link rel="stylesheet" href="styles/dataTables.bootstrap.css">
    <link rel="stylesheet" href="styles/main.css">


</head>

<body>


<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"> <img src="images/logo-mini.png" alt="logo intothemarketing" class="img-responsive" height="30px" width="30px" /></a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Articulos</a></li>
        <li class="dropdown">
         <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Listados <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Clientes</a></li>
            <li><a href="#">Medios Digitales</a></li>
            <li><a href="#">Editores</a></li>
          </ul>
        </li>
        <li><a href="#">Historico</a></li>
        <li><a href="#">Facturas</a></li>
        <li><a href="#">Registro</a></li>
      </ul>

      <ul class="nav navbar-nav navbar-right">
        <li><a href="logout.php">Logout</a></li>
      </ul>

    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

   <div class="container-fluid">

        <div id="tabla" class="row">
            <div class="col-md-12">
                <table id="miTabla" class="table stripe">
                <!--cabecera de la pagina, titulo e icono-->

        <div id="edicionok" class="alert alert-success" role="alert">Edicion correta!</div>
    <div id="edicionerr" class="alert alert-danger" role="alert">Edicion erronea!</div>

                <!--boton añadir doctor-->
                <button id="creaDoc" type="button" class="btn btn-success navbar-btn">Añadir doctor</button>
                 <!--boton añadir doctor-->

                <!--tabla-->
                    <thead>
                         <th>ID Contenido</th>
                        <th>Contenido</th>
                        <th>Fecha Inicio</th>
                        <th>Fecha Modificación</th>
                        <th>Descripción</th>
                        <th>Precio</th>
                        <th>Cliente</th>
                        <th>Medio</th>
                        <th>Editor</th>
                        <th>Editar</th>
                    </thead>
                    <tbody>
                    </tbody>
                    <tfoot>
                    <th>ID Contenido</th>
                         <th>Contenido</th>
                        <th>Fecha Inicio</th>
                        <th>Fecha Modificación</th>
                        <th>Descripción</th>
                        <th>Precio</th>
                        <th>Cliente</th>
                        <th>Medio</th>
                        <th>Editor</th>
                        <th>Editar</th>
                    </tfoot>
                </table>

            </div>


        </div>





    <script src="scripts/jquery.js"></script>
    <script src="../bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="../bower_components/bootstrap.growl/bootstrap-growl.js"></script>
    <script src="../bower_components/jquery-validate/dist/jquery.validate.js"></script>    
    <script src="scripts/jquery.dataTables.js"></script>
    <script src="scripts/dataTables.bootstrap.js"></script>
     <script src="scripts/modal.js"></script>

    <script src="scripts/main.js"></script> 
</body>
</html>

Main.js

    'use strict';

var IdContenido;

$(document).ready(function() {

  var miTabla = $('#miTabla').DataTable({
           'processing': true,
           'serverSide': true,
           'ajax': 'php/cargar-publi.php',
           'language': {
               'sProcessing': 'Procesando...',
               'sLengthMenu': 'Mostrar _MENU_ registros',
               'sZeroRecords': 'No se encontraron resultados',
               'sEmptyTable': 'Ningún dato disponible en esta tabla',
               'sInfo': 'Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros',
               'sInfoEmpty': 'Mostrando registros del 0 al 0 de un total de 0 registros',
               'sInfoFiltered': '(filtrado de un total de _MAX_ registros)',
               'sInfoPostFix': '',
               'sSearch': 'Buscar:',
               'sUrl': '',
               'sInfoThousands': ',',
               'sLoadingRecords': 'Cargando...',
               'oPaginate': {
                   'sFirst': 'Primero',
                   'sLast': 'Último',
                   'sNext': 'Siguiente',
                   'sPrevious': 'Anterior'
               },
               'oAria': {
                   'sSortAscending': ': Activar para ordenar la columna de manera ascendente',
                   'sSortDescending': ': Activar para ordenar la columna de manera descendente'
               }
           },
           'columns': [
           {
               'data': 'IdContenido'
           }, {
               'data': 'contenido'
           },{
               'data': 'fecha_inicio'
           },{
               'data': 'fecha_modificacion'
           },{
               'data': 'descripcion'
           },{
               'data': 'precio'
           },{
               'data': 'cliente'
           },{
               'data': 'medio'
           }, {
               'data': 'editor',
                'render': function(data) {
                   return '<li>' + data + '</li><br>';
               }
           },  {
               'data': 'IdContenido',
               /*añadimos las clases editarbtn y borrarbtn para procesar los eventos click de los botones. No lo hacemos mediante id ya que habrá más de un
               botón de edición o borrado*/
               'render': function(data) {
                   return '<a class="btn btn-primary editarbtn" href=http://localhost/php/modificar_contenido.php?id_contenido=' + data + '>Editar</a><a data-toggle="modal" data-target="#basicModal"  class="btn btn-warning borrarbtn" href=http://localhost/php/borrar_contenido.php?id_contenido=' + data + '>Borrar</a>';
               }
           }]
       });
} );

PHP File

   <?php
header('Access-Control-Allow-Origin: *');
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */

// DB table to use
$table = 'vpubli3';

// Table's primary key
$primaryKey = 'id_contenido';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'id_contenido', 'dt' => 'IdContenido' ),
    array( 'db' => 'contenido',  'dt' => 'contenido' ),
    array( 'db' => 'fecha_inicio',   'dt' => 'fecha_inicio' ),
    array( 'db' => 'fecha_modificacion',     'dt' => 'fecha_modificacion' ),
    array( 'db' => 'descripcion',     'dt' => 'descripcion' ),
    array( 'db' => 'precio',     'dt' => 'precio' ),
    array( 'db' => 'cliente',     'dt' => 'cliente' ),
    array( 'db' => 'medio',     'dt' => 'medio' ),
    array( 'db' => 'editor',     'dt' => 'editor' )
);

// SQL server connection information
$sql_details = array(
    'user' => 'root',
    'pass' => 'root',
    'db'   => 'itm',
    'host' => 'localhost'
);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

I dont have any errors now, but I had this error:

cannot read property defaults of undefined line 20

Thank you so much

  • 写回答

2条回答 默认 最新

  • doudu9148 2015-03-25 10:12
    关注

    Can you use below function in your listing page where all records get loaded:

    <link href="//cdn.datatables.net/1.10.0/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="http://legacy.datatables.net/release-datatables/media/js/jquery.js"></script>
    <script src="//legacy.datatables.net/release-datatables/media/js/jquery.dataTables.js"></script>
    <script type="text/javascript" language="javascript" class="init">
    $(document).ready(function() {
        $('#miTabla').dataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "iDisplayLength":50,
            "sPaginationType": "full_numbers",
            "sAjaxSource": "YOURPHPPAGE.php"
        } );
    } );
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!