1. 代码控制

本节将讲解如何通过JavaScript 和 SQL 代码来实现一些较为复杂的功能。

1.1. 1.JAVASCRIPT+SQL前后端交互

如下是一个应用实例

需求是:

通过客户业务表单上输入的身份证号码获取历史记录,将客户历史信息加载到当前业务表单明细表中。

实现方式:

首先前端JavaScript 得到客户身份证号,然后ajax传递给后台,调用预定义的SQL代码,完成后返回数据,装载数据到明细表。

1.javascirpt 代码如下:

function inputchange(obj){
    if($(obj).attr("name")=="ord_ncuname")
    /*sql代码定义的方法*/
    fn_load_history.runsql4xml($("td[name='ord_sfzhm']").myval(),order.ord_id,function(iseof,data,dsname){
        if(iseof&&!dsname){
            if($f[result]=="OK") 
                load1detail("table_4434");/*刷新明细*/
        } 
    })
}

2.sql代码如下,如下格式的sql 代码提交后会在后台数据库生成名为“userfunc_?_fn_load_history”的存储过程。其中fn_load_history是自己定义的方法名,JavaScript中可以直接调用,但是需要注意的是JavaScript的参数需要逐个给出,即“paramA,paramB...”的形式,而不能用json格式。

/*找最近一次评估记录,把它的历史评估记录和评估信息一起写入当前明细的历史记录表中,如果没有历史记录则不做处理*/
function fn_load_history(
    @identno varchar(20),
    @thisordid bigint
) 
begin
    declare @maxordid bigint ,
            @hasInserted int  = -1  /*0:写入了明细,1:没有数据写入明细*/

    set @maxordid  = @thisordid

    select @maxordid  = max(ord_id) from order_29 with(nolock)
    where ord_sfzhm = @identno 

    if(@maxordid <> @thisordid )
    begin

        insert into order_29_4(ord2_ordid,ord2_pgsj,ord2_cuname,ord2_opinion)
        select @thisordid ordidx,ord2_pgsj,ord2_cuname,ord2_opinion
        from order_29 with(nolock),order_29_2 with(nolock)
        where ord2_ordid = @thisordid
        union all
        select @thisordid ordidx,ord2_pgsj,ord2_cuname,ord2_opinion
        from order_29 with(nolock),order_29_4 with(nolock)
        where ord2_ordid = @thisordid

        set @hasInserted =@@ROWCOUNT

    end
    if(@@row_number>0)
        select 'YES' result 
    else 
        select 'NO' result
end

所有的SQL代码都会生成如下结构的前端代码,并注入到页面,所以1的JavaScript代码可以直接使用如下的一些方法。

var _t_=document.location.search.substr(1); 
var afterload={}; 
afterload.runsql=function(c1)
{
    runsql('userfunc_86_afterload&'+_t_,{},function(io,dd){
        if (c1) c1(io,dd);});
} 
afterload.runsql4xml=function(c1){
    runsql4xml('userfunc_86_afterload&'+_t_,{},function(io,dd,nn){
        if (c1) c1(io,dd,nn);
    });
} 
afterload.dialog=function(c1,t1,multi1,w1){
    pickany_user(w1,'',t1,'userfunc_86_afterload',,c1,multi1);
} 

var fn_load_history={}; 
fn_load_history.runsql=function(identno,thisordid,c1){
    runsql('userfunc_86_fn_load_history&'+_t_,{identno:identno,thisordid:thisordid},
        function(io,dd){if (c1) c1(io,dd);});
} 
fn_load_history.runsql4xml=function(identno,thisordid,c1){
    runsql4xml('userfunc_86_fn_load_history&'+_t_,{identno:identno,thisordid:thisordid},
    function(io,dd,nn){
            if (c1) c1(io,dd,nn);
    });
} 

fn_load_history.dialog=function(identno,thisordid,c1,t1,multi1,w1){
    pickany_user(w1,'',t1,'userfunc_86_fn_load_history','identno='+tohex(identno)+'&'+'thisordid='+tohex(thisordid),c1,multi1);
}

results matching ""

    No results matching ""