`

DWR 学习笔记<二>util.js学习

    博客分类:
  • AJAX
阅读更多

DWR util.js 学习笔记 

util.js包含一些有用的函数function,用于在客户端页面调用,它可以和dwr分开,独立营用于你的系统中。

主要功能如下:

1、$() 获得页面参数值

2、addOptions and removeAllOptions 初始化下拉框

3、addRows and removeAllRows  填充表格

4、getText  取得text属性值

5、getValue 取得form表单值

6、getValues 取得form多个值

7、onReturn  

8、selectRange

9、setValue

10、setValues

11、toDescriptiveString

12、useLoadingMessage

13、Submission box

***************************************************************************************

1、$()函数

  IE5.0 不支持

  $ = document.getElementById

  取得form表单值

  var name = $("name");

***************************************************************************************

2、用于填充 select 下拉框 option

  a、如果你想在更新select 时,想保存原来的数据,即在原来的select中添加新的option:

     var sel = DWRUtil.getValue(id);

     DWRUtil.removeAllOptions(id);

     DWRUtil.addOptions(id,...);

     DWRUtil.setValue(id,sel);

     demo:比如你想添加一个option:“--请选择--”

    DWRUtil.addOptions(id,["--请选择--"]);    

    DWRUtil.addOptions()有5中方式:

    @ Simple Array Example: 简单数组

      例如:

      Array array = new Array[ 'Africa', 'America', 'Asia', 'Australasia', 'Europe' ];

      DWRUtil.addOptions("demo1",array);

    @ Simple Object Array Example 简单数组,元素为beans

      这种情况下,你需要指定要显示 beans 的 property 以及 对应的 bean 值

      例如:

       public class Person {

     private String name;

     private Integer id;

     pirvate String address;

     public void set(){……}

     public String get(){……}

       }

       DWRUtil.addOptions("demo2",array,'id','name');

       其中id指向及bean的id属性,在optiong中对应value,name指向bean的name属性,对应下拉框中显示的哪个值.

     @ Advanced Object Array Example 基本同上

    DWRUtil.addOptions( "demo3", 

                [{ name:'Africa', id:'AF' },

                 { name:'America', id:'AM' },

                 { name:'Asia', id:'AS' },

                 { name:'Australasia', id:'AU' },

                 { name:'Europe', id:'EU' }

        ],'id','name');

     @ Map Example 用制定的map来填充 options:

       如果 server 返回 Map,呢么这样处理即可:

       DWRUtil.addOptions( "demo3",map);

       其中 value 对应 map keys,text 对应 map values;

     @ <ul> and <ol> list editing

       DWRUtil.addOptions() 函数不但可以填出select,开可以填出<ul>和<ol>这样的heml元素

 

***************************************************************************************

3、addRows and removeAllRows  填充表格

   DWR 提供2个函数来操作 table;

   ----------------------------

   DWRUtil.addRows(); 添加行

   ----------------------------

   DWRUtil.removeAllRows(id); 删除指定id的table

   ----------------------------

   下面着重看一下 addRows() 函数:

   DWRUtil.addRows(id, array, cellfuncs, [options]);

    其中id 对应 table 的 id(更适合tbodye,推荐使用 tbodye)

    array 是server端服务器的返回值,比如list,map等等

    cellfuncs 及用返回值来天春表格

    [options] 用来设置表格样式,它有2个内部函数来设置单元格样式(rowCreator、cellCreator)。

    比如: server端返回list,而list中存放的是下面这个 bean:

        public class Person {

     private String name;

     private Integer id;

     pirvate String address;

     public void set(){……}

     public String get(){……}

       }

    下面用  DWRUtil.addRows(); 

 

   /**************************************************************************************/

  

   function userList(data){

    //var delButton = "<input type='button'/>";

    //var editButton = "<input type='button'/>";

    var cellfuncs = [

        function(data){return data.id;},

        function(data){return data.userName;},

        function(data){return data.userTrueName;},

        function(data){return data.birthday;},

        function(data){

            var idd = data.id;

            var delButton = document.createElement("<INPUT TYPE='button' onclick='delPerson("+ idd +")'>");

            delButton.setAttribute("id","delete");

            delButton.setAttribute("value","delete");

            return delButton;

        },

        function(data){

            var idd = data.id;

            var editButton = document.createElement("<INPUT TYPE='button' onclick='editPerson("+ idd +")'>");

            editButton.setAttribute("name","edit");

            editButton.setAttribute("value","edit");            

            return editButton;

        }

    ];

    DWRUtil.removeAllRows('tabId');    

    DWRUtil.addRows('tabId', data,cellfuncs,{

    rowCreator:function(options) {

        var row = document.createElement("tr");

        var index = options.rowIndex * 50;

        row.setAttribute("id",options.rowData.id);

        row.style.collapse = "separate";

        row.style.color = "rgb(" + index + ",0,0)";

        return row;

      },

      cellCreator:function(options) {

        var td = document.createElement("td");

        var index = 255 - (options.rowIndex * 50);

        //td.style.backgroundColor = "rgb(" + index + ",255,255)";

        td.style.backgroundColor = "menu";

        td.style.fontWeight = "bold";

        td.style.align = "center";

        return td;

      }        

    });

    document.getElementById("bt").style.display = "none";

     }

     待续…………………………………………

   /**************************************************************************************/

   /**************************************************************************************/

   /**************************************************************************************/

   4、getText  取得text属性值

      DWRUtil.getText(id): 用来获得 option 中的文本

      比如:

       <select id="select">

    <option  value="1"> 苹果 </option>

    <option  value="2" select> 香蕉 </option>

    <option  value="3"> 鸭梨 </option>

       </select>

      调用 DWRUtil.getText("select"); 将返回 "香蕉" 字段;

      DWRUtil.getText(id);仅仅是用来获得 select 文本值,其他不适用。

 

   /**************************************************************************************/

   

 5、DWRUtil.getValue(id): 用来获得 form 表单值

      有如下几种情况:

          Text area (id="textarea"): DWRUtil.getValue("textarea")将返回 Text area的值;

      Selection list (id="select"): DWRUtil.getValue("select") 将返回 Selection list 的值;

      Text input (id="text"): DWRUtil.getValue("text") 将返回 Text input 的值;

      Password input (id="password"): DWRUtil.getValue("text") 将返回 Password input 的值;

      Form button (id="formbutton"): DWRUtil.getValue("formbutton") 将返回 Form button 的值;

      Fancy button (id="button"): DWRUtil.getValue("formbutton") 将返回 Fancy button 的值;

 

  /**************************************************************************************/

      6、getValues 取得form多个值

      批量获得页面表单的值,组合成数组的形式,返回 name/value;

      例如: form():

       <input type="textarea" id="textarea" value="1111"/>

       <input type="text" id="text" value="2222"/>

       <input type="password" id= "password" value="3333"/>

       <select id="select">

    <option  value="1"> 苹果 </option>

    <option  value="4444" select> 香蕉 </option>

    <option  value="3"> 鸭梨 </option>

       </select>

       <input type="button" id="button" value="5555"/>

      那么: DWRUtil.getValues({textarea:null,select:null,text:null,password:null,button:null})

      将返回  ^^^^^^^^^^^^^^^^{textarea:1111,select:4444,text:2222,password:3333,button:5555}

    

   /**************************************************************************************/

  

   7、DWRUtil.onReturn 防止当在文本框中输入后,直接按回车就提交表单。

     <input type="text" onkeypress="DWRUtil.onReturn(event, submitFunction)"/>

     <input type="button" onclick="submitFunction()"/>

  

   /**************************************************************************************/

 

   8、DWRUtil.selectRange(ele, start, end);

      在一个input box里选一个范围 

      DWRUtil.selectRange("sel-test", $("start").value, $("end").value);

      比如:<input type="text" id="sel-test" value="012345678901234567890">

      DWRUtil.selectRange("sel-test", 2, 15); 结果 文本框中的值"2345678901234"将被选中'

   

  /**************************************************************************************/

 

   9、DWRUtil.setValue(id,value);

      为指定的id元素,设置一个新值;

   /**************************************************************************************/

   10、DWRUtil.setValues({  

    name: "fzfx88", 

    password: "1234567890" 

    }

       ); 同上,批量更新表单值.

   /**************************************************************************************/

   11、DWRUtil.toDescriptiveString()

   带debug信息的toString,第一个为将要debug的对象,第二个参数为处理等级。等级如下: 

    0: Single line of debug 单行调试  

    1: Multi-line debug that does not dig into child objects 不分析子元素的多行调试  

    2: Multi-line debug that digs into the 2nd layer of child objects 最多分析到第二层子元素的多行调试 

    <input type="text" id="text">

    DWRUtil。toDescriptiveString("text",0);

   /**************************************************************************************/

   12、DWRUtil.useLoadingMessage();

    当发出ajax请求后,页面显示的提示等待信息;

    function searchUser(){

    var loadinfo = "loading....."

    try{

        regUser.queryAllUser(userList);

        DWRUtil.useLoadingMessage(loadinfo);        

    }catch(e){

    }

    }

    /**************************************************************************************/

分享到:
评论

相关推荐

    dwr 例子,jar包,engine.js util.js 学习笔记

    这个压缩包包含的是DWR的实例、相关的jar包以及两个重要的JavaScript文件——`engine.js`和`util.js`,这些都是学习DWR的关键组件。 1. **DWR基础概念**: DWR的核心功能是提供一种方式,使得客户端JavaScript可以...

    DWR util.js 学习笔记 整理

    DWR util.js 学习笔记整理 DWR util.js 是一个功能强大的 JavaScript 库,它提供了许多有用的函数,可以帮助开发者在客户端页面上实现各种操作。下面是对 DWR util.js 的学习笔记整理。 1. $() 函数 DWRUtil 中的...

    DWR util.js学习笔记.doc

    DWR util.js 学习笔记 DWR util.js 是一个JavaScript工具库,提供了一些有用的函数,用于在客户端页面调用。下面是DWR util.js中的主要功能: 1. $()函数:获取页面参数值 该函数用于获取页面参数值,例如:var ...

    DWR学习笔记

    ### DWR学习笔记知识点 #### 一、DWR简介与安装配置 **DWR (Direct Web Remoting)** 是一种开放源代码的JavaScript库,它可以让客户端的JavaScript代码直接调用服务器端的Java方法,实现远程过程调用(RPC)功能。...

    dwr学习笔记和总结

    ### dwr学习笔记和总结 #### 一、DWR简介 DWR (Direct Web Remoting) 是一个简化Ajax开发的框架,它使得JavaScript能够直接调用服务器端的Java方法成为可能,无需编写复杂的Ajax代码。这极大地提高了开发效率,并...

    dwr的一般使用学习笔记

    在JavaScript端,首先需要引入由DWR自动生成的接口文件(如TestClass.js)和DWR核心库(engine.js和util.js)。然后,你可以直接调用JavaScript对象的方法,如`testClass.testMethod1()`。 2. 调用有简单返回值的...

    dwr学习笔记

    - **第二种方式** 使用`&lt;convert converter="bean"`,这里DWR会使用BeanConverter,适用于普通的Java Bean对象。这种方式不涉及特定的ORM框架。 ### 2. 对象的保存 DWR提供了方便的方法来在前端JavaScript和后端...

    DWR学习笔记及经验总结

    在“DWR学习笔记及经验总结”中,我们可以期待找到以下关键知识点: 1. **DWR的基本概念**:了解DWR的核心理念,包括远程方法调用(Remote Method Invocation)、反向Ajax以及它如何通过JSON或XML格式传递数据。 2...

    dwr笔记 dwr自学资料

    4. **Ajax API**:为JavaScript提供了一组API,如`dwr.engine.remote.execute()`用于调用服务器方法,`dwr.util.*`提供了数据操作和类型转换的工具。 四、DWR的使用步骤 1. **引入依赖**:在项目中添加DWR库。 2. *...

    一套完整的dwr ajax框架综合资料

    DWR(Direct Web Remoting)是一种Java库,用于在浏览器和...`DWR学习.doc`则可能提供了学习路径和常见问题解答。通过这些资料,开发者可以系统地学习和掌握DWR与Ajax的结合应用,提升Web应用的交互性和用户体验。

Global site tag (gtag.js) - Google Analytics