layui encapsulated ajax table

As the project wrote a lot of things more than half of the global need to add, modify it encapsulates this small way.

 

commonRequest.js

layui.define(['jquery','table'], function(exports){
    var $ = layui.jquery,table=layui.table;
    var obj = {
        ajax: function (obj) {
            if (!obj.headers) {
                obj.headers = {
            // 兼容IE9 'cache-control': 'no-cache', 'Pragma': 'no-cache', 'Authorization': window.sessionStorage.getItem("token") }; } if (!obj.type) obj.type = "GET"; if (!obj.dataType) obj.dataType = "json"; if obj.cache = false (obj.cache!) // When configured as false, said they did not get the data from the browser cache, you can see when debugging, when hair Get request, it will automatically time-stamped if (obj.async!) Obj.async = true obj.crossDomain = true === !(document.all);//这句是关键 if (!obj.error) { obj.error = function (err) { layer.msg("网络连接失败!"); console.log(err); } } console.log(obj); $.ajax(obj); }, render: function (obj) { obj.headers = { 'cache-control': 'no-cache', 'Pragma': 'no-cache', 'Authorization': window.sessionStorage.getItem("token") }; console.log(obj); table.render (obj); } }; // output interface exports ( 'commonRequest', obj) ; // use the module name });

 Configuration configRequest.js

layui.config ({
     Base : ' ../../../../common/ '       // directory of the custom component layui 
}). Extend ({ // set component alias 
    Common:    ' commonRequest ' , 
} );

Introduced

<Script src = "../../../../ common / commonTbRqs.js"> </ script> // if there is another module is configured to use the following pages, this method being given

  

use

layui.use ([ 'Table', 'commonRequest'], function () { 
 var = Table layui.table, commonRequest = layui.commonRequest; 
// table rendering 
    commonRequest.render ({ 
            elem: tableConfig.elem, specify the original // table element selector (recommended id selector) 
            height: tableConfig.height, // height of the container 
            cols: tableConfig.cols, // set header 
            url:? tableConfig.params tableConfig.getUrl (): url, 
            Page: tableConfig.page ! = to false to true:? to false, 
            WHERE: requestParam, 
            headers: { 'the Authorization': window.sessionStorage.getItem ( "token")} 
        }) 
// Ajax request 
   commonRequest.ajax ({ 
        type: "the POST", 
        URL: URL ,
        date: date,
        dataType: "json",
        success: function(data) {
       
        }
      });

})

  

 

 

 

 

Guess you like

Origin www.cnblogs.com/langqq/p/11448102.html
Recommended