前端代码
更新时间: 2024-02-21 浏览次数: {{ hits }}

如何获取前台控件的值?

答:获取前台控件的值使用前端控件的GetValue()方法,该方法适用于文本控件、日期控件、数值、选项控件、关联表单、子表控件。

示例:

OnValidate: function() {

    var AccountNameManager   = this.AccountName; //获取控件

    if( AccountNameManager   == null || accountName == undefined)     //判断控件是否存在

        return true;

    var accountName =   AccountNameManager.GetValue();  //获取控件的值

    if(accountName   =="")

        return false;

    return true;

}

 获取人员/部门控件值?

答:this.控件编码. GetUnitIDs()。

示例:

this.F000001.GetUnitIDs();//获取的是个数组对象,可通过判断长度 length来遍历;

 如何判断表单模式?

示例:

$.SmartForm.ResponseContext.FormMode   // 0为审批/办理 1为办理完结 2为创建 4为查阅

如何给控件赋默认值?

答:在OnLoad方法里,书写控件赋值。

示例:

OnLoad: function() {

var company=”深圳奥哲网络科技有限公司”;

var name =”Jerry Zhou”;

this.AccountName.SetValue( company ); // 给文本控件AccountName赋值

this.ContactName.SetValue( name );  //给文本控件ContactName赋值

}

如何设置控制不可修改?

答:通过控件的SetReadonly(true)方法控件控件的不可修改,改方式适用于文本、数值、选项、关联表单控件。

示例:

OnLoad: function() {

this.AccountName.SetReadonly(true);

}

 如何隐藏控件控件不可见?

答:通过控件的SetVisible(false)方法设置控件隐藏不可见。

示例:

OnLoad: function() {

this.AccountName.SetVisible(false); // 页面加载时设置AccountName控件隐藏。

}

 如何给下拉框、单选框、多选框添加选项值?

答:下拉框、单选框、多选框可以通过ClearItems()方法清除所有选项,通过AddItem(value)方法添加可选项。

示例:

OnLoad: function() {

this.Company.ClearItems();//清除下拉框Company控件的所有选项

this.Company.AddItem("奥哲网络科技"); //给下拉框控件Company添加选项

}

 

如何在表单前端提交时校验子表数据?

答:OnValidate事件中书写return false可中止提交。

前端代码:

// 提交校验

OnValidate: function( actionControl ) {

//将OnValidate函数中this对象暂存

var parent = this;

//判断当前点击的按钮编码是否是"Submit"

if( actionControl.Action == "Submit" ) {

//获取子表数据,值是一个数组,每个元素对应子表数据的一行(D00021CCC即子表控件编码)

var childTableData = parent.D00021CCC.GetValue();

for( var i = 0;i < childTableData.length;i++ ) {

var itemData = childTableData[ i ];

//判断子表当前行数据ProjectName控件值是否为空,为空则不允许提交

if(!itemData[ "ProjectName" ]){

//弹出警告消息

$.IShowWarn( "警告", "ProjectName控件值为空,请输入!" );

//OnValidate事件中书写return false可中止提交

return false;

}

}

}

//其他按钮点击不中止

return true;

}

 

如何控制子表某一列的值变更时,控制其他列的值?

答:在子表中经常存在有当某一列的值变更是触发带出同一行的其他列的值。比如在销售订单中,填写某个产品采购数量,在根据单采购价自动计算出总价。

示例:

var   parent = this;

//绑定子表变化事件

this.D000024Fdetail123.BindChange("Set",

function(data)   {

    var responseData = data[0]; // 当前行

    //判定‘quantity’字段值是否变化了

    if (responseData != null &&   responseData.DataField == "D000024Fdetail123.quantity") {

        var currentRowId = responseData.ObjectId;   //获取行ID 

        var quantity =   this.GetCellManager(currentRowId,   "D000024Fdetail123.quantity").GetValue(); //获取数量

        var price =   this.GetCellManager(currentRowId,   "D000024Fdetail123.price").GetValue(); //获取单价

        var total = parseFloat(quantity) *   parseFloat(price) //合计

        //更新当前行‘total’的字段值

          parent.D000024Fdetail123.UpdateRow(currentRowId, {

              "D000024Fdetail123.total": total

        });

    }

})

 如何绑定单击事件?

答:BindClickEvent绑定单击事件,只能用于单行文本、数值控件。

示例:

this.F0000008.BindClickEvent(function()   {

        alert( "213" ); //弹出消息窗体

});

 如何绑定子表单击事件?

答:BindClickEvent绑定单击事件,只能用于单行文本、数值控件。

示例:

var   that = this;

var   rows = this.D001227F9c12e8459ed34a67a500c761bb84754e.GetValue(); //获取子表对象

for(   var i = 0;i < rows.length;i++ ) {

   var objectId = rows[ i ][   "ObjectId" ]; //获取行ID

   console.log( objectId );

    var urlLinkManager =   this.D001227F9c12e8459ed34a67a500c761bb84754e.GetCellManager( objectId,   "D001227F9c12e8459ed34a67a500c761bb84754e.F0001229" ); //获取子表‘F0001229’字段对象

 

      //BindClickEvent绑定单击事件,只能用于单行文本、数值控件

       urlLinkManager.BindClickEvent(   function() {

                alert( "213" ); //弹出消息窗体

            });

        } 

