3

こんにちは、MVC 3 の 1 つのパーシャルに 2 つのデータテーブルがあります。1 つは Table_1 を表示し、もう 1 つは Table_2 を表示します。データテーブル 1 (行) をダブルクリックすると、Datatables 2 formAddNewRow がポップアップする機能が必要です。これを行う方法はありますか?さらに、Datatables 1 ID がポップアップ フォームに送信されます。

現在、私の部分的なビューにはこれがあります:

<script type="text/javascript">
    $(document).ready(function () {
        $('#myItemPrice').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": '../AjaxItemPriceProvider?id=@ViewBag.ID',
            "sPaginationType": "full_numbers",
            "aoColumns": [
                                { "sName": "ID",
                                    "bSearchable": false,
                                    "bSortable": false,
                                    "bVisible": true
                                },
                                { "sName": "PDSID",
                                    "bSearchable": false,
                                    "bSortable": false,
                                    "bVisible": false
                                },
                                { "sName": "PID" },
                                { "sName": "EffDate" },
                                { "sName": "ExpDate" },
                                { "sName": "G140" },
                                { "sName": "AccID" },
                                { "sName": "CCID" }

                            ]
        });
    });

    $(document).ready(function () {
        $('#myTierPrice').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": '../AjaxTierPriceProvider?id=@ViewBag.ID',
            "sPaginationType": "full_numbers",
            "aoColumns": [
                                { "sName": "ID",
                                    "bSearchable": false,
                                    "bSortable": false,
                                    "bVisible": false
                                },
                                { "sName": "IPFID",
                                    "bSearchable": false,
                                    "bSortable": false,
                                    "bVisible": false
                                },
                                { "sName": "Quantity" },
                                { "sName": "Amount" },
                                { "sName": "Maximum Discount Percentage" },
                                { "sName": "Maximum Discount Amount" },
                                { "sName": "Tax 1" },
                                { "sName": "Tax 2" },
                                { "sName": "Commission" }

                            ]
        }).makeEditable({
            sUpdateURL: "../TierPriceUpdate",
            sAddURL: "../TierPriceAdd",
            sDeleteURL: "../TierPriceDelete"
        });
    });
</script>
<div class="clear">
</div>
<div id="itemPrice">
<h2>Item Pricing List</h2>
    <table id="myItemPrice" class="display">
        <thead>
            <th>
                ID
            </th>
            <th>
                PDSID
            </th>
            <th>
                PID
            </th>
            <th>
                Effective Date
            </th>
            <th>
                Expiry Date
            </th>
            <th>
                G140
            </th>
            <th>
                AccID
            </th>
            <th>
                CCID
            </th>
        </thead>
        <tbody>
        </tbody>
        <tfoot>
            <th>
                ID
            </th>
            <th>
                PDSID
            </th>
            <th>
                PID
            </th>
            <th>
                Effective Date
            </th>
            <th>
                Expiry Date
            </th>
            <th>
                G140
            </th>
            <th>
                AccID
            </th>
            <th>
                CCID
            </th>
        </tfoot>
    </table>
</div>
<div class="clear"></div><br />
    <div id="itemPrice">
    <h2>Item Tier Price List</h2>
    <table id="myTierPrice" class="display">
    <thead>
        <th>ID</th>
        <th>IPFID</th>
        <th>Quantity</th>
        <th>Amount</th>
        <th>Maximum Discount Percentage</th>
        <th>Maximum Discount Amount</th>
        <th>Tax 1</th>
        <th>Tax 2</th>
        <th>Commission</th>
    </thead>
    <tbody>
    </tbody>
    <tfoot>
        <th>ID</th>
        <th>IPFID</th>
        <th>Quantity</th>
        <th>Amount</th>
        <th>Maximum Discount Percentage</th>
        <th>Maximum Discount Amount</th>
        <th>Tax 1</th>
        <th>Tax 2</th>
        <th>Commission</th>
    </tfoot>
    </table>
    </div>
    <div>
    <button id="btnAddNewRow" value="Ok">
        Add New Tier Price</button>
    <button id="btnDeleteRow" value="cancel">
        Delete Tier Price</button>
    </div>
    <form id="formAddNewRow" action="#" title="New Tier Price Settings">
