AnsweredAssumed Answered

How to add multiple line items for phone field?

Question asked by ashokinsugar on Aug 27, 2014
Latest reply on Jan 31, 2016 by damund
Hi, the below code is used for adding multiple phones similar to email, where user can add multiple phone if required or remove. 

In view.edit.php under display function. this will give the basic layout of edit view-

$phone_numbers = '<table ><tr><td><button onclick="javascript:add_phoneNumber()" id="add_email" type="button"><img alt="Array.LBL_ID_FF_ADD" src="themes/default/images/id-ff-add.png?v=jlL480K5YkTHsr6Wx8BATQ"></button></td></tr><tr><td><input type="text" name="phone_0" value="" size="30"/><input type="hidden" name="phone_count" id="phone_count" value="0"/></td></tr></table><table id="tbl_phone"></table>';

$this->ss->assign('PHONENUMBERS',$phone_numbers);


Then you need to assign this in metadata/editviewdefs.php in the form of customCode.

'customCode' => '{$PHONENUMBERS}',

then inculde the js file in metadata/editviewdefs as below-

'file' => 'custom/modules/Contacts/js/custom.js',

file: custom.js


var add_phoneNumber = function(){
var phone_count = parseInt($('#phone_count').val());
var incr_count  = phone_count + 1;
var select_phonetype = '<select id="sel_'+incr_count+'" name="sel_'+incr_count+'"><option value="Home">Home</option><option value="Office">Office Phone</option><option value="Mobile">Mobile</option><option value="Fax">Fax</option><option value="Alternate">Alternate</option><option value="Others">Others</option></select>';

str_append = '<tr><td>'+select_phonetype+' <input type="text" id = "phone_'+incr_count+'" name="phone_'+incr_count+'" value="" size="30"/> <button onclick="javascript:remove_phoneNumber(this)" id="'+incr_count+'" class="id-ff-remove" name="'+incr_count+'" type="button" tabindex="0"><img src="index.php?entryPoint=getImage&amp;themeName=Sugar5&amp;imageName=id-ff-remove-nobg.png"></button></td></tr>';

$("#tbl_phone").last().append(str_append);
$('#phone_count').val(incr_count);
}


var remove_phoneNumber = function(res){
var decr_count = parseInt($('#phone_count').val()) -1;
$(res).closest( 'tr').remove();
$('#phone_count').val(decr_count);
resetTheOrder();
}

function resetTheOrder(){
var input = $('#tbl_phone input').attr('name','');
var select_val = $('#tbl_phone select').attr('name','');
var button = $('#tbl_phone button').attr('name','');

$.each(input,function(index,value){
  var incr_val = index+1;
  $(value).attr('name','phone_'+incr_val).prop('id','phone_'+incr_val);
});

$.each(select_val,function(index,value){
  var incr_val = index+1;
  $(value).attr('name','sel_'+incr_val).prop('id','sel_'+incr_val);
});

$.each(button,function(index,value){
  var incr_val = index+1;
  $(value).attr('name',incr_val).prop('id',incr_val);
});
}


Then,

You will get all the values in $_REQUEST , save the way you want to in your table.

Outcomes