如何书写自定义打印页面?

答:首先在后端增加个自定义打印按钮,然后在前端绑定这个按钮事件,自行书写需要打印html页面,然后替换当前页面,在调用window.print()输出打印。

示例:

后端增加打印按钮代码:

protected   override void OnLoad(H3.SmartForm.LoadSmartFormResponse response)

{

        base.OnLoad(response);

        Dictionary < string,   H3.SmartForm.ViewAction > dicActions = new Dictionary<string,   H3.SmartForm.ViewAction>();

        //重新添加按钮

          dicActions.Add("CustomPrint", new   H3.SmartForm.ViewAction("CustomPrint", "打印", ""));

foreach(KeyValuePair   < string, H3.SmartForm.ViewAction > action in response.Actions)

        dicActions.Add(action.Key,   action.Value);

        response.Actions = new   Dictionary<string, H3.SmartForm.ViewAction>();

        response.Actions = dicActions;

}

 

前端绑定打印按钮代码:

  // 提交后事件

    AfterSubmit: function( action,   responseValue ) {

        //判断点击的按钮

        if( action == "CustomPrint"   ) {

            var html = "<div>自定义打印的HTML页面</div>";

            window.document.body.innerHTML =   html;

            responseValue.ClosePage = false;

            responseValue.Refresh = false;

            window.print(); //输出打印页面

        }

    }

如何阻止页面刷新/关闭?

答:在前端代码的提交后事件设置属性值。

示例:

// 提交后事件

AfterSubmit:   function( action, responseValue ) {

responseValue.ClosePage   = false;//阻止页面关闭

responseValue.Refresh   = false; //阻止页面刷新

}

表单前端获取审批时填入的审批意见?

// 提交前事件

BeforeSubmit: function( action, postValue ) {

//Activity3节点审批同意时获取审批意见

if( action == "Submit" && $.SmartForm.ResponseContext.ActivityCode == "Activity3" ) {

var comment = postValue.Comment.Text;

}


//Activity3节点审批不同意时获取审批意见

if( action == "Reject" && $.SmartForm.ResponseContext.ActivityCode == "Activity3" ) {

var comment = postValue.Comment.Text;

}

}

如何在表单前端OnLoad事件获取当前登录人信息?

答:由于表单前端无当前登录人信息,所以需要PostForm到后端获取。

前端代码:

// 加载事件

OnLoad: function() {

//将OnLoad函数中this对象暂存,因为PostForm回调函数中的this指向与OnLoad中的this指向不一致

var parent = this;


//PostForm请求后端,actionName为"GetCurrentLoginUser"

$.SmartForm.PostForm( "GetCurrentLoginUser", {},

function( data ) {

if( data.Errors && data.Errors.length ) {

//将后端添加到Errors集合里的异常信息弹出

$.IShowError( "错误", JSON.stringify( data.Errors ) );

} else {

//取出后端响应的数据

var result = data.ReturnData;


//取出result对象的UserId属性值,并赋值到一个人员单选控件中

parent.F0000001.SetValue( result[ "UserId" ] );

//取出result对象的UserName属性值,并赋值到一个单行文本控件中

parent.F0000002.SetValue( result[ "UserName" ] );

//取出result对象的UnitId属性值,并赋值到一个部门单选控件中

parent.F0000003.SetValue( result[ "UnitId" ] );

}

},

function( error ) {

//将后端未知异常弹出

$.IShowError( "错误", JSON.stringify( error ) );

}, false );

}

后端代码:


protected override void OnSubmit(string actionName, H3.SmartForm.SmartFormPostValue postValue, H3.SmartForm.SubmitSmartFormResponse response)

{

//根据actionName判断当前请求是否是为了获取当前登录人信息

if(actionName == "GetCurrentLoginUser")

{

//获取当前登录用户的用户对象

H3.Organization.User user = this.Request.UserContext.User;


//默认response.ReturnData值为null,所以此处需要初始化

response.ReturnData = new Dictionary<string, object>();

//将当前登录用户的用户Id响应回前端

response.ReturnData.Add("UserId", user.ObjectId);

//将当前登录用户的用户姓名响应回前端

response.ReturnData.Add("UserName", user.Name);

//将当前登录用户的主部门Id响应回前端

response.ReturnData.Add("UnitId", user.ParentId);

}


base.OnSubmit(actionName, postValue, response);

}



如何让关联表单重新填充

this.编码.SetValue(this.编码.GetValue);


如何OnKeyDown里面获取元素最新值

目前氚云中表单控件设计的机制是这样,在没有失去焦点时值是没有更新的,通过GetValue拿到的就是旧值,在失去焦点后才能取到最新值。 

如果有场景需要在OnKeyDown时取到最新值,可参考如下代码。


可使用event.srcElement.value获取最新的值,参考代码:

parent.F0000001.OnKeyDown( function( event ) {

if( event.keyCode == 13 ) {  

var text = event.srcElement.value;  

parent.D000183Ff03d804c7768487e9266bfb0caddff7c.AddRow( $.IGuid(), {

"D000183Ff03d804c7768487e9266bfb0caddff7c.F0000003": text

}); 

}

})