Search code examples
asp.net-mvc-3jqueryjquery-templates

Grid generated with JQuery template need to reset using Ajax not working


Sometime working and sometime not.

I am trying to generate Grid with the help of JQuery Template via Ajax once record is added or deleted. In js file

$('.gridRow').remove();

is not working properly. Someone tell me how to reset grid to fill it again. Below is the code.

JS File

var ReloadGrid = (function(){
            $.getJSON("/HeaderMenu/GetHeaderGrid", function(data) {
                $('.gridRow').remove();
                (data.length <= 0) ? $("#gridBtn").hide() : $("#gridBtn").show();
                for (var i=0; i<data.length; i++) { data[i].num = i+1; }
                $('#gridTemplate').tmpl(data).appendTo('table.gridTable > tbody');
            });
 });

on MVC3 cxhtml page

<script id="gridTemplate" type="text/x-jquery-tmpl">
    <tr class="gridRow">
        <td class="cellTd ">
            <input type="checkbox" id="deleteCb" />
            <input type="hidden" id="Id_ + ${num}" class="idField" value="${Id}" />
        </td>
        <td class="cellTd">
            <input id="index" name="index" class="numberField" type="text" value="${IndexOrder}" />
        </td>
        <td class="cellTd">${DisplayName}</td>
        <td class="cellTd ">${UrlName}</td>
        <td class="cellTd ">
            <input type="checkbox" id="activeCb" {{if Active}} checked{{/if}} />
        </td>
    </tr>

</script>

<div class="gridDiv">
<table class="gridTable" cellspacing="0" cellpadding="0">
    <tbody>
        <tr class="gridTitleRow">
            <td class="iconLink width36">Delete</td>
            <td class="iconLink width60">Sort Order</td>
            <td class="iconLink widthAuto">Display Name</td>
            <td class="iconLink widthAuto">Url Name</td>
            <td class="iconLink widthAuto">Active</td>
        </tr>
    </tbody>
</table>
</div>

Solution

  • I usually empty the wrapper instead of the row.

    $('table.gridTable > tbody').empty();
    

    But for that to work you'd have to change your table to use thead

    <table class="gridTable" cellspacing="0" cellpadding="0">
        <thead>
            <tr class="gridTitleRow">
                <th class="iconLink width36">Delete</th>
                <th class="iconLink width60">Sort Order</th>
                <th class="iconLink widthAuto">Display Name</th>
                <th class="iconLink widthAuto">Url Name</th>
                <th class="iconLink widthAuto">Active</th>
            </tr>
        <thead>
        <tbody>
        </tbody>
    </table>