【问题标题】:DotNetNuke, Ajax and jQuery: Hiding a table rowDotNetNuke、Ajax 和 jQuery:隐藏表格行
【发布时间】:2010-07-06 22:49:51
【问题描述】:

我为客户编写了一个 DotNetNuke 模块,允许他们从表中“删除”优惠券。当他们单击该链接时,将使用 jQuery 创建一个 Ajax POST,成功后应删除该行(或至少隐藏)并显示一条成功消息,并附加一个 CssClass。一切正常,减去删除行的部分。我在任何其他 ASP.NET Web 窗体/MVC 项目中都没有遇到过这个问题,只有 DotNetNuke。最后发生的是我的整个表被删除并显示成功消息。这是我的代码:

<script language="javascript" type="text/javascript">

jQuery.noConflict();
var deletingCouponID = null;

function DeleteCoupon(_CouponID) {

    deletingCouponID = _CouponID;

    jQuery.post(
        "mylink.aspx",
        { CouponID: _CouponID },
        function (data) {

            if (data.Response == "Success") {
                alert("#row" + deletingCouponID);

                jQuery("#tblCoupons tbody tr.row" + deletingCouponID).remove();
                jQuery("#divAjaxMsg").html("<p>" + data.Message + "</p>");
                jQuery("#divAjaxMsg").addClass("NormalRed");
            }
            else {
                jQuery("#divAjaxMsg").html("<p>" + data.Message + "</p>");
                jQuery("#divAjaxMsg").addClass("NormalRed");
            }
        },
        "json"
    );
}

和 HTML:

<div style="padding:1px">

<asp:Label runat="server" ID="lblMessage" ></asp:Label>

<div runat="server" id="divCouponList" >

    <div style="text-align: center">
        <h1>Coupon List</h1>
    </div>

    <div id="divAjaxMsg" />

    <table cellpadding="5px" id="tblCoupons">
        <thead>
            <tr>
                <th>Coupon ID</th>
                <th>Coupon Code</th>
                <th>Author</th>
                <th>Date Created</th>
                <th>Expiration Date</th>
                <th>Amount</th>
                <th>Min Purchase Amount</th>
                <th>Num Uses</th>
                <th>Max Uses</th>
                <th>Target User</th>
                <th>Target Product</th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
        <%
            string Sql = "SELECT * FROM MyTable WHERE Expired != 'True'";
            using (IDataReader Reader = DataProvider.Instance ().ExecuteSQL (Sql))
            {
                int Count = 0;
                while (Reader.Read ())
                {
                    ++Count;
        %>
                    <tr id="row<%= ((int)Reader["CouponID"]).ToString () %>">
                        <td nowrap="nowrap"><%= ((int)Reader["CouponID"]).ToString () %></td>
                        <td nowrap="nowrap"><%= Reader["CouponCode"] as string %></td>
                        <td nowrap="nowrap"><%= GetUserDisplayName ((int)Reader["AuthorID"]) ?? "Author Not Found" %></td>
                        <td nowrap="nowrap"><%= ((DateTime)Reader["DateCreated"]).ToShortDateString () %></td>
                        <td nowrap="nowrap"><%= Reader["ExpirationDate"] != DBNull.Value ? ((DateTime)Reader["ExpirationDate"]).ToShortDateString () : "Indefinite" %></td>
                        <td nowrap="nowrap"><%= Reader["Amount"] as string %></td>
                        <td nowrap="nowrap"><%= Reader["MinPurchase"] != DBNull.Value ? String.Format ("{0:C}", (decimal)Reader["MinPurchase"]) : "None" %></td>
                        <td nowrap="nowrap"><%= ((int)Reader["NumUses"]).ToString () %></td>
                        <td nowrap="nowrap"><%= Reader["MaxUses"] != DBNull.Value ? ((int)Reader["MaxUses"]).ToString () : "Unlimited" %></td>
                        <td nowrap="nowrap"><%= !String.IsNullOrEmpty (Reader["TargetUserEmail"] as string) ? Reader["TargetUserEmail"] as string : "None" %></td>
                        <td nowrap="nowrap"><%= Reader["TargetProductID"] != DBNull.Value ? (GetProductName ((int)Reader["TargetProductID"]) ?? "None") : "None" %></td>
                        <td nowrap="nowrap"><a href="<%= NewEditURL + "?CouponID=" + ((int)Reader["CouponID"]).ToString () %>">Edit</a></td>
                        <td nowrap="nowrap"><a href="javascript: DeleteCoupon(<%= ((int)Reader["CouponID"]).ToString () %>)">Delete</a></td>
                    </tr>
        <%
                }

                if (Count < 1)
                {
        %>
                    <tr>
                        <td colspan="10" style="text-align: center;">No coupons found</td>
                    </tr>
        <%
                }
            }
        %>
        </tbody>
    </table>
    <p>
        <a href="<%= NewEditURL %>">Create New Coupon</a>
    </p>
</div>

我确信我错过了(或搞砸了)一些愚蠢的事情,所以我认为再多看几眼可能会有所帮助。我不太喜欢编写 DNN 模块,所以这没有多大帮助!提前致谢!

吉姆

编辑2:感谢大家的帮助和想法!感谢大家花时间和精力帮助我。

编辑:这是来自 IE 的“之前和之后”标记。该行实际上并没有被删除。我可以忍受只是隐藏的行,所以用户不能点击编辑/删除按钮:

