1. 程式人生 > >layui資料表格資料字典轉換實現

layui資料表格資料字典轉換實現

前言

專案中使用了賢心大大的layui(零門檻開箱即用的前端UI解決方案),但是目前缺失資料表格資料字典的轉換功能(本人是沒找到),以下是解決方法:延用layui中的laytpl模版引擎。

1.下載dwr的依賴包

<dependency>
    <groupId>org.directwebremoting</groupId>
    <artifactId>dwr</artifactId>
    <version>3.0.2-RELEASE</version>
</dependency>

2. spring純註解配置dwr的servlet

private void initializeDwrServlet(ServletContext servletContext) {
    Dynamic myServlet =
            servletContext.addServlet("dwr", DwrServlet.class);
    Map<String, String> initParameters=new HashMap<String, String>();
    initParameters.put("debug","true");        //引數配置
    myServlet.setInitParameters(initParameters);
    myServlet.addMapping("/dwr/*");
}

記得在專案初始化入口呼叫此方法,xml配置的可以寫到web.xml中,這裡不作說明。

3建立dwr,xml

此檔案路徑需放置在web-inf下

<!DOCTYPE dwr PUBLIC
        "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN"
        "http://getahead.org/dwr/dwr30.dtd">

<dwr>
    <allow>
        <create creator="new" javascript="dictionary">
            <param name="class"  value="com.centit.framework.ip.util.DictionaryUtil"/>
        </create>
    </allow>
</dwr>

建立DictionaryUtil後臺轉換資料字典靜態方法:

public class DictionaryUtil {
    public static String getValue(String sCatalog, String sKey) {
      //從快取中讀取資料字典,返回值返回
        return value;
    }
}

4 配置成功說明

ip/應用/dwr/test/dictionary; 瀏覽器輸入地址出現頁面則說明配置成功。

頁面會有兩個js路徑,記錄好路徑即可,在呼叫頁面需要引入engine.js和dictionary.js(根據配置檔案中的名稱而來)

5公共的js方法

var Dictionaryvalue = function(catalogcode,key){ //1.資料字典編碼2引數key
    var dicvalue;
    Dictionary.getValue(catalogcode,key,{
        callback:function(data){dicvalue = data;
        },
        async:false
    }  );
    return  dicvalue;
}

6 layui資料表格呼叫

<th lay-data="{field:'temptype',templet: '#temptypeTpl'}">型別</th>
<script type="text/html" id="temptypeTpl">
   {{Dictionaryvalue('dispatchFileType',d.temptype)}}
</script>