Solved

When I clone row autocomplete is not working and when I postback it doesn't se the new row

Posted on 2015-01-09
4
355 Views
Last Modified: 2015-01-11
Hi again
I have a table with a row for entering an agents name and more details
</tr>        

                  @{ int i = 0;   

             <tr class="tr_clone">                                  

                <td class="td-label">
                     @Html.Label("Agent Name")
                </td>
                <td class="td-data" colspan="1">
                    @Html.TextBox("mytxtbox1",null, new { @class = "AgentName" })  
                   @* @Html.HiddenFor(model => model.merchantpercentages[1].AGENT_ID, new { @class = "Agent", @id="txtAgent1" })*@
                    @Html.TextBoxFor(model => model.merchantpercentages[i].AGENT_ID,new { @class = "Agent"})
                </td>
                 <td colspan="3">
                     @Html.TextBoxFor(model => model.merchantpercentages[i].FIRST_YEAR, new { @class = "year1"})
                     @Html.TextBoxFor(model => model.merchantpercentages[i].SECOND_YEAR, new { @class  "year2" })
                     @Html.TextBoxFor(model => model.merchantpercentages[i].THIRD_YEAR, new { @class = "year3" })
                     @Html.TextBoxFor(model => model.merchantpercentages[i].FOURTH_YEAR, new { @class = "year4" })
                     @Html.TextBoxFor(model => model.merchantpercentages[i].FIFTH_YEAR, new { @class = "year5" })
                 </td>   
                  <td>
                       <button type="button", id="Add", class="AddRow">Add</button>
                  </td>
                 </tr>
    i++;
                  }  

        </table>

Open in new window


and jquery which is supposed to clone the last row
 $('table').on("click", ".AddRow",CloneRow) 

    function CloneRow()
    {
        var clone = $(this).closest('.tr_clone').clone()
        $(this).closest('.AgentName').autocomplete('destroy')         
        clone.insertAfter(".tr_clone:last");   
        $(clone).find(".AddRow").remove();
        clone.closest('.AgentName').addClass('.AgentName')
 
    }

Open in new window


When I clone the last row the autocomplete does not work anymore. Also once I got the autocomplete to work, but I can't remember what I did, but when I pressed to save the data, when it was posted back it seemed like it didn't see the new row of data.
Any help is greatly appreciated,
Thanks in advance
0
Comment
Question by:Niall292
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 

Author Comment

by:Niall292
ID: 40541504
What would be the best way to go about this jquery or HTML helper. I have never created a HTML helper but I'm reading up about them presently
0
 

Author Comment

by:Niall292
ID: 40542458
Now I look at it with a fresher head (After going away from it for a day I can see so many reasons why it's not working I got the Jquery to work by changing the CloneRow function to

function CloneRow()
{
var clone = $(this).closest('.tr_clone').clone()
$(this).closest('.AgentName).autocomplete('destroy')
clone.insertAfter('tr_clone:last');
$(clone.find('AddRow').remove(); //removes the button from the cloned row.
$(clone).find('AgentName').val(); //Clear the agent name box in the cloned row; TODO clear all in row
ApplyAutoComplete('.AgentName') // Earlier destroyed autocomplete but now with a new row I want to activate it again

Open in new window


I am now working on getting it to see all rows to be saved, will let you know how I get on
0
 

Accepted Solution

by:
Niall292 earned 0 total points
ID: 40543458
I changed the jquery again, now it copies the last row and it saves all data to the Server.
$(this).closest('.AgentName').autocomplete('destroy')

        var merID = $('#txtMerID').val()       

        var itemindex= $("#tableAgent > tbody > tr").length

        var newItem = $("<tr><td><label>Agent Name</label></td><td><input type='text' id='myTextbox'" + itemindex + " class='AgentName' /><input id='hidMerchantID_[" + itemindex + "]' name='merchantpercentages[" + itemindex + "].MERCHANT_ID' class='hidMerID'/><input id='merchantpercentages_['" + itemindex + "']__AGENT_ID name='merchantpercentages[" + itemindex + "].AGENT_ID' class='Agent' type='hidden' /></td><td><input type='text' id='merchantpercentages_[" + itemindex + "]__FIRST_YEAR' class='year1' name='merchantpercentages[" + itemindex + "].FIRST_YEAR' /><input type='text' id='merchantpercentages_[" + itemindex + "]__Second_YEAR' class='year2' name='merchantpercentages[" + itemindex + "].SECOND_YEAR' /><input type='text' id='merchantpercentages_[" + itemindex + "]__THIRD_YEAR' class='year3' name='merchantpercentages[" + itemindex + "].THIRD_YEAR' /><input type='text' id='merchantpercentages_[" + itemindex + "]__FOURTH_YEAR' class='year4' name='merchantpercentages[" + itemindex + "].FOURTH_YEAR' /><input type='text' id='merchantpercentages_[" + itemindex + "]__FIFTH_YEAR' class='year5' name='merchantpercentages[" + itemindex + "].FIFTH_YEAR' /></td></tr>");

        $("#tableAgent").append(newItem);

        $('#tableAgent').last("tr").find(".hidMerID").val(merID)

        ApplyAutoComplete(".AgentName");   

Open in new window

Now when I actually figured it out It is pretty easy to explain. the newitem variable is the HTML for the new row making sure you put the name in for any fieldyou want to save to the database. The name should be the class name . Fieldname
0
 

Author Closing Comment

by:Niall292
ID: 40543460
I finally figured it out.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question