<table cellpadding="5px" id="tblCoupons">
        <thead>
            <tr>
                <th>Coupon ID</th>
                <th>Coupon Code</th>
                <th>Author</th>
                <th>Date Created</th>
                <th>Expiration Date</th>
                <th>Amount</th>
                <th>Min Purchase Amount</th>
                <th>Num Uses</th>
                <th>Max Uses</th>
                <th>Target User</th>
                <th>Target Product</th>
                <th></th>
                <th></th>
            </tr>
        </thead>
        <tbody>

                    <tr id="row8">
                        <td nowrap="nowrap">8</td>
                        <td nowrap="nowrap">E82O7KX</td>
                        <td nowrap="nowrap">SomeUser</td>
                        <td nowrap="nowrap">7/5/2010</td>
                        <td nowrap="nowrap">Indefinite</td>
                        <td nowrap="nowrap">100%</td>
                        <td nowrap="nowrap">$500.00</td>
                        <td nowrap="nowrap">0</td>
                        <td nowrap="nowrap">50</td>
                        <td nowrap="nowrap">None</td>
                        <td nowrap="nowrap">None</td>
                        <td nowrap="nowrap"><a href="somepage">Edit</a></td>
                        <td nowrap="nowrap"><a href="javascript: DeleteCoupon(8)">Delete</a></td>
                    </tr>

                    <tr id="row11">
                        <td nowrap="nowrap">11</td>
                        <td nowrap="nowrap">D2GRI</td>
                        <td nowrap="nowrap">SomeUser</td>
                        <td nowrap="nowrap">7/5/2010</td>
                        <td nowrap="nowrap">Indefinite</td>
                        <td nowrap="nowrap">$300</td>
                        <td nowrap="nowrap">None</td>
                        <td nowrap="nowrap">0</td>
                        <td nowrap="nowrap">Unlimited</td>
                        <td nowrap="nowrap">None</td>
                        <td nowrap="nowrap">None</td>
                        <td nowrap="nowrap"><a href="somepage">Edit</a></td>
                        <td nowrap="nowrap"><a href="javascript: DeleteCoupon(11)">Delete</a></td>
                    </tr>

        </tbody>
    </table>

【问题讨论】:

  • 它会删除所有行吗? string Sql = "SELECT * FROM MyTable WHERE 已过期!= 'True'"; Expired 是一个布尔值吗?您可能需要使用 WHERE Expired != 1?只是一个建议
  • 我的问题实际上是删除 HTML 表行,而不是 SQL 行。我很抱歉在没有具体说明的情况下折腾。我需要将 HTML 表格行从存在中删除/隐藏/删除/删除,但整个 HTML 表格正在消失。
  • 你能粘贴渲染的标记而不是服务器端视图
  • @redsquare,感谢您的所有帮助。我发布了呈现客户端的表格。您可能还想看另一部分吗?不幸的是,正如我之前所说,我无法发布整个内容,但如果有您认为值得一看的内容,我可能会从中切出更多内容。

标签: jquery ajax dotnetnuke ascx tablerow


【解决方案1】:

改变

jQuery("#tblCoupons tbody tr.row" + deletingCouponID).remove();

jQuery("#row" + deletingCouponID).remove();

也可以从您的函数中返回 false 或使用 event.preventDefault() 来阻止链接跟随。

【讨论】:

  • 我也试过了,它做同样的事情。实际上,这正是我最初拥有的,只是在阅读 StackOverflow 上的另一篇文章后才尝试发布的内容。
  • 在 firebug 中 console.log(jQuery("#row" + deletedCouponID).length) 给你什么?
  • 您的标记有效吗?尝试在validator.w3.org 验证它,如果 dom 搞砸了 jquery 选择器可能会出现问题
  • 结果我得到 '1'。
  • @jdangelo,所以 jquery 找到了元素。然后它应该删除该行。我认为优惠券ID是主键?如果可以的话,粘贴渲染的标记
【解决方案2】:

为你的头添加一个标题类并使用下面的代码

$("#myTable tr:not(.header)").remove();
$("#myTable").append(table);

【讨论】:

  • 表格的头部和主体在删除后保持不变,并且在呈现的标记中仍然可用。他们只是没有被渲染。所以,是表格没有显示,而不是实际被删除。
【解决方案3】:

您确定优惠券 ID 设置正确吗?如果不是因为您的代码使用该 id 作为删除内容标准的一部分,那将是有意义的。如果它不存在,那么它基本上会告诉表删除所有行。下面基本上是未设置时的样子。

jQuery("#tblCoupons tbody tr.row").remove();

这会删除 tbody 中的所有行。

你可以在函数运行后使用firebug或其他一些动态源生成器查看页面源,看看是整个表被删除了还是只删除了所有行?

附言我写了很多 DNN 模块,似乎很难相信这会与 DNN 本身有关,而不仅仅是一些疏忽。虽然我不是说不可能。

【讨论】:

  • 优惠券 ID 设置正确(我查看了 IE 和 Firefox 中呈现的标记)。我的行被正确命名,并且 Ajax 调用成功地删除了数据库中的优惠券。这只是客户端的渲染问题。我在创建的非 DNN 页面中使用了我的代码,以确保它可以正常工作。在进行 Ajax 调用后,我查看了呈现的标记,并且表(thead、tbody 和所有行)都仍然存在,减去了已删除的行。
  • 您可以将该页面作为示例发布吗?
  • 我在原始问题中添加了一些标记。即使在删除之后,它也会吐出同一张表。正如我在上面发布的那样,我会很高兴至少隐藏这个该死的东西。现在,“它有效”,但没有达到预期。
猜你喜欢
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
相关资源
最近更新 更多