I have a detail view for my object. There is only one field available for editing. When I click on the Edit button I want to hide text for field and show input with the OK and Cancel button.
// create kendo buttons
$('#editGrnz').kendoButton({
icon: "k-icon k-i-pencil",
});
$('.rt-field-cancel-button').kendoButton({
icon: "k-icon k-i-cancel",
});
$('.rt-field-ok-button').kendoButton({
icon: "k-icon k-i-tick",
});
$(document).on('click', '#editGrnz', function(e) {
var $editButton = $(this),
$parentContainer = $(this).parent(),
$confirmEditButton = $parentContainer.find('.rt-field-ok-button'),
$cancelEditButton = $parentContainer.find('.rt-field-cancel-button'),
$editInput = $parentContainer.find('.rt-field-edit-input'),
$textContainer = $parentContainer.parent().find('.rt-detail-field');
$editButton.hide();
$textContainer.hide();
$confirmEditButton.show();
$cancelEditButton.show();
$editInput.show();
});
$(document).on('click', '.rt-field-ok-button', function(e) {
var $confirmEditButton = $(this),
$parentContainer = $(this).parent(),
$cancelEditButton = $parentContainer.find('.rt-field-cancel-button'),
$editButton = $parentContainer.find('.rt-field-edit-button'),
$editInput = $parentContainer.find('.rt-field-edit-input'),
$textContainer = $parentContainer.parent().find('.rt-detail-field'),
id = $('.rt-content > #trafficId').val(),
newTransportNumber = $editInput.val(),
fixationDate = $('.rt-content > #fixationDate').val();
//ajax call to save address
$editButton.show();
$textContainer.html(newTransportNumber);
$textContainer.parent().removeClass('text-muted');
$textContainer.show();
$confirmEditButton.hide();
$cancelEditButton.hide();
$editInput.hide();
});
$(document).on('click', '.rt-field-cancel-button', function(e) {
var $cancelEditButton = $(this),
$parentContainer = $(this).parent(),
$confirmEditButton = $parentContainer.find('.rt-field-ok-button'),
$editButton = $parentContainer.find('.rt-field-edit-button'),
$textContainer = $parentContainer.parent().find('.rt-detail-field'),
$editInput = $parentContainer.find('.rt-field-edit-input');
$editButton.show();
$textContainer.show();
$confirmEditButton.hide();
$cancelEditButton.hide();
$editInput.hide();
});
This code is located in my view file. Now I have another view when will be the same logic. I don't want to copy this code. How to refactor it? I use require.js
and may create a module for this functionality.
This is the working demo