2017-07-31 23 views
0

我有一張表,我需要檢查數據是否已存在於數據庫中。如果他們這樣做,一個警告圖標應該出現在他們各自的行上的行動欄下的垃圾桶圖標旁邊並顯示關於他們的適當信息。如果DB中存在數據,則顯示警報圖標 - jQuery AJAX

目前,我能夠返回我需要的ajax數據,如果我比較的數據已經存在於數據庫中。但我不會如何在各自的行上顯示圖標。

enter image description here

這是我返回的數據:

{"receive_array":[{"id":"77","batch_id":"45","courier_name":"","vendor_name":"","status":"stored","batch_no":"9","courier_tracking_no":"123"},"",{"id":"126","batch_id":"65","courier_name":"QW12","vendor_name":"Amazon","status":"itemized","batch_no":"18","courier_tracking_no":"QW11"}]} 

這裏是我的Ajax:

$.ajax({ 
     type: "POST", 
     url: window.base_url+'oss/admin/check_receive_data', 
     data: $.param($('form#receiving-form').serializeArray()), 
     dataType : 'JSON', 
     success: function (response) { 
      //THIS IS WHERE THE PROCESS SHOULD TAKE PLACE 
      $.each(response.receive_array, function(index, val) { 
      }); 
     }, 
      error: function (MLHttpRequest, textStatus, errorThrown) { 
      console.log("There was an error: " + errorThrown); 
     } 
    }); 

編輯:

HTML:

<table id="receiving-box-table" class="table table-hover table-bordered table-striped"> 
    <thead> 
     <tr> 
      <th>Courier Tracking #</th> 
      <th>Courier</th> 
      <th>Vendor</th> 
      <th>Action</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td><input type="text" form="receiving-form" class="form-control input-sm track_no" name="courier_tracking_no[]" id="courier_tracking_no_1" /></td> 
      <td><input type="text" form="receiving-form" class="form-control input-sm" name="courier_name[]" id="courier_name_1" onkeypress="if (event.keyCode == 13) {return false;}"/></td> 
      <td><input type="text" form="receiving-form" class="form-control input-sm" name="vendor_name[]" id="vendor_name_1" onkeypress="if (event.keyCode == 13) {return false;}"/></td> 
      <td class="box-action"><button class="btn btn-danger btn-xs clear-data" data-toggle="tooltip" data-placement="right" title="Clear input fields"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></td> 
     </tr> 
    </tbody> 
</table> 

(來自第一行中的行一邊被動態創建。)

任何幫助高度讚賞。謝謝! -Eli

+0

是你不知道如何顯示一個圖標問題或者你現在不知道如何將json中的值與行中的值進行匹配? – Difster

+0

將html粘貼到這裏 –

+0

@Difster我不知道如何在值匹配的同一行顯示圖標。 –

回答

1

您需要2路1中的數據進行匹配等是從DB像得到的迴應,

.... 
    success: function (response) { 
     // Change selectors as per you HTML design 
     $('table tr').each(function(index){ 
      var ctno=$(this).find('td:first input').val(); // get courier trancking 

      // check if ctno is present in response array or not 
      var arr = jQuery.grep(response.receive_array, function(n) { 
       return (n.courier_tracking_no === ctno); 
      }); 
      if(arr.length){ // if present then show error message 
       $('span.exists').show(); // let it be hidden by default 
      } 
     }); 

    }, 
.... 
+0

我嘗試使用3個數據只有2個副本,但所有的圖標仍然顯示。 https://snag.gy/jJ4SqE.jpg –

+0

我爲每個按鈕添加了一個ID來專門調用它們,因爲調用該類意味着調用具有相同類的所有內容。我只是想知道...我試圖提醒數據值,但是當我只輸入1個數據時,警報顯示兩次。第一個是未定義的,第二個是實際數據。 –

+0

你可以在'tr'中找到你的錯誤圖標,例如if(arr.length){$(this).find('span.exists')。show();}。同樣在成功回調中,首先隱藏所有存在的按鈕,以便只顯示現有的一次。 –