无法将ajax数据加载到bootstrap模式中

时间:2014-04-19 02:02:55

标签: jquery ajax twitter-bootstrap datatables

我有一个数据表,可以从AJAX源获取数据,效果很好。我遇到的问题是我想单击表获取单击行的id,然后打开带有数据的bootstrap模式。

当我将脚本复制到Chrome的控制台时效果很好,但当我尝试将其包含在文件中时,如下面的代码,我的模态为空。它很奇怪,因为我的控制台显示的输出与它工作时的输出相同。

<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Hello</title>
      <!-- Core CSS - Include with every page -->
      <link href="bootstrap.min.css" rel="stylesheet">
      <link href="css/font-awesome.css" rel="stylesheet">
      <!-- Page-Level Plugin CSS - Tables -->
      <link href="dataTables.bootstrap.css" rel="stylesheet">
      <!-- SB Admin CSS - Include with every page -->
      <link href="sb-admin.css" rel="stylesheet">
   </head>
   <body>
      <div id="wrapper">
         <nav class="navbar navbar-default navbar-fixed-top" role="navigation" style="margin-bottom: 0">
            <div class="navbar-header">
               <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
               <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="/">Hello</a>
            </div>
            <!-- /.navbar-header -->
            <ul class="nav navbar-top-links navbar-right">
               <li class="dropdown">
                  <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                  <i class="fa fa-envelope fa-fw"></i>  <i class="fa fa-caret-down"></i>
                  </a>
                  <ul class="dropdown-menu dropdown-messages">
                     <li>
                        <a href="#">
                           <div>
                              <strong>John Smith</strong>
                              <span class="pull-right text-muted">
                              <em>Yesterday</em>
                              </span>
                           </div>
                           <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eleifend...</div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <strong>John Smith</strong>
                              <span class="pull-right text-muted">
                              <em>Yesterday</em>
                              </span>
                           </div>
                           <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eleifend...</div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <strong>John Smith</strong>
                              <span class="pull-right text-muted">
                              <em>Yesterday</em>
                              </span>
                           </div>
                           <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eleifend...</div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a class="text-center" href="#">
                        <strong>Read All Messages</strong>
                        <i class="fa fa-angle-right"></i>
                        </a>
                     </li>
                  </ul>
                  <!-- /.dropdown-messages -->
               </li>
               <!-- /.dropdown -->
               <li class="dropdown">
                  <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                  <i class="fa fa-tasks fa-fw"></i>  <i class="fa fa-caret-down"></i>
                  </a>
                  <ul class="dropdown-menu dropdown-tasks">
                     <li>
                        <a href="#">
                           <div>
                              <p>
                                 <strong>Task 1</strong>
                                 <span class="pull-right text-muted">40% Complete</span>
                              </p>
                              <div class="progress progress-striped active">
                                 <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
                                    <span class="sr-only">40% Complete (success)</span>
                                 </div>
                              </div>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <p>
                                 <strong>Task 2</strong>
                                 <span class="pull-right text-muted">20% Complete</span>
                              </p>
                              <div class="progress progress-striped active">
                                 <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: 20%">
                                    <span class="sr-only">20% Complete</span>
                                 </div>
                              </div>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <p>
                                 <strong>Task 3</strong>
                                 <span class="pull-right text-muted">60% Complete</span>
                              </p>
                              <div class="progress progress-striped active">
                                 <div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%">
                                    <span class="sr-only">60% Complete (warning)</span>
                                 </div>
                              </div>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <p>
                                 <strong>Task 4</strong>
                                 <span class="pull-right text-muted">80% Complete</span>
                              </p>
                              <div class="progress progress-striped active">
                                 <div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width: 80%">
                                    <span class="sr-only">80% Complete (danger)</span>
                                 </div>
                              </div>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a class="text-center" href="#">
                        <strong>See All Tasks</strong>
                        <i class="fa fa-angle-right"></i>
                        </a>
                     </li>
                  </ul>
                  <!-- /.dropdown-tasks -->
               </li>
               <!-- /.dropdown -->
               <li class="dropdown">
                  <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                  <i class="fa fa-bell fa-fw"></i>  <i class="fa fa-caret-down"></i>
                  </a>
                  <ul class="dropdown-menu dropdown-alerts">
                     <li>
                        <a href="#">
                           <div>
                              <i class="fa fa-comment fa-fw"></i> New Comment
                              <span class="pull-right text-muted small">4 minutes ago</span>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <i class="fa fa-twitter fa-fw"></i> 3 New Followers
                              <span class="pull-right text-muted small">12 minutes ago</span>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <i class="fa fa-envelope fa-fw"></i> Message Sent
                              <span class="pull-right text-muted small">4 minutes ago</span>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <i class="fa fa-tasks fa-fw"></i> New Task
                              <span class="pull-right text-muted small">4 minutes ago</span>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a href="#">
                           <div>
                              <i class="fa fa-upload fa-fw"></i> Server Rebooted
                              <span class="pull-right text-muted small">4 minutes ago</span>
                           </div>
                        </a>
                     </li>
                     <li class="divider"></li>
                     <li>
                        <a class="text-center" href="#">
                        <strong>See All Alerts</strong>
                        <i class="fa fa-angle-right"></i>
                        </a>
                     </li>
                  </ul>
                  <!-- /.dropdown-alerts -->
               </li>
               <!-- /.dropdown -->
               <li class="dropdown">
                  <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                  <i class="fa fa-user fa-fw"></i>  <i class="fa fa-caret-down"></i>
                  </a>
                  <ul class="dropdown-menu dropdown-user">
                     <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
                     </li>
                     <li><a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a>
                     </li>
                     <li class="divider"></li>
                     <li><a href="/logout"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
                     </li>
                  </ul>
                  <!-- /.dropdown-user -->
               </li>
               <!-- /.dropdown -->
            </ul>
            <!-- /.navbar-top-links -->
            <div class="navbar-default navbar-static-side" role="navigation">
               <div class="sidebar-collapse">
                  <ul class="nav" id="side-menu">
                     <li class="sidebar-search">
                        <div class="input-group custom-search-form">
                           <input type="text" class="form-control" placeholder="Search...">
                           <span class="input-group-btn">
                           <button class="btn btn-default" type="button">
                           <i class="fa fa-search"></i>
                           </button>
                           </span>
                        </div>
                        <!-- /input-group -->
                     </li>
                     <li>
                        <a href="/"><i class="fa fa-dashboard fa-fw"></i> Dashboard</a>
                     </li>
                     <li>
                        <a href="#"><i class="fa fa-bar-chart-o fa-fw"></i> Charts<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                           <li>
                              <a href="/flot">Flot Charts</a>
                           </li>
                           <li>
                              <a href="/morris">Morris.js Charts</a>
                           </li>
                        </ul>
                        <!-- /.nav-second-level -->
                     </li>
                     <li>
                        <a href="/tables"><i class="fa fa-table fa-fw"></i> Tables</a>
                     </li>
                     <li>
                        <a href="/technicians"><i class="fa fa-users"></i> Technicians</a>
                     </li>
                     <li>
                        <a href="/cart"><i class="fa fa-shopping-cart"></i> Cart</a>
                     </li>
                     <li>
                        <a href="/forms"><i class="fa fa-edit fa-fw"></i> Forms</a>
                     </li>
                     <li>
                        <a href="#"><i class="fa fa-wrench fa-fw"></i> UI Elements<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                           <li>
                              <a href="/panels-wells">Panels and Wells</a>
                           </li>
                           <li>
                              <a href="/buttons">Buttons</a>
                           </li>
                           <li>
                              <a href="/notifications">Notifications</a>
                           </li>
                           <li>
                              <a href="/typography">Typography</a>
                           </li>
                           <li>
                              <a href="/grid">Grid</a>
                           </li>
                        </ul>
                        <!-- /.nav-second-level -->
                     </li>
                     <li>
                        <a href="#"><i class="fa fa-sitemap fa-fw"></i> Multi-Level Dropdown<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                           <li>
                              <a href="#">Second Level Item</a>
                           </li>
                           <li>
                              <a href="#">Second Level Item</a>
                           </li>
                           <li>
                              <a href="#">Third Level <span class="fa arrow"></span></a>
                              <ul class="nav nav-third-level">
                                 <li>
                                    <a href="#">Third Level Item</a>
                                 </li>
                                 <li>
                                    <a href="#">Third Level Item</a>
                                 </li>
                                 <li>
                                    <a href="#">Third Level Item</a>
                                 </li>
                                 <li>
                                    <a href="#">Third Level Item</a>
                                 </li>
                              </ul>
                              <!-- /.nav-third-level -->
                           </li>
                        </ul>
                        <!-- /.nav-second-level -->
                     </li>
                     <li>
                        <a href="#"><i class="fa fa-files-o fa-fw"></i> Sample Pages<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                           <li>
                              <a href="/blank">Blank Page</a>
                           </li>
                           <li>
                              <a href="/login">Login Page</a>
                           </li>
                        </ul>
                        <!-- /.nav-second-level -->
                     </li>
                  </ul>
                  <!-- /#side-menu -->
               </div>
               <!-- /.sidebar-collapse -->
            </div>
            <!-- /.navbar-static-side -->
         </nav>
         <div id="page-wrapper">
            <div class="row">
               <div class="col-lg-12">
                  <h1 class="page-header">Tables</h1>
               </div>
               <!-- /.col-lg-12 -->
            </div>
            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
               <div class="modal-dialog">
                  <div class="modal-content">
                     <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Order Details</h4>
                     </div>
                     <div class="modal-body">
                        ...
                     </div>
                     <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                     </div>
                  </div>
               </div>
            </div>
            <!-- /.row -->
            <div class="row">
               <div class="col-lg-12">
                  <div class="panel panel-default">
                     <div class="panel-heading">
                        Workorders
                     </div>
                     <!-- /.panel-heading -->
                     <div class="panel-body">
                        <div class="table-responsive">
                           <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                              <thead>
                                 <tr>
                                    <th>_id</th>
                                    <th>Phone #</th>
                                    <th>Sub Area</th>
                                    <th>Due By</th>
                                    <th>City</th>
                                    <th>State</th>
                                 </tr>
                              </thead>
                           </table>
                        </div>
                     </div>
                     <!-- /.panel-body -->
                  </div>
                  <!-- /.panel -->
               </div>
               <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <!-- /.row -->
            <div class="row">
               <div class="col-lg-12">
                  <div class="panel panel-default">
                     <div class="panel-heading">
                        Products
                     </div>
                     <!-- /.panel-heading -->
                     <div class="panel-body">
                        <div class="table-responsive">
                           <table class="table table-striped table-bordered table-hover" id="dataTables-example2">
                              <thead>
                                 <tr>
                                    <th>Product</th>
                                    <th>Category</th>
                                    <th>Price</th>
                                 </tr>
                              </thead>
                           </table>
                        </div>
                     </div>
                     <!-- /.panel-body -->
                  </div>
                  <!-- /.panel -->
               </div>
               <!-- /.col-lg-12 -->
            </div>
         </div>
         <!-- /#page-wrapper -->
      </div>
      <!-- /#wrapper -->
      <!-- Core Scripts - Include with every page -->
      <script src="jquery-1.10.2.js"></script>
      <script src="bootstrap.min.js"></script>
      <script src="jquery.metisMenu.js"></script>
      <!-- Page-Level Plugin Scripts - Tables -->
      <script src="jquery.dataTables.js"></script>
      <script src="dataTables.bootstrap.js"></script>
      <!-- SB Admin Scripts - Include with every page -->
      <script src="sb-admin.js"></script>
      <!-- -->
      <script src="mustache.js"></script>
      <!-- Page-Level Demo Scripts - Tables - Use for reference -->
      <script>
         $(document).ready(function() {
             $('#dataTables-example').dataTable( {
                 "processing": true,
                 "oScroller": {
                     "loadingIndicator": true
                 },
                 "oColVis": {
                   "activate": "mouseover",
                 },
                 "aaSorting": [[ 1, "asc" ]],
                 // "bServerSide": true,
                 // "bJQueryUI": true,
                 // "bStateSave": true,
                 "ajax": "/workordersjson",
                 "columns": [
                     { "data": "_id" },
                     { "data": "Phone #" },
                     { "data": "Sub Area" },
                     { "data": "Due By" },
                     { "data": "City" },
                     { "data": "State" }
                 ]
             } );
         } );

             $('#dataTables-example2').dataTable( {
                 "processing": true,
                 "oScroller": {
                     "loadingIndicator": true
                 },
                 "oColVis": {
                   "activate": "mouseover",
                 },
                 "aaSorting": [[ 1, "asc" ]],
                 // "bServerSide": true,
                 // "bJQueryUI": true,
                 // "bStateSave": true,
                 "ajax": "/productsjson2",
                 "columns": [
                     { "data": "value" },
                     { "data": "category" },
                     { "data": "price" },
                 ]
             } );



      </script>
      <script>
         $(document).ready(function(){
             $('#dataTables-example tbody').on('click', 'tr', function(event){
                     event.preventDefault();                    

                     var nTds = $('td', this);
                     //example to show any cell data can be gathered, I used to get my ID from the first coumn in my final code
                     var sBrowser = $(nTds[0]).text();
                     var sGrade = $(nTds[4]).text();
                     var dialogText="The info cell I need was in (col2) as:"+sBrowser+" and in (col5) as:"+sGrade+"" ;
                     var targetUrl = $(this).attr("href");
                     console.log(sBrowser);
                    $.getJSON('/workorder/'+sBrowser, function(data) {
                     console.log(data);
                     var template = "<table class=table><thead><tr><th>Line</th><th>Product</th><th>Status</thead><tbody></tbody<tr></tr>{{#lines}}<td>{{Line #}}</td><td>{{Product}}</td><td>{{Status}}</td><tr>{{/lines}}</table>";
                     var html = Mustache.to_html(template, data);
                     $('.modal-body').html(html);
                     $('#myModal').modal()
         });
         });
         })
      </script>
   </body>
