日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【jquery模仿net控件】初步GridView模型实现,及其简单应用

發(fā)布時(shí)間:2023/11/30 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【jquery模仿net控件】初步GridView模型实现,及其简单应用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近日子不好過(guò),主要原因是要畢業(yè)了,學(xué)校那邊的畢業(yè)論文讓人很頭痛,就跟寫(xiě)八股文似的,個(gè)人非常頭疼并且厭恨這種

對(duì)我無(wú)意義的東西,哎!!!體制啊,既然無(wú)法改變,何不將之做好呢!!!對(duì),于是我還是不想寫(xiě)論文,拖著吧,到哪天確實(shí)

拖不下去了再說(shuō);加之公司項(xiàng)目也非常緊,又要寫(xiě)論文,雖說(shuō)沒(méi)寫(xiě),心里就是一個(gè)不舒服啊!!!所以平時(shí)抽了點(diǎn)時(shí)間模擬了下

gridview,個(gè)人是菜鳥(niǎo)自不必多說(shuō),但是這幾個(gè)月來(lái),模擬寫(xiě)著寫(xiě)著感覺(jué)學(xué)到了不少東西喲!!!

算了....扯遠(yuǎn)了,進(jìn)入正文。

?

前期工作


之前主要模擬了一次datalist,基本上以它為基礎(chǔ)將igoogle功能完全實(shí)現(xiàn),并且應(yīng)用到項(xiàng)目中去了,控件不好,但也不是完全沒(méi)有意義,

過(guò)程中需要類(lèi)似gridview的功能,索性也就寫(xiě)了一下,思路與datalist差距不大。

因?yàn)轳R上要寫(xiě)論文了,而且是不完全版本這里就不畫(huà)圖什么的了,以后發(fā)完整版本再說(shuō)吧:

?

效果圖


?

簡(jiǎn)單說(shuō)明


該簡(jiǎn)單應(yīng)用也是我第一次做測(cè)試,有一下幾個(gè)事件:

① 鼠標(biāo)滑動(dòng)時(shí)變色事件

② 點(diǎn)擊大類(lèi)選取小類(lèi)事件

③ 點(diǎn)擊獲取時(shí)候全部獲取事件

④ 當(dāng)然,不能忘了行綁定事件,初期只是綁定了大類(lèi),小類(lèi)根據(jù)大類(lèi)id作出的加載

?

核心代碼


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title></title><script src="../js/jquery-1.7.1.min.js" type="text/javascript"></script><script src="http://www.cnblogs.com/../scripts/jquery-1.7.1.js" type="text/javascript"></script><script src="../js/GridViewBase.js" type="text/javascript"></script><script src="../js/GridColum.js" type="text/javascript"></script><script src="../js/GridRow.js" type="text/javascript"></script><script src="../js/GridView.js" type="text/javascript"></script><script src="http://www.cnblogs.com/../scripts/jquery.cookie.js" type="text/javascript"></script><script type="text/javascript">$(document).ready(function () {var url = '../Ajax.aspx?sql=select * from bigType ';$.getJSON(url, function (data) {var g = new GridView();$("#www").click(function () {var rows = g.rows;var str = "";$.each(rows, function (i, row) {var checks = row.getEl("input:checkbox");checks.each(function (ii, jj) {if ($(this).attr("checked")) {str += $(this).val() + " , "}});});alert(str);});g.style.width = "500px";g.colModel = [{'th': { 'title': '大類(lèi)' },'td': { 'template': '{%title%}<input value="{%id%}" type="checkbox" class="bigId" />', 'attribute': {}, 'style': {} }}, {'th': { 'title': '小類(lèi)' },'td': { 'template': '<div class="type"></div>', 'attribute': {}, 'style': {} }}];g.trEvent = [{eventType: 'ready',func: function (param, e) {var scope = this;var tr = scope.el;var back = tr.css('background-color');tr.unbind('mousemove');tr.bind('mousemove', function () {tr.css('background-color', '#efefef');});tr.unbind('mouseout');tr.bind('mouseout', function () {tr.css('background-color', back);});}},{eventType: 'ready',func: function (param, e) {var scope = this;var type = scope.getEl(".type");var id = param.id;var _url = "../Ajax.aspx?sql=select * from smallType where bigTypeId='" + id + "' "$.getJSON(_url, function (data) {$.each(data, function (i, item) {var html = ' ' + item.title + '<input value="' + item.id + '" type="checkbox" class="sId" disabled="disabled" />';type.append(html);});var bigId = scope.getEl(".bigId");bigId.unbind("change");bigId.bind("change", function () {var smallTyle = scope.getEl(".sId");if (bigId.attr('checked') == 'checked') {smallTyle.attr("checked", "checked");smallTyle.removeAttr('disabled');} else {smallTyle.removeAttr('checked');smallTyle.attr('disabled', 'disabled');}});});}}];g.dataSource = data;g.render();});});</script> </head> <body><input type="button" value="獲取" id="www" /> </body> </html>

