3

こんにちは、私はテーブルで働いています

サイトを更新しないと、テーブルを atm 更新できません。簡単にできる方法が必要です

行の追加、行の削除、表の内容の行の変更。

私のテーブルはこのように構築されています。

{....}
@using (Html.BeginForm())
{
<fieldset>
    <legend>ShiftTypes</legend>
    <table class="EditableTable" id="EditableTableShiftTypes">
        <thead>
            <tr>
                <th>
                    #:
                </th>
                <th>
                    ShiftName:
                </th>
                <th>
                    ShiftCode:
                </th>
                <th>
                    Suplement:
                </th>
                <th>
                    ExtraSuplement:
                </th>
                <th>
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (BPOPortal.Domain.Models.ShiftTypeView type in Model.ShiftTypeList)
            {
                <tr>
                    <td>
                        @type.ID
                    </td>
                    <td>
                        @type.ShiftName
                    </td>
                    <td>
                        @type.Name

                    </td>
                    <td>
                        @type.Supplement

                    </td>
                    <td>
                        @type.OneTimePayment

                    </td>
                    <td>
                        <button>
                            Delete</button>
                    </td>
                </tr>
            }
            <tr>
                <td>
                    <label>
                    </label>
                </td>
                <td>
                    @Html.Editor("ShiftName")
                </td>
                <td>
                    @Html.Editor("ShiftCode")
                </td>
                <td>
                    @Html.Editor("Suplement")
                </td>
                <td>
                    @Html.DropDownList("ExtraSuplement", new SelectListItem[] { new SelectListItem() { Text = "yes", Value = "true", Selected = false }, new SelectListItem() { Text = "No", Value = "false", Selected = false } }, "--Choose--")
                </td>
                <td>
                    <button id="AddButton">
                        Add</button>
                </td>
            </tr>
        </tbody>
    </table>
    <button type="submit" id="Gem">
        Save</button>
</fieldset>
{....}

次の方法で Ajax を使用しようとしましたが、ページ全体が更新されます。

{....}
        var ID= 2;
        $("#AddButton").click(function(){
            var ShiftName= $('#ShiftName').attr('value');
            var ShiftCode= $('#ShiftCode').attr('value');
            var Suplement= $('#Suplement').attr('value');
            var ExtraSuplement= $('#ExtraSuplement').attr('value');


            $.ajax({
                url: '@Url.Action("AddData", "ShiftTypesConfiguration")',
                data: { ID: ID.toString(), ShiftName: $('#ShiftName').attr('value'), ShiftCode: $('#ShiftCode').attr('value'), Suplement: $('#Suplement').attr('value'), ExtraSuplement: $('#ExtraSuplement').attr('value') },
                type: 'POST',
                //                contentType: 'application/json; charset=utf-8;',
                dataType: 'json',
                success: function (response)
                {
                   function fnClickAddRow() {                       
                      $('#EditableTableShiftTypes').dataTable().fnAddData([
                     response.ID,
                     response.ShiftName,
                     response.ShiftCode,
                     response.Suplement,
                         response.ExtraSuplement,
                         "<button>Delete</button>"]); } 
                    }
            });
            ID++;
        });
{....}


</script>

リクエストを処理するコントローラー内の私のメソッド。

public JsonResult AddData(string ID, string ShiftName, string ShiftCode, string Suplement, string ExtraSuplement)
    {
        TableViewModel tableViewModel = new TableViewModel();
        tableViewModel.ID = ID;
        tableViewModel.ShiftName= ShiftName;
        tableViewModel.ShiftCode= ShiftCode;
        tableViewModel.Suplement= Suplement;
        tableViewModel.ExtraSuplement= ExtraSuplement;


        return Json(tableViewModel);
    }

しかし、それはうまくいかないようです 今、私はこれをより良く/より簡単に/働くためのアイデアと方法を求めています

編集:過去のエラーのコピーを見た

EDIT2:少し変更しました。スクリプトの実行方法にエラーがあることがわかりました。これは最新です。まだ問題はありますが、少なくともエラーを確認して説明できるようになりました。

これは私がスクリプトを変更したものです

 $("#AddButton").click(function (event) {
    event.preventDefault();
    var ShiftName = $('#ShiftName').attr('value');
    var ShiftCode = $('#ShiftCode').attr('value');
    var Suplement = $('#Suplement').attr('value');
    var ExtraSuplement = $('#ExtraSuplement').attr('value');


    $.ajax({
        url: '@Url.Action("AddData", "ShiftTypesConfiguration")',
        data: { ID: ID.toString(), ShiftName: ShiftName, ShiftCode: ShiftCode, Suplement: Suplement, ExtraSuplement: ExtraSuplement },
        type: 'POST',
        //                contentType: 'application/json; charset=utf-8;',
        dataType: 'json',
        success: function (response) {
            function fnClickAddRow() {
                $('#EditableTableShiftTypes').dataTable().fnAddData([
                 response.ID,
                 response.ShiftName,
                 response.ShiftCode,
                 response.Suplement,
                 response.ExtraSuplement,
                 "<button>Delete</button>"]);
            }
        }
    });
    ID++;
});

現在、firebug の助けを借りて、値がページに戻っていることがわかりましたが、値が表示される前にページが更新されました。

4

1 に答える 1

0

ここではコードを書いていませんが、その方法は次のとおりです。

追加/編集/削除で、アクションへの ajax 呼び出しを行います。アクションで操作 (追加/編集/削除) を実装し、操作が正常に完了したかエラーにより失敗したかに応じて、true/false フラグを json として返します。

次に、ajax 呼び出しの成功関数success: function (response){}で、返された値が成功またはエラーを意味する true/false であるかどうかを確認します。

次に、いくつかのjqueryを使用して、行を追加したり、テーブルから行を削除したりできます。

これらのリンクをチェックしてください: http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/

于 2012-10-26T09:31:48.390 に答える