MVC+jQuery数据绑定①:列表绑定(三)

本文介绍了一种通过模板绑定和数据源动态生成列表的方法。利用自定义jQuery插件实现列表的绑定,支持从远程获取数据并填充到指定的HTML模板中。支持多种参数配置,并提供示例代码。

除了绑定Table外,还有其他的循环列表需要绑定。

其实都一样,都是把模版化成数据。 模板的存法用注释的方式只是一种方法。更好的方法自由发挥。目前就是为了取得模板。
比如 用一个Json来存放需要的Template,然后专门放在一个templates.js里或由xml配置的模板然后生成json数据, 需要用的时候直接 templates.tempName 就可以了。
写博文太累了。 特别是自己的思想给别人分享时更累。 我这里也不赘述了。贴下代码吧
ContractedBlock.gif ExpandedBlockStart.gif 代码

    
; ( function ($) {
var templateList = []; // 存放一般的模板列表

$.fn.bindList
= function (args, template, options, async) {
var me = this ;

var url, async, template, dataSource;

url
= me.attr( " datasource " );
async
= me.attr( " async " ) == " 0 " ;
if (arguments.length > 0 ) {
switch ( typeof (args)) {
case " string " :
url
= args;
break ;
case " object " :
dataSource
= args;
break ;
}
}
// args是json
switch (arguments.length) {
case 1 :
if (args == null )
return me;
if ( typeof (args) == " string " ) {
url
= args;
}
else if ( typeof (args) == " object " ) {
url
= args.url;
async
= args.async || async;
template
= args.template || template;
options
= args.options || options;
dataSource
= args.data;
}
break ;
case 2 : // url,async || url,tempalte || data,template
if ( typeof (arguments[ 0 ]) == " string " )
url
= arguments[ 0 ];
else
dataSource
= arguments[ 0 ];

if ( typeof (arguments[ 1 ]) == " bool " ) {
async
= arguments[ 1 ];
template
= me.getTemplate();
}
break ;
case 3 : // url,template,async || url,template,options || data,template,options
template = template;
options
= options;
if ( typeof (options) == " bool " ) {
async
= options;
options
= undefined;
}
break ;
case 4 :
template
= template;
options
= options;
async
= async;
break ;
}


if ((url == undefined || url == "" ) && dataSource == undefined) {
return me;
}

if (dataSource == undefined) {
$.ajax({
type:
" POST " ,
async: async,
url: url,
success:
function (data) {

if (data == "" || data == null )
return ;
data
= eval( " ( " + data + " ) " );

me.bindListData(data.list
|| data, template, options);
}
});
}
else {
me.bindListData(dataSource, template, options);
}
return me;
};
$.fn.bindListData
= function (data, template, options) {

var me = this ;
template
= template || me.getTemplate();
this .empty();
var resultTemplate = replaceTemplate(template, data);


var prepend, append, dvalue;
if (options != undefined) {
prepend
= options.prepend;
append
= options.append;
dvalue
= options.dvalue;
}

if (prepend != undefined) {
resultTemplate
= prepend + resultTemplate;
}



if (append != undefined) {
resultTemplate
+= append;
}

this .html(resultTemplate);

// 绑定控件的默认值
if ( this .attr( " type " ) == " select-one " ) {
if (dvalue != undefined)
this .bindControl(dvalue);
else
this [ 0 ].selectedIndex = 0 ;
}

me.setLink();

};


})(jQuery);

 

转载于:https://www.cnblogs.com/mad/archive/2009/09/11/1564902.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值