?

/// <reference path='jquery-1.7.1.min.js' /> /// <reference path='GridViewBase.js' /> /// <reference path='GridColum.js' /> /// <reference path='GridRow.js' />/* 控件生成流程: 1 先初始化表格每列的【列頭】以及【body信息】,最好可指定列id,用于后期檢索 2 賦予控件二維數(shù)據(jù)(但控件字段最好可支持多維數(shù)據(jù)獲取) 3 初始化外圍table,并賦予其屬性樣式,最外層結(jié)束 4 根據(jù)二維數(shù)據(jù)依次初始化每行數(shù)據(jù),【其實(shí)可在此處初始化表頭,因?yàn)槿羰菦](méi)有數(shù)據(jù)表頭將不初始化】 5 每行獲得其數(shù)據(jù)源,先形成tr,然后將數(shù)據(jù)源直接賦予單元行進(jìn)行初始化 6 單元化格式化單元模板,生成html標(biāo)簽*/var GridView = function (id) {var scope = this;this.style = {};this.attribute = {id: id && id.length > 0 ? id : new Date().getTime().toString(),className: 'wl'};this.id = this.attribute.id;this.thId = this.id + '_header';this.dataSource = [];this.header = null;this.rows = [];this.el = null;this.parentData = null;this.thEvent = [{func: function (rows, e) {var scope = this;var select = scope.getThEl('.th_select');select.unbind('click');select.bind('click', function (e) {var th_s = select.attr('checked');$.each(rows, function (i, row) {var td_select = row.getEl('.td_select');// var td_s = td_select.attr('checked');if (th_s && th_s == 'checked') {td_select.attr('checked', 'checked');} else {td_select.removeAttr('checked');}});});}}];this.trEvent = [{eventType: 'ready',func: function (param, e) {var scope = this;var tr = scope.el;var back = tr.css('background-color');tr.unbind('mousemove');tr.bind('mousemove', function () {tr.css('background-color', '#efefef');});tr.unbind('mouseout');tr.bind('mouseout', function () {tr.css('background-color', back);});}},{eventType: 'ready',func: function (param, e) {var scope = this;var edit = scope.getEl('.td_edit');edit.unbind('click');edit.bind('click', function (ee) {var div = scope.getEl('div');div.hide();});}}];this.tdEvent = [{// eventType: 'click',// func: function (param, e) {// var scope = this;// var td = scope.el;// var input = $('<input style="width:100%;height:100%;" value="' + td.text() + '" />');// td.html(input);// }}];this.preInit = new EventHandler();this.initComplete = new EventHandler();this.preLoad = new EventHandler();this.loadComplete = new EventHandler();this.parentEl = $('body');this.colModel = [{'th': { 'title': '<input value="" type="checkbox" class="th_select" />', 'attribute': {}, 'style': {} },'td': { 'type': '', 'value': '', 'template': '<input value="{%title%}" type="checkbox"class="td_select" />', 'attribute': {}, 'style': {} }}, {'th': { 'title': '111', 'attribute': {}, 'style': {} },'td': { 'type': '', 'value': '', 'template': '<input style="display:none;" value="{%title%}" /><div class="td_title">{%title%}</div>', 'attribute': { className: 'wlR' }, 'style': { 'color': 'Blue'} }}, {'th': { 'title': '222', 'attribute': {}, 'style': {} },'td': { 'type': '', 'value': '', 'template': '<textarea style="display:none;" >{%summary%}</textarea><div class="td_summary">{%summary%}</div>', 'attribute': {}, 'style': {} }}, {'th': { 'title': '編輯', 'attribute': {}, 'style': {} },'td': { 'type': '', 'value': '', 'template': '<input type="button" value="編輯" class="td_edit" />', 'attribute': {}, 'style': {} }}];}; GridView.prototype.render = function () {var scope = this;scope.onInit();scope.onLoad();scope.eventBind();};GridView.prototype.onInit = function () {var scope = this;scope.preInit.invoke();scope.initHtml();scope.initAttr();scope.initStyle();scope.initComplete.invoke();}; GridView.prototype.initHtml = function () {var scope = this;var el = $('<table></table>');scope.el = el;var parentEl = scope.parentEl;parentEl.append(el);scope.initHeader(); }; GridView.prototype.initHeader = function () {var scope = this;var header = $('<tr></tr>');header.attr('id', scope.thId);var model = scope.colModel;$.each(model, function (i, col) {var th = col['th'];if (th) {var val = th['title'];var td = $('<th>' + val + '</th>');header.append(td);}});scope.header = header;var table = scope.el;table.append(header); }; GridView.prototype.initAttr = function () {utils.initAttr(this); }; GridView.prototype.initStyle = function () {utils.initStyle(this); };GridView.prototype.onLoad = function () {var scope = this;scope.preLoad.invoke();scope.loadHtml();scope.loadRows();scope.loadComplete.invoke(); }; GridView.prototype.loadHtml = function () {utils.loadhtml(this); }; GridView.prototype.loadRows = function () {var scope = this;var dataSource = scope.dataSource;$.each(dataSource, function (index, item) {var gridRow = new GridRow();gridRow.parentObj = scope;gridRow.dataSource = item;gridRow.rowIndex = index;gridRow.event = scope.trEvent;gridRow.tdEvent = scope.tdEvent;gridRow.colModel = scope.colModel;gridRow.render();scope.rows.push(gridRow);});}; GridView.prototype.eventBind = function () {var scope = this;scope.headerEventBind(); };GridView.prototype.headerEventBind = function () {var scope = this;var el = scope.el;var thEvent = scope.thEvent;$.each(thEvent, function (i, item) {var func = item.func;el.ready(function (e) {func.call(scope, scope.rows, e);});}); };GridView.prototype.getThEl = function (elementKey) {var scope = this;var thId = scope.thId;var id = "#" + thId + " " + elementKey;var element = $(id);return element; };

