将数据添加到表

时间:2019-04-04 15:11:45

标签: javascript jquery html css

我有UI的屏幕快照,有桌子外的电影列表,我想创建一个按钮,该按钮将显示下表中的所有电影,我有css html和javascript代码

My UI

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
<link href="mystyle.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<script src="execute.js"></script>
  <title>My Movies</title>
  <header>
    <h1>Movies</h1>
  </header>
  <table class="table table-bordered table-hover" width="100%">
  <thead style="background-color:#ddd;" class="table-borderless">
    <body>
      <tr>
        <th></th>
        <th></th>
      <th>idmovielist</th>
      <th>name</th>
      <th>thumnail_path</th>
      <th>description</th>
      <th>year_released</th>
      <th>language_released</th>
      <th>Action</th>
      <center>
        <ul id = "movielist"></ul>
         <button id = "showMovies" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">All Movies</button>
     </center>
      </tr>
  </thead>
  <tbody>
    <tr>
    <td align="center"></td>
    <td><td>
     <td></td>
     <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td>
     <button id = "movieAdded" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
    Add
    </button>
    <button button id = "deleteMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
    Delete
  </button>
    <button  button id = "updatedMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
    Edit
    </button>
  </td>
</tr>
    </tbody>
    <tbody>
      <tr>
      <td align="center"></td>
      <td><td>
       <td></td>
       <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td>
       <button id = "movieAdded" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Add
      </button>
      <button button id = "deleteMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Delete
    </button>
      <button  button id = "updatedMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Edit
      </button>
    </td>
  </tr>
    </tbody>
    <tbody>
      <td align="center"></td>
      <td><td>
       <td></td>
       <td> </td>
        <td></td>
        <td></td>
        <td></td>
        <td>
       <button id = "movieAdded" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Add
      </button>
      <button button id = "deleteMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Delete
    </button>
      <button  button id = "updatedMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Edit
      </button>
    </td>
    </tbody>
    <tbody>
      <td align="center"></td>
      <td><td>
       <td></td>
       <td> </td>
        <td></td>
        <td></td>
        <td></td>
        <td>
       <button id = "movieAdded" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Add
      </button>
      <button button id = "deleteMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Delete
    </button>
      <button  button id = "updatedMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Edit
      </button>
    </td>
    </tbody>
    <tbody>
      <td align="center"></td>
      <td><td>
       <td></td>
       <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td>
       <button id = "movieAdded" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Add
      </button>
      <button button id = "deleteMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Delete
    </button>
      <button  button id = "updatedMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Edit
      </button>
    </td>
    </tbody>
    <tbody>
      <td align="center"></td>
      <td><td>
       <td></td>
       <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td>
       <button id = "movieAdded" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Add
      </button>
      <button button id = "deleteMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Delete
    </button>
      <button  button id = "updatedMovie" type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
      Edit
      </button>
    </td>
    </tbody>
    </table>                <!-- Modal -->
  <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          ...
        </div>
        <div class="container">
          <form id="newForm">
              <div class="form-group row">
                <label for="idmovielist" class="col-sm-2 col-form-label">idmovielist</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="intNum" placeholder="idmovielist">
                  </div>
              </div>
              <div class="form-group row">
                <label for="name" class="col-sm-2 col-form-label">name</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="name" placeholder="name">
                  </div>
              </div>
              <div class="form-group row">
                <label for="thumnail_path" class="col-sm-2 col-form-label">thumnail_path</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="thumnail_path" placeholder="thumnail_path">
                  </div>
              </div>
              <div class="form-group row">
                <label for="description" class="col-sm-2 col-form-label">description</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="description" placeholder="description">
                  </div>
              </div>
              <div class="form-group row">
                <label for="year_released" class="col-sm-2 col-form-label">year_released</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control" id="year_released" placeholder="year_released">
                  </div>
              </div>
CSS
body {
  background-color: rgb(10, 200, 196);
  font-family: sans-serif
}
  h1 {
    text-align: center;
    font-family: arial;
    color: #5a5a5a;
  }

  ul {
    display: flex;
    list-style:none;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content:center;
    flex-basis: 80%;
  }

JS

$(function(){
  const $movielist = $('#movielist');
  const $idmovielist = $('#idmovielist');
  const $name = $('#name');
  const $thumnail_path = $('#thumnail_path');
  const $description = $('description');
  const $language_released = $('language_released');
  const $year_released = $('year_released');
 $("#showMovies").click(function(){
$.ajax({
  method:"GET",
  url: "http://localhost:3000/movielist",
  dataType: "json",
  success: function (response) {
       $.each(response, function(i, movie) {
         $movielist.append('<td>idmovielist: ' + movie.idmovielist + ',name:' + movie.name + ', thumnail_path: ' +
       movie.thumnail_path + ', description: ' + movie.description + ', year_released: ' + movie.year_released +
       ', language_released: ' + movie.language_released + '</td>');
       });
      }
  });
  });
  $("#movieAdded").click(function() {
    const movies = {
      idmovielist: $idmovielist.val(),
      name: $name.val(),
      thumnail_path: $thumnail_path.val(),
      description: $description.val(),
      year_released: $year_released.val(),
      language_released: $language_released.val(),
    };
    $.ajax({
      method:"POST",
      url: "http://localhost:3000/movielist/addMovie",
      data: movies,
      success: function (newMovie) {
             $movielist.append('<li>idmovielist: ' + newMovie.idmovielist + ', name:' + newMovie.name + ', thumnail_path: ' +
           newMovie.thumnail_path + ', description: ' + newMovie.description + ', year_released: ' + newMovie.year_released +
           ', language_released: ' + newMovie.language_released + '</li>');
          }
      });
  });
  $("#deleteMovie").click(function() {
    const movies = {
      idmovielist: $idmovielist.val(),
      name: $name.val(),
      thumnail_path: $thumnail_path.val(),
      description: $description.val(),
      year_released: $year_released.val(),
      language_released: $language_released.val(),
    };
    $.ajax({
      method:"DELETE",
      url: "http://localhost:3000/movielist/3",
      data: movies,
      success: function (newMovie) {
             $movielist.append('<li>idmovielist: ' + newMovie.idmovielist + ', name:' + newMovie.name + ', thumnail_path: ' +
           newMovie.thumnail_path + ', description: ' + newMovie.description + ', year_released: ' + newMovie.year_released +
           ', language_released: ' + newMovie.language_released + '</li>');
          }
      });
  });
  $("#updatedMovie").click(function() {
    const movies = {
      idmovielist: $idmovielist.val(),
      name: $name.val(),
      thumnail_path: $thumnail_path.val(),
      description: $description.val(),
      year_released: $year_released.val(),
      language_released: $language_released.val(),
    };
    $.ajax({
      method:"PUT",
      url: "http://localhost:3000/movielist/update/2",
      data: movies,
      success: function (newMovie) {
             $movielist.append('<li>idmovielist: ' + newMovie.idmovielist + ', name:' + newMovie.name + ', thumnail_path: ' +
           newMovie.thumnail_path + ', description: ' + newMovie.description + ', year_released: ' + newMovie.year_released +
           ', language_released: ' + newMovie.language_released + '</li>');
          }
      });
  });

});

我已经在上面设置了我的代码,但是每当我执行操作时,数据都显示在表外部,我是否必须使用html对其进行硬编码

0 个答案:

没有答案