<input type="hidden" name="ID" id="ID" value="-1" rel="0" />
<label>
    Price ID</label><br />
<input type="text" name="IPFID" id="IPFID" rel="2" /><br />
<br />
<label>
    Quantity</label><br />
<input type="text" name="Qty" id="Qty" rel="2" /><br />
<br />
<label>
    Amount</label><br />
<input type="text" name="Amt" id="Amt" rel="3" /><br />
<font color="green">Leave empty to use default pricing</font><br />
<br />
<label>
    Maximum Discount Percent</label><br />
<input type="text" name="MaxDiscPct" id="MaxDiscPct" rel="4" /><br />
<br />
<label>
    Maximum Discount Amount</label><br />
<input type="text" name="MaxDiscAmt" id="MaxDiscAmt" rel="5" /><br />
<br />
<table style="border-style:none;">
<tr><td>
<label>
    Tax 1</label><br />
<input type="text" name="Tax1" id="Tax1" rel="6" style="width:90px;" /><br />
<br /></td>
<td>
<label>
    Tax 2</label><br />
<input type="text" name="Tax2" id="Tax2" rel="7"  style="width:90px;" /><br />
<br /></td>
</tr>
</table>
<label>
    Commission</label><br />
<input type="text" name="Comm" id="Comm" rel="7" /><br />
<br />
</form>

ヒントやガイドをいただければ幸いです。ありがとうございました。

4

1 に答える 1

3

コードを書き直すのに数時間 (!) 費やしました。 ここからダウンロードできます

環境

  • Windows 7、64 ビット
  • Visual Studio 2010 SP1 で開発
  • ASP.NET 4.0 MVC 3.0 の使用
  • Internet Explorer 9.0 および Firefox 7.0.1 でテスト済み

インフラストラクチャー

_Layout.cshtml のブロック内に次のリンクとスクリプトがあります。

<head>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
</head>

プロジェクトに 3 つのフォルダーを追加しました。

  1. TierPrice.cs を含む \Models\
  2. FakeRepository1.cs および FakeRepository2.cs を含む \Services\
  3. jquery.dataTables.js とその関連ファイルを含む \Scripts\DataTables

他のフォルダーとファイルは、新しい ASP.NET MVC 3 プロジェクトと同じように残しました。

注: .makeEditable() メソッド (jquery.dataTables.editable プラグイン) をコメントアウトしました。

TierPrice クラス

TierPrice を表す単純なクラス。

public class TierPrice
{
    public int ID { get; set; }
    public int IPFID { get; set; }
    public int Qty { get; set; }
    public int Amt { get; set; }
    public int MaxDiscPct { get; set; }
    public int MaxDiscAmt { get; set; }
    public int Tax1 { get; set; }
    public int Tax2 { get; set; }
    public int Comm { get; set; }
}

偽のリポジトリ

2 つの異なるデータベースを模倣するためだけの 2 つの異なる偽のリポジトリ。

ホームコントローラー

プロジェクト内の唯一のコントローラー。それについて特別なことは何もありません。

指数法

public ActionResult Index()
{
    ViewBag.ID = 1; // ID of table?
    return View();
}