?

// <reference path="jquery-1.7.1.min.js" /> /// <reference path="GridViewBase.js" /> /// <reference path="GridColum.js" />var GridRow = function () {var scope = this;this.parentObj = null;this.el = null;this.style = {};this.attribute = {};this.dataSource = [];this.columns = [];this.rowIndex;this.colModel = null;this.id = "";this.event = [];this.preInit = new EventHandler();this.initComplete = new EventHandler();this.preLoad = new EventHandler();this.loadComplete = new EventHandler();}; GridRow.prototype.render = function () {var scope = this;scope.onInit();scope.onLoad();scope.eventBind(); };GridRow.prototype.onInit = function () {var scope = this;scope.parentEl = scope.parentObj.el;scope.parentId = scope.parentObj.id;scope.preInit.invoke();scope.initHtml();scope.initAttr();scope.initStyle();scope.initComplete.invoke();}; GridRow.prototype.initHtml = function () {var scope = this;var el = $('<tr></tr>');scope.el = el;var parentEl = scope.parentEl;parentEl.append(el); }; GridRow.prototype.initAttr = function () {var scope = this;utils.initAttr(this);var el = scope.el;var parentId = scope.parentId;var rowIndex = scope.rowIndex;var id = parentId + "_row_" + rowIndex;scope.id = id;scope.attribute.id = id;el.attr("id", id); }; GridRow.prototype.initStyle = function () {utils.initStyle(this); };GridRow.prototype.onLoad = function () {var scope = this;scope.preLoad.invoke();scope.loadHtml();scope.loadCols();scope.loadComplete.invoke(); }; GridRow.prototype.loadHtml = function () {utils.loadhtml(this); };GridRow.prototype.loadCols = function () {var scope = this;var dataSource = scope.dataSource;var colModel = scope.colModel;utils.formatColTemplate(this);$.each(colModel, function (i, model) {var td = model.td;var gridColumn = new GridColumn();gridColumn.parentObj = scope;gridColumn.dataSource = dataSource;gridColumn.colIndex = i;gridColumn.model = model;gridColumn.attribute = td.attribute;gridColumn.style = td.style;gridColumn.event = scope.tdEvent;gridColumn.render();scope.columns.push(gridColumn);});};GridRow.prototype.eventBind = function () {utils.eventBind(this, this.dataSource); };GridRow.prototype.getEl = function (elementKey) {var scope = this;var id = scope.id;var id = "#" + id + " " + elementKey;var element = $(id);return element; };

?

