DIV CSS 佈局教程網

用 jQuery 搭配 LigerUI 實作彈出式編輯視窗
編輯:JQuery特效代碼     

LigerUI 是一套功能完整的 jQuery UI 元件庫,其中的 dialog 與 form 很適合做「列表 + 彈窗編輯」的常見後台介面。以下依步驟示範如何實作一個可新增、查看、修改、刪除的彈出編輯框。

一、載入相關資源

<link href="../lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<script src="../lib/jquery/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="../lib/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="../lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script>
<script src="../lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
<link href="../lib/css/common.css" rel="stylesheet" type="text/css" />
<script src="../lib/js/common.js" type="text/javascript"></script>
<script src="../lib/js/LG.js" type="text/javascript"></script>
<script src="../lib/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="../lib/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="../lib/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="../lib/js/ligerui.expand.js" type="text/javascript"></script>
<script src="../../lib/ligerUI/js/plugins/ligerDateEditor.js" type="text/javascript"></script>

二、HTML 中放一個隱藏的編輯容器

<div id="Editdetail" style="display:none;"><form id="EditForm" method="post"></form> </div>

三、在工具列事件中分派動作

//工具條事件
function toolbarBtnItemClick(item) {
    switch (item.id) {
        case "add":
            addbill({}, true, '添加記錄', false);
            break;
        case "view":
            var selected = grid.getSelected();
            if (!selected) { LG.tip('請選擇行!'); return }
            addbill(selected, false, '查看記錄', true);
            break;
        case "modify":
            var selected = grid.getSelected();
            if (!selected) { LG.tip('請選擇行!'); return }
            addbill(selected, false, '修改記錄', false);
            break;
        case "delete":
            jQuery.ligerDialog.confirm('確定刪除嗎?', function (confirm) {
                if (confirm)
                    f_delete();
            });
            break;
    }
}

四、addbill 函式:開啟或 reuse 彈窗並回填資料

var detailWin = null, currentData = null, currentIsAddNew, currentIsView;
function addbill(data, isAddNew, t, isview) {
    currentData = data;
    currentIsAddNew = isAddNew;
    currentIsView = isview;
    if (detailWin) {
        detailWin.set('title', t);
        detailWin.show();
    }
    else {
        // 放入彈出窗口樣式內容(見第五步)
    }
    if (!isAddNew) {
        $("#ProtId2").val(currentData.ProductName);
        $("#ProId1").val(currentData.ProductId);
        $("#ForId2").val(currentData.FormatName);
        $("#Foad1").val(currentData.FormatId);
        $("#Appl").val(currentData.AppendBill);
        $("#Bum").val(currentData.BoxNum);
        $("#Maate").val(currentData.MadeDate);
        $("#BottleNum").val(currentData.BottleNum);
        $("#Bumpany2").val(currentData.BuyCompanyName);
        $("#BuyCoy1").val(currentData.BuyCompanyId);
        $("#VayId2").val(currentData.VarietyName);
        $("#VarId1").val(currentData.VarietyId);
        $("#Handate").val(currentData.HandDate);
        $("#Fact2").val(currentData.FactoryName);
        $("#Fact1").val(currentData.FactoryId);
        $("#Froce2").val(currentData.FromPlaceName);
        $("#Froce1").val(currentData.FromPlaceId);
    }
}

五、以 ligerForm 建立表單並用 ligerDialog 彈出

var mainform = $("#EditForm");
mainform.ligerForm({
    inputWidth: 150,
    fields:
    [
        { name: "ProId1", type: "hidden" }, // 隱藏字段,為彈出選擇編號保存值
        { display: "倉庫", name: "Daihao1", newline: true, labelWidth: 100, width: 150, space: 30, type: "text", validate: { required: true, digits: true} },
        { display: "商品名稱", name: "ProId", comboboxName: "ProId2", newline: false, labelWidth: 100, width: 150, space: 30, type: "select", option: {} }, // 彈出選擇框
        { display: "單位", name: "DegreeId", comboboxName: "DegreeId2", newline: false, labelWidth: 100, width: 150, space: 30, type: "select", options: { valueFieldID: "DegreeId1", treeLeafOnly: false, tree: { url: "../handle/se1.ashx?ajaxaction=Getgree", checkbox: false}} },
        { display: "生產日期", name: "MadeDate1", newline: true, labelWidth: 100, width: 150, space: 30, type: "date", validate: { required: true} },
        { display: "備注", name: "mark", newline: false, labelWidth: 100, width: 150, space: 30, type: "text", validate: { required: true, digits: true} }
    ],
    toJSON: JSON2.stringify
});
$.metadata.setType("attr", "validate");
LG.validate(mainform, { debug: true });
$("#HandDate").val(currentdate);
$("#BoNum").val("0");
$.ligerui.get("ProId2").set('onBeforeOpen', f_selectCoct)
$.ligerui.get("Faory2").set('onBeforeOpen', f_selectFary_1)
$.ligerui.get("Buyany2").set('onBeforeOpen', f_selectFary_2)
$.ligerui.get("Froce2").set('onBeforeOpen', f_selectFroace)
detailWin = $.ligerDialog.open({
    target: $("#Editdetail"),
    width: 595, height: 460, top: 80, title: "添加保存修改窗口",
    buttons: [
        { text: '保存', onclick: function () { save(); } },
        { text: '取消', onclick: function () { detailWin.hide(); } }
    ]
});

整體流程是:用 ligerForm 依欄位設定動態產生表單並掛上驗證規則,用 ligerDialog.open 把表單包成彈窗,並在開窗前透過 onBeforeOpen 掛上各下拉選擇框的事件。新增時清空表單、修改時回填資料、查看時設為唯讀,即可完成一個完整的編輯視窗。第六步的保存事件則在 save() 中收集表單資料並送出。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved