Raihan
Raihan

Reputation: 145

how can we make a form in the website editable odoo 12? (edit/save in form using js/jquery)

I've created a form view in the website for the portal user. I'm fetching the datas from a model A.

What I'm trying to do here is, a user can see the already fetched data using (t-field in span) and the user needs to edit and save the resulting data needs to updated to the corresponding model. However when there is data fetched in the span field ,user can't edit the field and save

$('#edit_save').click(function(e) {
    e.preventDefault();
    editable = !editable;
    if (editable) {
      $(this).text('Save');
    } else {
    }
    ); 
    alert("Hello! I am an alert box!!"); $(this).text('Edit');
}

$('.input').each(function() {
var that = $(this);
if (editable) {
  that.addClass('hidden');
  $('<input value="' + that.text() + '">').insertAfter(that);
} else {
  var input = that.next();
  that.removeClass('hidden');
  that.text(input.val());
  input.remove();
}
})
})
.input {
  margin: 5px;
  position: relative;
}
<form class="form-horizontal" style="bg-light">
  <t t-set="card_body">
    <t t-foreach="health_record" t-as="hr">
      #health_record = request.env['model'].search([)]
      <div class="form-group">
        <t>
          <button id="edit_save" class="btn btn-primary">Edit</button>
          <button id="health_profile" type="submit" class="btn btn-primary">Health Profile</button>
        </t>
        <h3 style="padding-top: 30px; padding-bottom: 45px; text-align: center; font-family: 'sans-serif'; font-weight: bold;" class="text-secondary">HEALTH RECORD</h3>
        <div style="padding-bottom:1px;" class="col-md text-md-left">
          <label class="text-secondary"><strong>Name:</strong></label>
          <span t-field="hr.partner_id" class="input" title="Name" />
        </div>
        <div class="col-md text-md-left">
          <label class="text-secondary"><strong >Age:</strong></label>
          <span id="age" t-field="hr.age" class="input" title="Age" />
        </div>
    </t>
  </t>
</form>

Upvotes: 2

Views: 1393

Answers (1)

Swati
Swati

Reputation: 28522

You can add inputs for name and age next to span tag and hide them first when your page gets load . Then ,whenever your edit button gets clicked you can check if the text is Edit if yes you can show the inputs-box and hide span tag and change your edit button to save button .So , if user clicked on save button you can grab both input values and send it to backend using form submit or ajax call .

Nextly , when edit button gets clicked you can show cancel button(optional) as well so if user clicked on cancel button then no changes will be made and inputs will get hidden.

Demo Code :

$(".form-group input").hide() //hide inputs
//on click ofedit button
$(".edit_save").click(function() {
  var selector = $(this).closest(".form-group")
  var btnText = $(this).text();
  if (btnText === 'Edit') {
    $(this).text('Save');
    $(this).next("button").show(); //hide
    selector.find("form span").hide() //span hide
    selector.find("form input").show() //show inputs
  } else {
    $(this).text('Edit');
    $(this).next("button").hide();
    selector.find("form span").show()
    selector.find("form input").hide()

    //get values from input box which is edited
    var name = selector.find("input[name=name]").val();
    var age = selector.find("input[name=age]").val()
    //your ajax call put here to send both values and some id to identify record whwere to update...
    //put updated values in span again add this inside success fn of ajax call 
    selector.find("span.names").text(name)
    selector.find("span.ages").text(age)
    //or submit from here using $(this).closest("form").submit();
    //this will submit form with input value to your backed don't forget to add action to form also some hidden input to identify record.
  }



});

$('.cancel').click(function() {
  var selector = $(this).closest('form')
  //get span values 
  var names = selector.find("span.names").text()
  var ages = selector.closest('form').find("span.ages").text()

  $(this).hide(); //hide cancel buton
  $(this).prev(".edit_save").text('Edit'); //change text
  selector.find("span").show() //hide & Show
  selector.find("input").hide()
  selector.find("input[name=name]").val(names);
  selector.find("input[name=age]").val(ages)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<div class="form-group">
  <form class="form-horizontal" style="bg-light">
    <t>
      <button type="button" class="edit_save" class="btn btn-primary">Edit</button>
      <!--added this button-->
      <button class="cancel" type="button" class="btn btn-primary" style="display:none">Cancel</button>
      <button class="health_profile" type="submit" class="btn btn-primary">Health Profile</button>
    </t>
    <h3 style="padding-top: 30px; padding-bottom: 45px; text-align: center; font-family: 'sans-serif'; font-weight: bold;" class="text-secondary">HEALTH RECORD</h3>
    <div style="padding-bottom:1px;" class="col-md text-md-left">
      <label class="text-secondary"><strong>Name:</strong></label>
      <span t-field="hr.partner_id" class="input names" title="Name">Abc</span>
      <!--added input here you can use t-att-value="hr.partner_id" same for other fields-->
      <input type="text" name="name" value="Abc" />
    </div>
    <div class="col-md text-md-left">
      <label class="text-secondary"><strong >Age:</strong></label>
      <span t-field="hr.age" class="input ages" title="Age">20</span><input type="text" name="age" value="20" />
    </div>
  </form>
</div>
<div class="form-group">
  <form class="form-horizontal" style="bg-light">
    <t>
      <button type="button" class="edit_save" class="btn btn-primary">Edit</button>
      <button class="cancel" type="button" class="btn btn-primary" style="display:none">Cancel</button>
      <button class="health_profile" type="submit" class="btn btn-primary">Health Profile</button>
    </t>
    <h3 style="padding-top: 30px; padding-bottom: 45px; text-align: center; font-family: 'sans-serif'; font-weight: bold;" class="text-secondary">HEALTH RECORD</h3>
    <div style="padding-bottom:1px;" class="col-md text-md-left">
      <label class="text-secondary"><strong>Name:</strong></label>
      <span t-field="hr.partner_id" class="input names" title="Name">Mno</span><input type="text" name="name" value="Mno" />
    </div>
    <div class="col-md text-md-left">
      <label class="text-secondary"><strong >Age:</strong></label>
      <span t-field="hr.age" class="input ages" title="Age">23</span><input type="text" name="age" value="23" />
    </div>
  </form>
</div>
<div class="form-group">
  <form class="form-horizontal" style="bg-light">
    <t>
      <button type="button" class="edit_save" class="btn btn-primary">Edit</button>
      <button class="cancel" type="button" class="btn btn-primary" style="display:none">Cancel</button>
      <button class="health_profile" type="submit" class="btn btn-primary">Health Profile</button>
    </t>
    <h3 style="padding-top: 30px; padding-bottom: 45px; text-align: center; font-family: 'sans-serif'; font-weight: bold;" class="text-secondary">HEALTH RECORD</h3>
    <div style="padding-bottom:1px;" class="col-md text-md-left">
      <label class="text-secondary"><strong>Name:</strong></label>
      <span t-field="hr.partner_id" class="input names" title="Name">Xyz</span><input type="text" name="name" value="Xyz" />
    </div>
    <div class="col-md text-md-left">
      <label class="text-secondary"><strong >Age:</strong></label>
      <span t-field="hr.age" class="input ages" title="Age">10</span><input type="text" name="age" value="10" />
    </div>

  </form>
</div>

Note : You just need to add input with value i.e : t-att-value="yourvalue" or value="yourvalue" i have not use odoo before but one of them should work . Also, make sure to add hidden input inside form which will differentiate each datas i.e : some id which is unique for each record so that it would help you to update your data at backend.

Upvotes: 4

Related Questions