/// <reference path="jquery-1.7.1.min.js" /> /// <reference path="GridViewBase.js" />var GridColumn = function (cfg) {var scope = this;this.parentObj = null;this.el = null;this.style = {};this.attribute = {};this.type = 'field'; //暫時(shí)提供filed數(shù)據(jù)字段、template模板兩種this.model = {};this.colIndex;this.dataSource = null;this.event = [];this.preInit = new EventHandler();this.initComplete = new EventHandler();this.preLoad = new EventHandler();this.loadComplete = new EventHandler();}; GridColumn.prototype.render = function () {var scope = this;scope.onInit();scope.onLoad();scope.eventBind(); };GridColumn.prototype.onInit = function () {var scope = this;scope.parentEl = scope.parentObj.el;scope.parentId = scope.parentObj.attribute.id;scope.preInit.invoke();scope.initHtml();scope.initAttr();scope.initStyle();scope.initComplete.invoke();};GridColumn.prototype.initBody = function (td, dataSource) {var scope = this;var parentEl = scope.parentEl;var templateObj = td['templateObj'];var tempHtm = "";$.each(templateObj, function (i, item) {if (item.field) {tempHtm = tempHtm + item.html + dataSource[item.field];} else {tempHtm = tempHtm + item.html;}});var newHtm = tempHtm;var reg = /\[%(.*?)%\]/;var para = reg.exec(newHtm);while (para && para.length > 0) {var len = para.index;var comStr = para[0].substr(2, para[0].length - 4);var s1 = newHtm.substr(0, len);var s2 = newHtm.substr(len + para[0].length);newHtm = s1 + eval(comStr) + s2;para = reg.exec(newHtm);}tempHtm = newHtm;var td = $('<td>' + tempHtm + '</td>');scope.el = td;parentEl.append(td);}; GridColumn.prototype.initHtml = function () {var scope = this;var dataSource = scope.dataSource;var col = scope.model;var td = col['td'];scope.initBody(td, dataSource);};GridColumn.prototype.initAttr = function () {var scope = this;var el = scope.el;var parentId = scope.parentId;var id = parentId + "_"+scope.colIndex;scope.id = id;scope.attribute.id = id;utils.initAttr(this);el.attr("id", id);};GridColumn.prototype.initStyle = function () {utils.initStyle(this); };GridColumn.prototype.onLoad = function () {var scope = this;scope.preLoad.invoke();scope.loadHtml();scope.loadComplete.invoke(); }; GridColumn.prototype.loadHtml = function () {}; GridColumn.prototype.eventBind = function () {utils.eventBind(this, this.dataSource); };

?

/// <reference path="jquery-1.7.1.min.js" />var Delegate = function (func, sender, param) {this.target = sender;this.method = func;this.invoke = function () {if (func && typeof (func) == "function") {func.call(sender, param);};}; };var EventHandler = function () {this.delegate = [];this.add = function (func, sender, param) {var delegate = new Delegate(func, sender, param);this.delegate.push(delegate);};this.remove = function (func) {for (var i = 0, len = this.delegate.length; i < len; i++) {if (this.delegate[i][func] == func) {this.delegate[func] = null;}}};this.invoke = function () {for (var i = 0, len = this.delegate.length; i < len; i++) {this.delegate[i].invoke();}}; };var GridViewUtils = function () { }; GridViewUtils.prototype.initStyle = function (sender) {var scope = sender;var el = scope.el;$.each(scope.style, function (key, value) {if (typeof (key) == 'string' && key.length > 0) {el.css(key, value);}}); }; GridViewUtils.prototype.initAttr = function (sender) {var scope = sender;var el = scope.el;$.each(scope.attribute, function (key, value) {if (typeof (key) == 'string' && key.length > 0) {if (key == 'className')key = 'class';el.attr(key, value);}}); }; GridViewUtils.prototype.eventBind = function (sender,param) {var scope = sender;var el = scope.el;$.each(scope.event, function (i, item) {var func = item.func;var eventType = item.eventType;if (eventType == "ready") {el.ready(function (e) {func.call(scope, param, e);});} else {el.unbind(eventType);el.bind(eventType, function (e) {func.call(scope, param, e);});}}); }; GridViewUtils.prototype.loadhtml = function (sender) {// var scope = sender;// var parentEl = scope.parentEl;// var el = scope.el;// parentEl.append(el); }; GridViewUtils.prototype.formatColTemplate = function (sender) {var scope = sender;var model = scope.colModel;var reg = /\{%[\w]*\%}/;$.each(model, function (i, v) {model[i]['td']['templateObj'] = [];var template = v['td']['template'];var para = reg.exec(template);var tempHtml = template;while (para && para.length > 0) {var len = para.index;var temp = {};temp.html = tempHtml.substr(0, len);temp.field = para[0].substr(2, para[0].length - 4);model[i]['td']['templateObj'].push(temp);tempHtml = tempHtml.substr(len + para[0].length);para = reg.exec(tempHtml);}var end = {};end.html = tempHtml;model[i]['td']['templateObj'].push(end);var sss = "";}); }; var utils = new GridViewUtils();

?

?

后續(xù)


做的過(guò)程中,想模擬.net控件的生命周期,并且實(shí)現(xiàn)委托那種高級(jí)東西,只不過(guò)自己學(xué)的太淺了,有點(diǎn)不夠力啊。

詳細(xì)請(qǐng)看下個(gè)版本吧,此版本歡迎拍磚

?

?

?

?

?

?

?

?

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/yexiaochai/archive/2012/05/17/2507029.html

總結(jié)

以上是生活随笔為你收集整理的【jquery模仿net控件】初步GridView模型实现,及其简单应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。