Saturday, September 6, 2014

Get Item details and bind values to multiple control Using ECMA Script in Sharepoint



Get the Item details using ecma script and bind to multiple controls.

1.  get item id from query string
2.  get item details
3.  get drop down list index and bind to value to this
4.  get multiple radio button list and bind value to this.
5.  get textbox and bind value to this.
6.  get multi choice and add and remove value to this.

       // get the ID value from the URL

        var itemId = GetUrlKeyValue("rId", false, location.href); 

          if (itemId > 0 || itemId!=="")
           {
             GetItem();
           }
          function GetItem()
          {                            
            clientContext= new SP.ClientContext.get_current();
            web = clientContext.get_web();
            list = web.get_lists().getByTitle('RMMChangeProcess');
            listItem = list.getItemById(itemId);         
            clientContext.load(listItem);  
            clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess),      Function.createDelegate(this, onFail));
          }
         function onSuccess() {                                                 
                       
                       // get lookup value from the list
                        var strLocation= listItem.get_item("Location").get_lookupValue(); 
                       
                       //  get dropdown index and bind value that
                        var LocationId= $("select[Title='Location']").attr("id");
                        var LocationControl= document.getElementById(LocationId);                                
                        LocationControl.options[LocationControl.selectedIndex].text=strLocation;
                        
                         // get lookup value from the list
                        var strCategory= listItem.get_item("Category").get_lookupValue(); 
                      
                       //  find radio button list in the div and if option value is find then check that
                        $("#divCategory").find("input[type='radio']").each(function(){                              
                              var Category= $(this).closest('span').attr("title");                            
                               if(Category==strCategory)
                               {                                 
                                  $(this).prop('checked', true);                              
                               }                       
                        });                       
                                                
                        var RawMatcode = listItem.get_item("RawMaterialCode");
                        
                       // get textbox value in the table row and bind value to this
                        var Rmcode= document.getElementById('tr').getElementsByTagName('td') [1].getElementsByTagName('input'); 
                        Rmcode[0].value=RawMatcode ;
                        Rmcode[0].readOnly=true;

                        var supplier= listItem.get_item("PresentSupplier"); 
                        
                         // remove value from multi choice list
                        var arrSuppliers=[];
                        for (var i = 0; i < supplier.length; i++)
                        {
                          arrSuppliers.push(supplier[i].get_lookupId()+";" +  supplier[i].get_lookupValue());
                           // by value
                       $("[id$='_SelectCandidate'] option[value="+ supplier[i].get_lookupId()+"]").remove();
                           // by text                //$("[id$='_SelectCandidate']option:contains("+supplier[i].get_lookupValue()+")").remove();                 
                        }
                       
                       // add the value to multi choice list
                        for(var key in arrSuppliers)
                        {
                          var arrKeyValue=arrSuppliers[key].split(';')
                          // construct option item                        
                          var $resultOptions = "<OPTION title="+ arrKeyValue[1]+" value="+arrKeyValue[0] +">"+arrKeyValue[1]+"</OPTION>";
                      
//var $resultOptions = "<OPTION title=India value=1>India</OPTION>";                        
                          $("[id$='_SelectResult']").append($resultOptions);
                        }                            
        }
        function onFail() {
        
         alert('fail');
       }

No comments:

Post a Comment