Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:05:20+00:00 2026-06-08T15:05:20+00:00

I am currently trying to implement an Add functionality to my page. Currently, the

  • 0

I am currently trying to implement an Add functionality to my page. Currently, the add is already functioning but I have to refresh the page in order for the newly added row to appear on the table.

Please see my codes below:

This is how I generate my table:

<table class="webGrid" id="ProductsTable">
<tr>
    <td><strong><span>ProductID</span></strong></td>
    <td><strong><span>ProductName</span></strong></td>
    <td><strong><span>Price</span></strong></td>
    <td colspan="2"><strong><span>Action</span></strong></td>
</tr>

@foreach (var item in repository.GetAllProducts(ViewData["BranchCode"].ToString()))
{ 
<tr>
    <td><span class="ProductID">@item.ProductID</span></td>
    <td><span class="ProductName">@item.ProductName</span></td>
    <td><span class="Price">@item.Price</span></td>
    <td>@Html.ActionLink("Edit", "Edit", new { RecordID = item.RecordID }, new { @class = "editLink" })</td>
    <td>@Html.ActionLink("Delete", "Delete", new { RecordID = item.RecordID }, new { @class = "editLink" })</td>
</tr>
}

Currently, the edit and delete are already functioning well. Below is how I do the edit:

function update(data) {
if (data.Success == true) {
    var parent = linkObj.closest("tr");

    parent.find(".ProductID").html(data.Object.ProductID);
    parent.find(".ProductName").html(data.Object.ProductName);
    parent.find(".Price").html(data.Object.Price);
}
else {
    $("#update-message").html(data.ErrorMessage);
    $("#update-message").show();
}

}

Now I am trying to implement an add functionality which is almost the same as the edit jquery i am using. I have tried using the .append method unsuccessfully.


EDIT:

I have tried using the code below for the add. But it doesn’t seem to do anything. Or perhaps I’m doing something wrong:

    function add(data) {
    if (data.Success == true) {

        var rowTemplate = $("#rowTemplate").html();
        var tbl = document.getElementById("ProductsTable");
        var counter = $("#ProductsTable tr").length;
        data.Counter = counter;
        $("#ProductsTable").append(applyTemplate(rowTemplate, data));

    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}

function applyTemplate(template, data) {
    var str = template;
    if ($.isPlainObject(data)) {
        $.each(data, function (index, value) {
            var find = new RegExp("\\$1" + index, "ig");
            str = str.replace(/\$/g, "\$1");
            str = str.replace(find, value);
        });
    }
    return str;
}

It makes use of a row template such as the following:

<script type="text/x-template" id="rowTemplate">
        <tr><td><input type="text" id="txtName_$Counter" value="" /></td></tr>
    </script>

I just found this solution online but I can’t make it to work. I also tried the code below (i just made up based on the edit jquery I have):

    function add(data) {
    if (data.Success == true) {
        var parent = linkObj.closest("tr");
        parent.find(".ProductID").append(data.Object.ProductID);
        parent.find(".ProductName").append(data.Object.ProductName);
        parent.find(".Price").append(data.Object.Price);
    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}

EDIT:

Right now, this is how my jQuery looks like:

function add(data) {
    if (data.Success == true) {
        data = { Counter: 3 };
        $("#rowTemplate").tmpl(data).appendTo("#ProductsTable");
        $('#updateDialog').dialog('close');
    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}

and this is my template:

<script type="text/x-template" id="rowTemplate">
    <tr>
        <td><span class="ProductID"></span></td>
        <td><span class="ProductName"></span></td>
        <td><span class="Price"></span></td>
        <td>@Html.ActionLink("Edit", "_EditProduct", new { @class = "editLink" })</td>
        <td>@Html.ActionLink("Delete", "_DeleteProduct", new { @class = "editLink" })</td>
    </tr>
</script>

Thanks to Sir @Muhammad Adeel Zahid for helping me make the jQuery work to add rows. However, it only adds a new row to my table. What I need now is to make it add the values I have from the data object in the add function of the jQuery.

I have tried following the tutorial from THIS LINK but I can’t seem to make it work. My code is below:

function add(data) {
    if (data.Success == true) {
        var prod = $.map(data.results, function (obj, index) {
            return {
                ProductID:  obj.text,
                ProductName: obj.text,
                Price: obj.text
            };
        });

        prod = { Counter: 3 };
        $("#rowTemplate").tmpl(prod).appendTo("#ProductsTable");
        $('#updateDialog').dialog('close');
    }
    else {
        $("#update-message").html(data.ErrorMessage);
        $("#update-message").show();
    }
}

Thanks a lot for helping!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:05:22+00:00Added an answer on June 8, 2026 at 3:05 pm

    I think there is something wrong with your template code. Please try changing it to

    <script type="text/x-jquery-tmpl" id="rowTemplate">
            <tr><td><input type="text" id="txtName_${Counter}" value="" /></td></tr>
        </script>
    

    and then generate html from it like

    var obj = {Counter:3};
    $("#rowTemplate").tmpl(obj).appendTo("#ProductsTable");
    

    Edit
    First I thought you were using jquery template engine and my answer was based on that assumption. you can find how to use templating engine here. Please see that i have also edited the type field in <script type="text/x-jquery-tmpl" .... Import jquery template js file in your code and leave rest of the things as is. It should work then.
    Edit 2
    Ok that is a different template. Remember you must have unique id for each of your template. That template would look like

    <script type="text/x-jquery-tmpl" id="rowTemplate2">
        <tr>
            <td><span class="ProductID">${ProductId}</span></td>
            <td><span class="ProductName">${ProductName}</span></td>
            <td><span class="Price">${Price}</span></td>
            <td>@Html.ActionLink("Edit", "_EditProduct", new { @class = "editLink" })</td>
            <td>@Html.ActionLink("Delete", "_DeleteProduct", new { @class = "editLink" })</td>
        </tr>
    </script>
    

    Note how you add placeholders ${Variable} in the templates. Now when you need to use the template, you will need a json object with properties matching the variables used in the template. For example to use above template, I would do something like

    var obj2 = {ProductId:2,ProductName:'First Product', Price: 323};//note the properties of json and template vars match.
    $("#rowTemplate2").tmpl(obj2).appendTo("#somecontainer");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to implement a simple Add-In for InfoPath 2010 Filler/Editor mode, which
I am currently trying to implement a marker interface in c#. But I haven't
I am trying to add validation to a datagrid in XAML. Currently, I have
I am trying to add asynchronous output to a my program. Currently, I have
I am currently trying to implement a Custom SiteMap Provider. I have read several
I am currently trying to implement a graph on a website from a raw
I am currently trying to implement Conway's Game of Life in a Code, and
I'm currently trying to implement some kind of search system in a program of
I am currently trying to implement a PNG encoder in C++ based on libpng
I'm currently trying to implement a socket server that enables the clients to send

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.