Dynamics CRM2013 Lookup Filtering using addCustomFilter

dynamics crm中对lookup视图的过滤是一个很平常性的需求,在2011的时候都是用添加自定义视图的方式例如下面这段示例代码

<span style="font-size: 18px;">var pEntityName = "sc_stock";
    var pViewDisplayName = "符合条件库存";
    var </span><span style="font-size:18px;">pSotckControl</span><span style="font-size: 18px;">= Xrm.Page.getControl("new_stock");
     
    var pFetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
                    + "<entity name='sc_stock'><attribute name='sc_stockid'/>"
                    + "<attribute name='sc_stocksn'/><attribute name='createdon'/>"
                    + "<attribute name='sc_salebillamount'/><attribute name='sc_outputamount'/>"
                    + "<attribute name='sc_bizbookresidue'/><attribute name='sc_bizbookamount'/>"
                    + "<attribute name='sc_owner'/><attribute name='sc_storage'/>"
                    + "<attribute name='sc_stocktype'/>"
                    + "<attribute name='sc_uom'/>"
                    + "<attribute name='sc_tank'/>"
                    + "<attribute name='sc_product'/>"
                    + "<attribute name='sc_bizunit'/>"
                    + "<attribute name='sc_bizoperator'/>"
                    + "<attribute name='sc_purchase'/>"
                    + "<order attribute='sc_stocksn' descending='false'/>"
                    + "<filter type='and'>"
                    + "<condition attribute='sc_decreaseandoverflow' operator='eq' value='" + this.sc_isDecreaseandoverflow + "'/>"                    
                    + "</filter>"
                    + "</entity>"
                    + "</fetch>"

     var pLayoutXml = "<grid name='resultset' object='10122' jump='sc_stocksn' select='1' icon='1' preview='1'>"
                        + "<row name='result' id='sc_stockid'>"
                        + "<cell name='sc_stocksn' width='100'/><cell name='sc_purchase' width='100'/>"
                        + "<cell name='sc_bizunit' width='100'/><cell name='sc_bizoperator' width='100'/>"
                        + "<cell name='sc_product' width='100'/><cell name='sc_owner' width='100'/>"
                        + "<cell name='sc_storage' width='100'/><cell name='sc_tank' width='100'/>"
                        + "<cell name='sc_stocktype' width='100'/><cell name='sc_uom' width='100'/>"
                        + "<cell name='sc_bizbookamount' width='100'/><cell name='sc_bizbookresidue' width='100'/>"
                        + "<cell name='sc_outputamount' width='100'/><cell name='sc_salebillamount' width='100'/>"
                        + "<cell name='createdon' width='125'/>"
                        + "</row></grid>"
     var id = "{CFD4B604-1C6A-4E61-B057-BA07620C0D46}";         
     pSotckControl.addCustomView(id, pEntityName, pViewDisplayName, pFetchXml, pLayoutXml, false);
     pSotckControl.setDefaultView(id);</span>
要写一大堆的查询XML,显示XML很是繁琐

2013中加入了addCustomFilter这个API,

<span style="font-size:18px;">Xrm.Page.getControl("new_stock").addPreSearch(function () {
        var filter = "<filter type='and'>"
        + "<condition attribute='sc_decreaseandoverflow' operator='eq' value='" + this.sc_isDecreaseandoverflow + "'/>"
        + "</filter>";
        Xrm.Page.getControl("new_stock").addCustomFilter(filter);
    });</span>
代码极度简化,XML也只是用到了一个condition条件而已,只是对默认的视图添加了搜索过滤而不是添加新的自定义视图。

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2014-07-22 10:01  Vic Tang  阅读(271)  评论(0编辑  收藏  举报