I have a table and I am making it editable. On double click on a cell I make the cell editable and clicking outside I revert it with the updated value.
tableCells.dblclick(function() {
cell=$(this)
value=cell.text();
editing=true;
rowIndex = cell.parent().index('table tbody tr');
tdIndex = cell.index('table tbody tr:eq(' rowIndex ') td');
switch(tdIndex) {
case 1:
case 2:
case 9:
cell.html('<input type="text" value="' value '">');
break;
case 3:
cell.html('<select name="dob" id="dob" class="form-control"><option value="1">Uomo</option><option value="2">Donna</option></select>');
break;
case 4:
let temp = value.split('/');
value=temp[2] '-' temp[1] '-' temp[0];
cell.html('<input type="date" value="' value '">');
break;
}
});
$(document).click(function(e) { //se clicco fuori dalla tabella
if(editing){
if (!tableCells.toArray().some(f => f.contains(e.target))) {
let row = cell.parent('tr').data('row'); //la riga da aggiornare nel db
updValue=cell.find('input').val();
if(e.target.cellIndex==4){
let temp=updValue.split('-');
updValue=temp[2] '/' temp[1] '/' temp[0];
}
console.log(updValue);
console.log('riga ' row);
cell.html(updValue);
editing = false;
}
}
})
this updValue=cell.find('input').val();
works fine for the fields where I have an input. But in one situation I replace the cell with a select. How can I add the select
situation to this code in the lightest way? I will always have only input or select only editing the table. In pseudocode I'd say .find('input'||'select')
. I don't want to have to add a class or a name attribute but to keep searching for the tag type (input or select)
I have tried with .find('input','select')
but i get undefined this way
CodePudding user response:
You can add classes to those inputs/selects and then cell.find('.myInput')
or use this, assume you have name
attribute cell.find('[name]')
Or else the solution is: cell.find('select, input')