AjaxItemPriceProviderAjaxTierPriceProviderは、ビュー内の 2 つの dataTables にデータを提供する 2 つの JsonResult メソッドです。

    public JsonResult AjaxItemPriceProvider (int id)
    {
        /* =======================================
         Description of parameters

         * INPUT PARAMETERS:
         iDisplayLength: Number of rows in each page 
         iDisplayStart: The Number of the row that will be shown from it to iDisplayLength
         sSearch: Seach phrase
         iSortCol_0 (_n): Name of Sort field
         sSortDir_0 (_n): Direction of the sort

         * OUTPUT PARAMETERS:
         sEcho: Number of calling this method via dataTables-AJAX (1-base: in first call sEcho is 1)
         iTotalRecords: Number of total records
         iTotalDisplayRecords: 
         aaData: Return data in string array format
        * =======================================
        */

        string[][] ItemPriceRows = FakeRepository1.GetAllItemPrices(id);

        Dictionary<string, object> result = new Dictionary<string, object>();
        result.Add("sEcho", Request["sEcho"]);
        result.Add("iTotalRecords", ItemPriceRows.Length);
        result.Add("iTotalDisplayRecords", ItemPriceRows.Length);
        result.Add("aaData", ItemPriceRows);

        JsonResult json = Json(result, JsonRequestBehavior.AllowGet);
        return json;
    }

    public JsonResult AjaxTierPriceProvider (int id)
    {
        string[][] TierPriceRows = FakeRepository2.GetAllTierPrices(id);

        Dictionary<string, object> result = new Dictionary<string, object>();
        result.Add("sEcho", Request["sEcho"]);
        result.Add("iTotalRecords", TierPriceRows.Length);
        result.Add("iTotalDisplayRecords", TierPriceRows.Length);
        result.Add("aaData", TierPriceRows);

        JsonResult json = Json(result, JsonRequestBehavior.AllowGet);
        return json;
    }

注: Id (ViewBag.ID) はこれらのメソッドに渡され、メソッドはそれを偽のリポジトリに渡します。しかし、このサンプルでは、​​リポジトリは実際にはそれらを気にせず、偽のデータを返すだけです。

最後に、もちろん POST アクションを介して、新しい行 (TierPrice) を受け入れる簡単な方法:

    [HttpPost]
    public String AjaxAddNewTierPrice(TierPrice newTierPrice)
    {
        // TierPrice calss is defined in Models\TierPrice.cs
        string addReult = FakeRepository2.AddTierPrice(newTierPrice);
        return addReult;
    }

意見

ビューは少し複雑に見えるかもしれませんが、そうではありません。

dataTables の AJAX パスの記述方法に注意してください。

"sAjaxSource": '@Url.Action("AjaxItemPriceProvider", new { id = ViewBag.ID })',

次に、myItemPrice dataTable で、最初の列は次のように定義されます。

{ "sName": "ID",
  "fnRender": function (oObj) { return renderAddNewRowLink(oObj); },
  "aTargets": [0],
  ...
},

renderAddNewRowLink() は次のように単純です。

function renderAddNewRowLink(obj) {
    var hyperlink = "<a href='javascript:void(0)' ";
    hyperlink += "onclick='showFormAddNewRow("
    hyperlink += obj.aData[0];
    hyperlink += ")'>";
    hyperlink += obj.aData[0];
    hyperlink += "</a>";
    return hyperlink;
}

注: oObj は、dataTable プラグインのコンテナー オブジェクトです。renderAddNewRowLink() に渡す必要があるため、関数は AJAX 呼び出しからのデータを含む obj.aData 配列にアクセスできます。

わかった。もうすぐ着きます。showFormAddNewRow() も非常にシンプルで簡単です。見てみましょう:

function showFormAddNewRow(id) {
    $('#divAddNewRow').show();
    $('#divCoverBg').show();
    $('#formAddNewRow #ID').val(id);
}

注: $('#formAddNewRow #ID').val(id) は、myItemPrice テーブルから formAddNewRow フォームに ID を渡すキーです。

Ajax.BeginFormに精通している場合、残りは明確です。そうでない場合は、これが良い例です: Using Ajax.BeginForm with ASP.NET MVC 3 Razor

注: 次の関数は、新しい TierPrice が追加されるたびに (AjaxTierPriceProvider を呼び出して) myTierPrice dataTable を強制的に更新します。

function addNewTierPriceCompleted(obj) {
    TierPriceTable.fnDraw();
}

また、site.css に 2 つのスタイルを追加しました。新しい行を追加するプロセスには、これらのスタイルが必要です。

于 2011-10-05T20:13:41.323 に答える