Update an object in the array using a modal form and display updated details

后端 未结 1 869
春和景丽
春和景丽 2021-01-27 10:27

I\'m trying to update an external object by clicking on the edit button. It brings up a pop-up, wherein I can enter the new information and it should then update the user accord

相关标签:
1条回答
  • 2021-01-27 10:40

    Alright, I found the problem. It was the variable index which I was using. It was being over-ridden I believe on trying submitting the form, which was causing the problem.

    var myData = [{
      company: "ABC",
      url: "www.abc.com",
      type: "internal"
    }, {
      company: "CDE",
      url: "www.cde.com",
      type: "internal"
    }, {
      company: "DEF",
      url: "www.def.com",
      type: "external"
    }, {
      company: "EFG",
      url: "www.efg.com",
      type: "internal"
    }, {
      company: "FGH",
      url: "www.fgh.com",
      type: "external"
    }];
    
    $('#createData').click(function() {
      createDisplay();
    });
    function createDisplay() {
      $('.container').empty();
      myData.forEach(function(obj) {
      $('.container').append(
        $('<div>').addClass('box').append(
        $('<label>').text('Company Website: '),
        $('<a>').addClass('compUrl').attr('href', obj.url).text(obj.company),
        obj.type == 'external' ? $('<br /><button>').addClass('edit-btn').text('Edit').attr({'data-toggle':'modal', 'data-target':'#updateData'}) : ''
        )
      )
      });
    }
    var currentIndex;
    $(document).on('click', '.edit-btn', function(){
      currentIndex = $(this).parents('.box').index();
    });
    $('#updateForm').submit(function(){
      updateData(currentIndex);
      return false;
    });
    
    function updateData(index) {
      companyName = $('#companyName').val();
      companyUrl = $('#companyUrl').val();
    
      var upObj = {
      company: companyName,
      url: companyUrl,
      type: 'external'
      }
      myData.splice(index, 1, upObj);
      createDisplay();
    }
    .box {
      height: 100px;
      background-color: skyblue;
      border: 1px solid black;
      margin-top: 5px;
    }
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="container">
    
    </div>
    
    <button id="createData">Create divs</button>
    <!-- Modal -->
    
    <div class="modal fade" id="updateData" tabindex="-1" role="dialog" aria-labelledby="update-data" aria-hidden="true">
      <div class="modal-dialog">
      <div class="modal-content">
        <!-- Modal Header -->
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">
          <span aria-hidden="true">&times;</span>
          <span class="sr-only">Close</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Update Data</h4>
        </div>
        <form id="updateForm">
        <!-- Modal Body -->
        <div class="modal-body">
          <div class="form-group">
          <label class="col-sm-4 control-label" for="companyName">Company Name</label>
          <div class="col-sm-8">
            <input type="text" class="form-control" id="companyName" placeholder="Company Name" required/>
          </div>
          </div>
          <div class="form-group">
          <label class="col-sm-4 control-label" for="companyUrl">Website</label>
          <div class="col-sm-8">
            <input type="url" class="form-control" id="companyUrl" placeholder="Company URL" required/>
          </div>
          </div>
        </div>
        <!-- Modal Footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">
          Close
          </button>
          <button class="btn btn-primary">
          Save changes
          </button>
        </div>
        </form>
      </div>
      </div>
    </div>

    0 讨论(0)
提交回复
热议问题