</html>

1 个答案:

答案 0 :(得分:1)

确保将脚本放在每个脚本之后或单独的文件上,然后将它们按顺序放在html中。

 $(document).ready(function() {
                 $('#dataTables-example').dataTable( {
                     "processing": true,
                     "oScroller": {
                         "loadingIndicator": true
                     },
                     "oColVis": {
                       "activate": "mouseover",
                     },
                     "aaSorting": [[ 1, "asc" ]],
                     // "bServerSide": true,
                     // "bJQueryUI": true,
                     // "bStateSave": true,
                     "ajax": "/workordersjson",
                     "columns": [
                         { "data": "_id" },
                         { "data": "Phone #" },
                         { "data": "Sub Area" },
                         { "data": "Due By" },
                         { "data": "City" },
                         { "data": "State" }
                     ]
                 } );
             } );

                 $('#dataTables-example2').dataTable( {
                     "processing": true,
                     "oScroller": {
                         "loadingIndicator": true
                     },
                     "oColVis": {
                       "activate": "mouseover",
                     },
                     "aaSorting": [[ 1, "asc" ]],
                     // "bServerSide": true,
                     // "bJQueryUI": true,
                     // "bStateSave": true,
                     "ajax": "/productsjson2",
                     "columns": [
                         { "data": "value" },
                         { "data": "category" },
                         { "data": "price" },
                     ]
                 } );
 $('#dataTables-example tbody').on('click', 'tr', function(event){
                     event.preventDefault();                    

                     var nTds = $('td', this);
                     //example to show any cell data can be gathered, I used to get my ID from the first coumn in my final code
                     var sBrowser = $(nTds[0]).text();
                     var sGrade = $(nTds[4]).text();
                     var dialogText="The info cell I need was in (col2) as:"+sBrowser+" and in (col5) as:"+sGrade+"" ;
                     var targetUrl = $(this).attr("href");
                     console.log(sBrowser);
                    $.getJSON('/workorder/'+sBrowser, function(data) {
                     console.log(data);
                     var template = "<table class=table><thead><tr><th>Line</th><th>Product</th><th>Status</thead><tbody></tbody<tr></tr>{{#lines}}<td>{{Line #}}</td><td>{{Product}}</td><td>{{Status}}</td><tr>{{/lines}}</table>";
                     var html = Mustache.to_html(template, data);
                     $('.modal-body').html(html);
                     $('#myModal').modal()
         });
         });
         })
)}