2017-01-03 4 views
0

여러 개의 열과 여러 개의 행이있는 테이블과 편집 버튼이 있습니다. 편집 버튼을 클릭하면 나머지 열을 편집 가능하게 유지하면서 MR_ID 및 MR_Name은 편집 할 수없는 2 개의 열을 만들고 싶습니다. 내 업데이트 기능에 부정적인 영향을 미치므로 .not()에서 멀리하고 싶습니다. 다른 방법으로이 작업을 수행 할 수 있습니까? 테이블의다른 사람을 편집 가능한 상태로 유지하면서 열을 편집 할 수 없게 만들기

HTML/PHP :

<table id="html_master" class="ui-widget ui-widget-content"> 
<thead> 
    <tr class="ui-widget-header"> 
    <td>ID</td> 
    <td>Vendor</td> 
    <td>Buyer ID</td> 
    <td>POC Name</td> 
    <td>POC Email</td> 
    <td>POC Phone</td> 
    <td>Edit</td> 
    </tr> 
</thead> 
<tbody> 


<?php 
    /* Foreach loop that brings in information to populate table */ 
    foreach ($dbh->query($sql) as $rows){ 
    ?> 
    <tr id="<?php echo intval ($rows['MR_ID'])?>"> 
     <td class="mr_id" id="mr_id-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td> 
     <td class="mr_name" id="mr_name-<?php echo intval ($rows['MR_ID'])?>" name="field" contenteditable="false"><?php echo $rows['MR_Name']?></td> 
     <td class="buyer_id" id="buy_id<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['Buyer_ID']?></td> 
     <td class="poc_n" id="poc_n-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>  
     <td class="poc_e" id="poc_e-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_E']?></td> 
     <td class="poc_p" id="poc_p-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_P']?></td> 
     <td><input type="button" class="edit" name="edit" value="Edit"> 
    </tr> 
<?php 
    } 
?> 
</tbody> 
</table> 

자바 스크립트 :

// ----- Edit Row ----- 

$(document).on("click", "#html_master .edit", function() { 
    var $this = $(this); 
    var tds = $this.closest('tr').find('td').filter(function() { 
    return $(this).find('.edit').length === 0; 
    }); 
    if ($this.val() === 'Edit') { 
    $this.val('Save'); 
    if($this.id != '.mr_id'){ 
     tds.prop('contenteditable', true); 
    } 
    } else { 
    var isValid = true; 
    var errors = ''; 
    $('#myDialogBox').empty(); 
    var elements = tds; 
    if (tds.find('input').length > 0) { 
     elements = tds.find('input'); 
    } 
    var dict = {}; 
    elements.each(function (index, element) { 
     var type = $(this).attr('class'); 
     var value = (element.tagName == 'INPUT') ? $(this).val() : $(this).text(); 
     console.log(type); 
     // ----- Switch statement that provides validation for each table cell ----- 
     switch (type) { 
     case "mr_id": 
      dict["MR_ID"] = value; 
      break; 
     case "mr_name": 
      if (value == value.match(/^[a-zA-Z\s]+$/)) { 
       dict["MR_Name"] = value; 
      } 
      break; 
     case "buyer_id": 
      if (!$.isNumeric(value)) { 
      isValid = false; 
      errors += "Please enter a valid Buyer ID\n"; 
      } 
      if (isValid) { 
       dict["Buyer_ID"] = value; 
      } 
      break; 
     case "poc_n": 
      if (value == value.match(/^[a-zA-Z\s]+$/)) { 
       dict["MR_POC_N"] = value; 
      break; 
      } 
      else { 
      isValid = false; 
      errors += "Please enter a valid Name\n"; 
      } 
      break; 
     case "poc_e": 
      if (value == value.match(/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/)) { 
       dict["MR_POC_E"] = value; 
      break; 
      } 
      else { 
      isValid = false; 
      errors += "Please enter a valid Email\n"; 
      } 
      break; 
     case "poc_p": 
      if (value == value.match('^[0-9()+/-]{10,}$')) { 
       dict["MR_POC_P"] = value; 
      break; 
      } 
      else { 
      isValid = false; 
      errors += "Please enter a valid Phone Number\n"; 
      } 
      break; 
     } 
    }) 
    if (isValid) { 
     console.log(dict); 
     $this.val('Edit'); 
     tds.prop('contenteditable', false); 
     var request = $.ajax({ 
      type: "POST", 
      url: "update-copy.php", 
      data: dict 
     }); 

     request.done(function (response, textStatus, jqXHR){ 
      if(JSON.parse(response) == true){ 
      console.log("row updated"); 
      } else { 
      console.log("row failed to updated"); 
      console.log(response); 
      console.log(textStatus); 
      console.log(jqXHR); 
      } 
     }); 

     // Callback handler that will be called on failure 
     request.fail(function (jqXHR, textStatus, errorThrown){ 
      // Log the error to the console 
      console.log(textStatus); 
      console.log(jqXHR); 
      console.error(
       "The following error occurred: "+ 
       textStatus, errorThrown 
      ); 
     }); 

     // Callback handler that will be called regardless 
     // if the request failed or succeeded 
     request.always(function() { 

     }); 
    } else { 
     alert(errors); 
    } 
    } 
}); 

답변

0

당신이 CSS 클래스를 생성하고 올바른 위치에 jQuery를 사용하여 추가 할 수 있습니다

.disable_td{ 
    background-color: #ddd; 
    cursor: not-allowed; 
} 

또는 당신이 그것을 숨기기 원한다면 사용

함수의 상단에

.disable_td{ 
     display:none; 
    } 
바로
$(document).on("click", "#html_master .edit", function() { 
    var $this = $(this); 

후 이러한 작업의 어느 쪽을

$(".mr_id").addClass("mr_id disable_td"); 
$(".mr_name").addClass("mr_name disable_td"); 
+0

을 추가합니다. 편집 버튼을 눌렀을 때만 CSS를 편집 할 수 있기 때문에 CSS가 작동하지 않는다고 생각합니다. – Rataiczak24

+0

답변을 어떻게 편집합니까? –

+0

편집 버튼을 누르면 코드가 추가되고 열이 여전히 편집 가능합니다. – Rataiczak24