第5章 jQuery中操作表单和表格

例:当文本框获取焦点后,改变其背景颜色;失去焦点后,恢复为原来的样式.
css代码:
.focus{border:1px solid #f00;background:#fcc;}
jquery代码如下:

$(function(){
    $(":input").focus(function(){
      $(this).addClass("focus");
      if($(this).val()==this.defaultValue)
      {
        $(this).val("");
      }    
       }).blur(function(){
        $(this).removeClass("focus");
        if($(this).val()=="")
        {
        $(this).val(this.defaultValue);
        }
    });
});

例:给一个多行文本框放置一个放大和缩小按钮,通过这两个按钮来实现动画调整文本框大小

$(function(){
    var $comment=$("#comment");    //获取文本框
    $("btnbigger").click(function(){    //放大按钮绑定单击事件
    if(!$comment.is(":animated"))    //判断是否处于动画
    {
       if($comment.height()<400)
       {
        $comment.animate({height:"+=30"},400);    //动画设置高度,在原有的基础上增加30
       }
    }    
     });
    $("btnsmaller").click(function(){    //缩小按钮绑定单击事件
    if(!$comment.is(":animated"))    
    {
       if($comment.height()>30)
       {
        $comment.animate({height:"-=30"},400);    //动画设置高度,在原有的基础上减30
       }
    }    
     });
});

设置文本框向上或向下则用滚动条的属性:scrollTop

jquery中获取复选框选中的多个值
$("btnSubmit").click(function(){
 var str="你喜欢的运动是:\r\n";
 $("[name=items]:checkbox:checked").each();
});

DataSet和List的转换

将dataset转化为IList   
将dataset转化为IList<T>,关键是将datatable转化为类型IList<T>,代码如下:
public T Export(System.Data.DataTable Table)
        {
            // 定义集合 
            IList<T> List = null;
            // 获得此模型的类型 
            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in Table.Rows)
            {
                T t = new T();
                //获得此模型的公共属性 
                PropertyInfo[] propertys = t.GetType().GetProperties();

                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    //检查DataTable是否包含此列 
                    if (Table.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter 
                        if (!pi.CanWrite)
                        {
                            continue;
                        }
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                        {
                            if (value.GetType().ToString() == "System.Double")
                            {
                                pi.SetValue(t, doubleToInt(value), null);
                            }
                            else if (value.GetType().ToString() == "System.String")
                            {
                                pi.SetValue(t, value, null);
                            }
                        }
                    }
                }
                return t;
            }
        }

List转化成DataSet
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Data;  
namespace WebApplication2  
{  
    public partial class _Default : System.Web.UI.Page  
    {  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
        //模拟数据
                var list = new List<Demo> {  
                    new Demo{ id=1,age=18, name="Tim"},  
                    new Demo{ id=2,age=22, name="Allen"},  
                    new Demo{ id=3,age=24, name="Jim"}  
                };  
                var ds = list.ToDataSet();  
                GridView1.DataSource = ds.Tables[0];  
                GridView1.DataBind();  
            }  
        }  
    }  
  
    static class Extensions  
    {  
        internal static DataSet ToDataSet<T>(this IList<T> list)  
        {  
            Type elementType = typeof(T);  
            var ds = new DataSet();  
            var t = new DataTable();  
            ds.Tables.Add(t);  
            elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));  
            foreach (T item in list)  
            {  
                var row = t.NewRow();  
                elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);  
                t.Rows.Add(row);  
            }   
            return ds;  
        }  
    }  
    class Demo  
    {  
        public int id { get; set; }  
        public string name { get; set; }  
        public int age { get; set; }  
    }  
}  


  /// DataSet装换为泛型集合         
        /// </summary>         
        /// <typeparam name="T"></typeparam>         
        /// <param name="ds">DataSet</param>         
        /// <param name="tableIndex">待转换数据表索引</param>         
        /// <returns></returns>         
        public static IList<T> DataSetToIList<T>(DataSet ds, int tableIndex)
        {
            if (ds == null || ds.Tables.Count < 0)
                return null; 
            if (tableIndex > ds.Tables.Count - 1)
                return null; 
            if (tableIndex < 0)
                tableIndex = 0;
            DataTable dt = ds.Tables[tableIndex];
        
            IList<T> result = new List<T>();
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (pi.Name.Equals(dt.Columns[i].ColumnName))
                        {
                            if (dt.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, dt.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                } result.Add(_t);
            } return result;
        }
        /// <summary>         
        /// DataSet装换为泛型集合        
        ///  </summary>     
        /// <typeparam name="T"></typeparam>   
        ///<param name="ds">DataSet</param>      
        /// <param name="tableName">待转换数据表名称</param>         
        /// <returns></returns>         
        public static IList<T> DataSetToIList<T>(DataSet ds, string tableName)
        {
            int _TableIndex = 0; if (ds == null || ds.Tables.Count < 0)
                return null; if (string.IsNullOrEmpty(tableName))
                return null;
            for (int i = 0; i < ds.Tables.Count; i++)
            {
                if (ds.Tables[i].TableName.Equals(tableName))
                {
                    _TableIndex = i;
                    break;
                }
            }
            return DataSetToIList<T>(ds, _TableIndex);
        }
View Code

 

posted @ 2013-12-22 12:01  mmww  阅读(171)  评论(0)    收藏  举报