1. 程式人生 > >js循環數據時綁定方法並將數據對象傳遞出來

js循環數據時綁定方法並將數據對象傳遞出來

gif json response templet 對象 min msg title checkbox

代碼是參考的layui的作者 賢心 的代碼,其他的代碼太多了,看的tree中的代碼,雖然效果實現了,但是原理不是很清楚,因為涉及到閉包了,而我對於前段只知道基礎的東西,對於深入的東西不是很清楚,所以,有幸看到的人,不要問我,我也不懂,嘿嘿

var dictype;

        $(function () {
            ajaxEx("/DictionaryInfo/GetDicType", {}, function (json) {
                if (json.Code == 0) {
                    var data = json.Data;

                    //dic.load(data);
                    //加載數據
                    dictionary.load(data);

                    return;
                }

                alert(json.Message);
            })
        })

        //對象
        var dictionary = {};

        //對象點擊方法
        dictionary.click = function (elem,item) {
            //項綁定事件
            elem.children(‘a‘).on("click", function (e) {

                zsw.stope(e);

                dictype = item;

                LoadTable({ code: item.Code });
                //alert(JSON.stringify(item));
            })
        }

        //對象加載數據方法
        dictionary.load = function (json) {

            var dicListObj = $("#dicType");

            var html = "";

            if (json.length > 0) {

                $.each(json, function (i, item) {
                    if (i == 0) {
                        dictype = item;
                    }

                    //此句是關鍵,function方法閉包,不懂原理,對前端研究不深
                    html = $(["<li>", function () { return "<a>" + item.Name + "</a>" }(), "</li>"].join(‘‘));

                    dicListObj.append(html);

                    //觸發單擊方法
                    dictionary.click(html,item);

                })
            }

            dicListObj.append("<li> </li>");
        }

        //加載表格
        function LoadTable(obj) {
            var dicitemtable = table.render({
                elem: ‘#dicItem‘
          , url: ‘/DictionaryInfo/GetDicItem‘
          , method: "post"
          , where: obj//{code:dictype.Code}//where: {token: ‘sasasas‘, id: 123}
                //resuest://用於修改默認的請求中頁碼的參數名稱
          , request: {
              pageName: ‘pageIndex‘ //頁碼的參數名稱,默認:page
                , limitName: ‘pageSize‘ //每頁數據量的參數名,默認:limit
          }
                //response 用於定義返回的數據格式
            , response: {
                statusName: ‘Code‘ //數據狀態的字段名稱,默認:code
                , statusCode: 0 //成功的狀態碼,默認:0
                , msgName: ‘Message‘ //狀態信息的字段名稱,默認:msg
                , countName: ‘TotalCount‘ //數據總數的字段名稱,默認:count
                , dataName: ‘Data‘ //數據列表的字段名稱,默認:data
            }
          , cellMinWidth: 80 //全局定義常規單元格的最小寬度,layui 2.2.1 新增
                //, width: "90%"//$(window).width()- 40
          , cols: [[
            //{ type: ‘checkbox‘ }  //開啟多選框
            //{ checkbox: true }
             { type: ‘numbers‘, title: ‘序號‘ }
            //, { field: ‘Id‘, width: 80, title: ‘序號‘, sort: true,style:"display:none;" }//隱藏使用display
            //, { field: ‘numbers‘, width: 80, title: ‘序號‘, sort: true, style: "display:none;" }
            //DKey,Value
            , { field: ‘DKey‘, width: 80, title: ‘值‘, sort: true }
            , { field: ‘Value‘, width: 120, title: ‘顯示名稱‘, sort: true }
            //, { field: ‘Remark‘, title: ‘說明‘, sort: true/*, templet: ‘#temp1‘ 加載模板*/ }
            , { align: ‘center‘, width: 180, title: ‘操作‘, toolbar: ‘#barDicItem‘ }
          ]]
          , page: false
            , limit: 10
            , limits: [10, 30, 50, 100]
            , id: ‘idTest‘
            });
        }

  

js循環數據時綁定方法並將數據對象傳遞出來