将源HTML元素传递给PrimeFaces p:ajax的oncomplete函数

时间:2016-01-14 01:37:14

标签: javascript ajax jsf-2 primefaces

我想将源HTML元素作为参数传递给<p:ajax oncomplete>的JavaScript回调函数。我尝试传递thisonclick通常适用于<p:ajax ... oncomplete="callbackFunction(this)" /> 等等:

namespace IMS.Model
 {
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Reflection;
using System.Linq;

[AttributeUsage(AttributeTargets.Property, Inherited = true)]
[Serializable]
public class MappingAttribute : Attribute
{
    public string ColumnName = null;
}

public partial class IMS_Controller<IMS_WorkingTable> where IMS_WorkingTable : new()
{
    private readonly string tablename;
    private readonly string keyname;

    protected IMS_Controller(string tablename, string keyname)
    {
        this.tablename = tablename;
        this.keyname = keyname;
    }

    public string _tablename { get { return tablename; } }
    public string _keyname { get { return keyname; } }
    public IEnumerable<IMS_WorkingTable> _recordset;

    IMS_WorkingTable MapToClass(IDataRecord record)
    {
        IMS_WorkingTable returnedObject = Activator.CreateInstance<IMS_WorkingTable>();
        List<PropertyInfo> modelProperties = returnedObject.GetType().GetProperties().OrderBy(p => p.MetadataToken).ToList();
        for (int i = 0; i < modelProperties.Count; i++)
            try
            {
                modelProperties[i].SetValue(returnedObject, Convert.ChangeType(record.GetValue(i), modelProperties[i].PropertyType), null);
            }
            catch { }
        return returnedObject;
    }

    public void gets(string keyval)
    {
        string sql = string.Format("SELECT * from {0} where {1}='{2}'", _tablename, _keyname, keyval);
        getIt(sql);
    }

    public string gets(string keyval, string where)
    {
        string sql = string.Format("SELECT * from {0} where {1}='{2}' {3}", _tablename, _keyname, keyval, where);
        try
        {
            getIt(sql);
            return "Sucess: " + sql;
        }
        catch
        {
            return "Error: " + sql;
        }
    }

    public void sets(string keyval, string field, string value)
    {
        setIt(keyval, field, value);
    }

    private void getIt(string strSQL)
    {
        var table = new List<IMS_WorkingTable>();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["IMS"].ConnectionString))
        {
            con.Open();
            using (var cmd = new SqlCommand(strSQL, con))
            {
                IMS_WorkingTable t = new IMS_WorkingTable();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        t = MapToClass((IDataRecord)reader);
                        table.Add(t);
                    }
                    _recordset = table;
                    reader.Close();
                    reader.Dispose();
                }
                cmd.Dispose();
            }
            con.Close();
            con.Dispose();
        }
    }

    private void setIt(string keyval, string field, string value)
    {
        var products = new List<IMS_WorkingTable>();
        var strSQL = string.Format("update {0} set {1} = '{2}' where {3}='{4}'", _tablename, field, value, _keyname, keyval);
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["IMS"].ConnectionString))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(strSQL, con);
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
            con.Dispose();
        }
    }
}
}


namespace IMS.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;

public partial class IMM_StoreOrder
{
    [Mapping(ColumnName = "StoreOrderID")]
    [Key]
    public Guid StoreOrderID { get; set; }

    [Mapping(ColumnName = "StoreUserId")]
    public Guid StoreUserId { get; set; }

    [Mapping(ColumnName = "OrderID")]
    [Required]
    [StringLength(128)]
    public string OrderID { get; set; }

    [Mapping(ColumnName = "OrderDate")]
    public DateTime OrderDate { get; set; }

    [Mapping(ColumnName = "Status")]
    [StringLength(256)]
    public string Status { get; set; }

    [Mapping(ColumnName = "DeletedFlag")]
    public bool DeletedFlag { get; set; }

    [Mapping(ColumnName = "IPAddress")]
    [StringLength(50)]
    public string IPAddress { get; set; }
}

public partial class IMS_StoreOrderController : IMS_Controller<IMM_StoreOrder>
{
    public IMS_StoreOrderController() : base("IMM_StoreOrder", "StoreOrderID") { }

}
}

它无法正常工作。这似乎是一个不同的对象。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:7)

oncomplete的上下文中,this代表PrimeFaces ajax对象,它实际上包含大量信息。您可以通过检查调试器中的对象,或者通过传递给console.log来轻松地弄清楚它,它会自动打印JS对象。

function callbackFunction(arg) {
    console.log(arg);
}

如果是..

<h:form id="formId">
    <h:commandLink id="linkId" value="test">
        <p:ajax oncomplete="callbackFunction(this)" />
    </h:commandLink>
</h:form>

..它在控制台中看起来像这样(按F12到达那里):

accepts: Object
async: true
beforeSend: (w,i)
cache: false
complete: (w,i)
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
contents: Object
converters: Object
crossDomain: false
data: "javax.faces.partial.ajax=true&javax.faces.source=formId%3AlinkId&javax.faces.partial.execute=formId%3AlinkId&javax.faces.behavior.event=action&javax.faces.partial.event=click&formId=formId&javax.faces.ViewState=-4870787666399983047%3A-2006040112896840046"
dataType: "xml"
dataTypes: Array[2]
error: (x,i,w)
flatOptions: Object
global: false
hasContent: true
isLocal: false
jsonp: "callback"
jsonpCallback: ()
portletForms: null
processData: true
responseFields: Object
source: a#formId:linkId
success: (x,i,y)
type: "POST"
url: "/playground/test"
xhr: bD()
__proto__: Object

如果仔细观察,source属性就是您最终需要的属性。

所以,只需改变你的电话:

<p:ajax ... oncomplete="callbackFunction(this.source)" />
相关问题