2015-12-01 92 views
0

我是一名初學者,在紅寶石軌道上進行開發。 我想要做的是:從數據庫搜索記錄並將它們添加到列表中

  1. 創建一個輸入搜索來搜索產品。
  2. 然後,我點擊一個提交按鈕,這個產品將顯示在下面的列表中。
  3. 我搜索另一個產品,它將被添加到我的清單。
  4. 最後,我會列出搜索到的產品。

<script type="text/javascript"> 
 
    
 
var foodList = []; 
 

 
function addToFood() { 
 
    var addFood = document.getElementById('addFood').value; 
 
    var addQuantity = document.getElementById('addFood').value; 
 

 
    foodList.push(addFood + " " + addQuantity); 
 

 
    
 
    for (i = 0; i < foodList.length; i++) \t { 
 
     var newFood = "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>"; 
 
    }; 
 
    document.getElementById('foods').innerHTML += newFood; 
 

 
    document.getElementById('addQuantity').value = ''; 
 
    document.getElementById('addFood').value = ''; 
 

 
} 
 

 

 
function removeRecord (i) { 
 
    foodList.splice(i, 1); 
 
    var newFood = ""; 
 
    // re-display the records from foodList the same way we did it in addToFood() 
 
    for (var i = 0; i < foodList.length; i++) { 
 
     newFood += "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>"; 
 
    }; 
 
    document.getElementById('foods').innerHTML = newFood; 
 
} 
 

 
</script>
<%= form_tag akala_health_path, :method => 'get', :id => "products_search" do %> 
 
    <p> 
 
    Product name 
 
    <%= text_field_tag :search, params[:search] , :id => "addFood"%></br> 
 
    <%= submit_tag "Search", :name => nil %> 
 
    </p> 
 
    
 
<% end %> 
 
    
 
<table id="products"> 
 
    <tr> 
 
    <th>Product name</th> 
 
    <th>Product code</th> 
 
    <th>Product quantity</th> 
 
    <th></th> 
 
    </tr> 
 
    <% if @products.count == 0 %> 
 
    <% else %> 
 
    <% @products.each do |product| %> 
 
     <tr> 
 
     <td id="addFood"><%= product.product_name %></td> 
 
     <td id="addFood"><%= product.product_code %></td> 
 
     <td><input type="text" value="" /></br></td> 
 
     <td><input type="submit" value="Ajouter" onClick="addToFood();"> </td> 
 
     </tr> 
 
    <% end %> 
 
    <% end %> 
 
</table> 
 
<%= will_paginate @products%> 
 
<!-- The list of food is displayed in the following div -->  
 
<div id="foods"></div>

我怎樣才能做到這一點?請幫幫我。

回答

相關問題