vendredi 7 août 2020

dataTable cell editable onclick and enter key pressed without button

I'm working on datatable edit function, but edit option should only be available on click on the cell and then press enter key, but what's happening is that if I clicked on multiple cell then press enter will allow all clicked cells to edit So I want to edit option for only last cell I clicked;

I have a double click function for the same table code:

$('#example5 tbody').on( 'click', 'tr', function (event) 
{
    //alert("on");
    // $(event.target).get(0).reset();
    var rowIndex = $(this).index(); 
           
    var col  = event.target;
    var colindex = col.cellIndex; 

     event.target.style.padding = "0px";
    event.target.style.boxSizing = "border-box";
      //event.target.style.height = "32px";
    event.target.style.lineHeight = "0";   

    var mainobj = $(this);
    var tdobj = event.target;
    var cell = $(event.target).get(0); // This is the TD you clicked
    console.log(cell);
    var cellobj = $(cell);

    var tdht = $(cell).height();
    var tdwdt = $(cell).width();
    var tdobj_val_old =  $(tdobj).text();
    tdobj_val = tdobj_val_old.replace(/"/g, "\'");
    var inptype = (colindex=="6") ? "text" : "text";
    var inpcls = (colindex=="6" || colindex=="8" || colindex=="10" || colindex=="12") ? "onlydecimalval" : "escdoubleltgt";
    var inpmax = (colindex=="6" || colindex=="10") ? 14 : 100;
            var bgcolor = (colindex=="0" || colindex=="3" || colindex=="6" || colindex=="8" ||colindex=="10" || colindex=="12" || colindex=="14") ? "#ffc8dc" : "#afe1e1";
            //alert(colindex);
    $(document).keydown(function(e)
    {
        if(e.keyCode==13)
        {
            // alert('hdeep');
            // console.log("single");

            if(colindex=="2") 
            { 
                //alert("hgg");
                var txthtml = '<input class="'+inpcls+'" id="imeditable" type="'+inptype+'" value="'+tdobj_val+'" style="height:'+tdht+'px;width:'+tdwdt+'px;position:relative;background-color:'+bgcolor+';" onblur="hmeditlosefocusfp(this,'+rowIndex+','+colindex+');" maxlength="'+inpmax+'" />';
            $(cell).addClass("editablecellcls");
            $(cell).html(txthtml);
            $("#imeditable").focus().select();
            }    
        }
    });
});


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire