2013-05-13 50 views
0

這是我的代碼:保存數據時它僅存儲第一迭代值

Form<Tenantitem> tenantitemForm = form(Tenantitem.class).bindFromRequest(); 
String[] itemid = request().body().asFormUrlEncoded().get("idd"); 
String[] postAction = request().body().asFormUrlEncoded().get("action"); 

if (postAction == null || postAction.length == 0) { 
    return badRequest("You must provide a valid action"); 
} else { 
    String action = postAction[0]; 
    if ("Add".equals(action)) { 
     for (String item: itemid){ 
      tenantitemForm.get().name = Tenantitem.findById(item).name; 
      tenantitemForm.get().description = Tenantitem.findById(item).description; 
      tenantitemForm.get().image_url = Tenantitem.findById(item).image_url; 
      tenantitemForm.get().price = Tenantitem.findById(item).price; 
      tenantitemForm.get().tenant_id = tenantitemForm.get().tenant_id; 
      tenantitemForm.get().tenant_location_id = tenantitemForm.get().tenant_location_id; 
      tenantitemForm.get().tenant_page_id = tenantitemForm.get().tenant_page_id; 

      tenantitemForm.get().save(); 

      System.out.println("name tenant_id"+tenantitemForm.get().name); 
      System.out.println("name tenant_location_id"+tenantitemForm.get().tenant_id); 
      System.out.println("name tenant_page_id"+tenantitemForm.get().tenant_page_id); 
     } 


     return redirect(
      routes.Project.pts(loc_id,id) 
     ); 
    } else if ("Cancel".equals(action)) { 
     System.out.println("Delete ID:"+id); 
     return ok("deleted"); 
    } else { 
     return badRequest("This action is not allowed"); 
    } 
} 

我views.scala:

@helper.form(routes.Project.addexist(loc_id,pageid,id,pageid)){ 
     <div class="row-fluid" > 
      <div class="span3"> 

      </div> 
      <div class="span9"> 
       <div class="form-inline" id="options1"> 
        <input type="button" class="btn" id="togglein" value="SelectAll" onclick="do_thisin()"/> 
        <label class="control-label offset1"> 
         Filter By Name: 
        </label> 
        <input class="span3" type="text" placeholder="Enter name"> 
       </div> 
       <ol id="selectable1"> 


        <input type="checkbox" id="selectall" > 
        <input type="hidden" name="tenant_id" value="@id"/> 

        <input type="hidden" name="tenant_location_id" value="@loc_id"/> 

        <input type="hidden" name="tenant_page_id" value="@pageid"/> 


        @for(it <- item){ 
        @if(it.tenant_location_id == locid){ 
         <li> 
          <div class="well"> 
           <div class="row-fluid"> 
            <div class="span1"> 
             <input name="idd" class="case" value="@it.id" type="checkbox"> 
            </div> 
            <div class="span5"> 
             <img class="img-polaroid span7" src="http://p.imgci.com/db/PICTURES/CMS/128400/128483.1.jpg"> 
            </div> 
            <dl> 
             <dt> 
             <h4>Name:</h4> 
             </dt> 
             <dd> 
              <input type="hidden" name="name" value="@it.name"/> 
              <h5>@it.name</h5> 
             </dd> 
             <dt> 
             <h4>Price:</h4> 
             </dt> 
             <dd> 
              <input type="hidden" name="price" value="@it.price"/> 
              <h5>@it.price</h5> 
              <input type="hidden" name="description" value="@it.description"/> 
             </dd> 
            </dl> 
           </div> 
          </div> 
         </li> 

        <hr> 
        } 
        } 

       </ol> 
      </div> 
     </div> 
     <div class="row-fluid"> 
      <div class="form-inline"> 
       <div class="span5 offset6" > 
        <input type="submit" name="action" class="btn" value="Add"/> 

        <input type="submit" name="action" class="btn " value="Cancel"> 
       </div> 
      </div> 
     </div> 
     } 

我的功能性: @views: 步驟:1 - >我需要使用複選框選擇多個項目。 步驟:2 - >我必須得到tenant_id,tenant_location_id,tenant_page_id和item_id。

@controller: step:3 - >我需要從請求中使用綁定來獲取值,我必須使用item_id找到項目名稱,描述,價格並從@model獲取。 步驟:4 - >最後我必須存儲選定的值(根據複選框的長度)。

當保存數據時,它只存儲第一個迭代值,而不存儲剩餘的選定值。我不知道究竟發生了什麼,而且我也檢查了數據正在接收並且正在節省一次。

感謝您的幫助。

+0

什麼是你想幹什麼,朋友?你可以解釋嗎?看起來這個聲明'表格 tenantitemForm = form(Tenantitem.class).bindFromRequest();'是無用的,因爲你想覆蓋你的迭代值。並且我總是不經常像'tenantitemForm.get()'和'Tenantitem.findById(item)''調用類似的東西,我建議將該值存儲在其他變量中。 – 2013-05-14 01:32:46

+0

@Wayan Wiprayoga我需要根據我在VIEWS.html中的選擇保存多次。你有任何其他建議嗎?我也需要一些表單值來存儲到數據庫中,這就是爲什麼我使用綁定'.bindFromRequest();'。 – joan 2013-05-14 09:02:49

+0

你可以發表你的觀點,以便我可以更加了解你想做什麼?所以,請通過解釋它來更新你的帖子,朋友。 – 2013-05-14 10:03:24

回答

0

此代碼也許對你一個提示:

Form<Tenantitem> tenantitemForm = form(Tenantitem.class).bindFromRequest(); 
Tenantitem tenantItem = tenantitemForm.get(); // bind as Tenantitem object 

String[] itemid = request().body().asFormUrlEncoded().get("idd"); 
String[] postAction = request().body().asFormUrlEncoded().get("action"); 

if (postAction == null || postAction.length == 0) { 
    ... // your code 
} else { 
    ... // your code 
    if ("Add".equals(action)) { 
     // declare new variable to store the data submitted 
     Tenantitem tenantToBeSaved; 

     for (String item: itemid) { 
      // asign to an Item object? 
      tenantToBeSaved = new Tenantitem(); 
      Item item_assigned = Tenantitem.findById(item) 
      tenantToBeSaved.name = item_assigned.name; 
      tenantToBeSaved.description = item_assigned.description; 
      tenantToBeSaved.image_url = item_assigned.image_url; 
      tenantToBeSaved.price = item_assigned.price; 
      tenantToBeSaved.tenant_id = tenantItem.tenant_id; 
      tenantToBeSaved.tenant_location_id = tenantItem.tenant_location_id; 
      tenantToBeSaved.tenant_page_id = tenantItem.tenant_page_id; 

      tenantToBeSaved.save(); 

      ... // rest of your code 
     } 
    } 
} 
+0

感謝buddy其工作,它更好地創建對象,並分配給它的權利。謝謝你的時間。 – joan 2013-05-14 11:47:02

+0

是的朋友。項目祝你好運! – 2013-05-14 11:51:08

+0

感謝哥們,很高興與你討論。 – joan 2013-05-14 12:01:05