- <html>
- <style type="text/css">
- span.note {
- font-size: 140%;
- color: red;
- }
-
- .textboxclass {
- width: 300px;
- }
- }
- div#AddListData {
- margin-left: 200px;
- border: 0px outset;
- width: 600px;
- margin-top: -40px;
- background-color: #f3e7e7;
- }
- td {
- font-size: 15px;
- }
- table#AddLeaveTable {
- padding-left: 10px;
- margin-left: 75px;
- margin-top: 5px;
- margin-bottom: 5px;
- border-collapse: separate;
- border-spacing: 15px;
- }
- </style>
- <script type="text/javscript"> $(function () { var ddlSample=""; var DlJob=""; var Ownurl = "URURL/_api/web/Lists/getByTitle('products')/items?select=Title,ID"; $.ajax({ url: Ownurl, headers: { Accept: "application/json;odata=verbose" }, async:false, success: function (data) { var items = data.d.results; if (data.d.results.length > 0) { for (var i = 0; i
- < data.d.results.length; i++) { if (data.d.results[i].Title!=n ull) { var StrJob=data.d.results[i][ "Title"]; DlJob+='<option value=' +data.d.results[i].ID+ '>'+StrJob+ '</option>';}} ddlSample='<select id="txtlev"> <option value=0>Select</option> ' +DlJob+ ' </select>'; $( "#txtlev").html(ddlSample); } function jsFunction(value) { var val=value; var ddlSamples="" ; var DlJobs="" ; var Ownurl="URURl/_api/web/Lists/getByTitle('SubProducts')/items$select=Title,ID,Product/ID,Product/Title&$expand=Product/ID&$filter=Product/ID%20eq%20" +val+ ""; $.ajax({ url: Ownurl, headers: { Accept: "application/json;odata=verbose" }, async:false, success: function (data) { var items=d ata.d.results; if (data.d.results.length> 0) { for (var i = 0; i
- < data.d.results.length; i++) { if (data.d.results[i].Title!=n ull) { var StrJobs=data.d.results[i][ "Title"]; DlJobs+='<option value=' +StrJobs+ '>' +StrJobs+ '</option>';console.log(DlJobs);}} ddlSamples='<select id="category"><option value="0">Select</option>' +DlJobs+ '</select>'; $( "#category").html(ddlSamples); } },eror: function (data) { alert( "An error occurred. Please try again."); } }); } function CreateListItem() { var clientContext=n ew SP.ClientContext( 'URURL'); var List=c lientContext.get_web().get_lists().getByTitle( 'MasterList'); var itemCreateInfo=n ew SP.ListItemCreationInformation(); this.ListItem=L ist.addItem(itemCreateInfo); var title=document.getElementById( "adtitle").value; var category=document.getElementById( "txtlev").value; var subcategory=document.getElementById( "category").value; ListItem.set_item( 'Title',title); ListItem.set_item( 'category',category); ListItem.set_item( 'subcategory',subcategory); ListItem.update(); clientContext.load(ListItem); clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); } function onQuerySucceeded() { alert( 'Success'); } function onQueryFailed(sender, args){ alert( 'Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); } </script>
-
- <body>
- <div id="AddListData">
- <table id='AddLeaveTable'>
- <div>
- <tr>
- <td> Ad Title</td>
- <td><input type="text" id="adtitle" /> </td>
- </tr>
- </div>
- <tr>
- <td>Category</td>
- <td> <select id="txtlev" style="width: 99%; height: 90%" onmousedown="this.value='';" onchange="jsFunction
- (this.value);">
- <option value=0>Select</option>
- </td>
- </tr>
- <tr> <td>subcategory</td><td><select name="category" id="category"><option value=0>Select</option></select> </td>
- </tr>
- <tr>
- <td></td>
- <td> <input type='button' id='btnSubmit' onclick="CreateListItem()" value='Submit' /></td>
- </tr>
- </table>
- </div>
- </body>
-
- </html>
Here i am Using Products as my 1st list whereTitle,ID as their Fields. SubProducts as my 2nd list where Title,ID as their Fields. MasterList as 3rd list(it for storing values ).
Step 1
Include Min.js
Step 2
Check data.d.results.length > 0) If its True for 1st dropdown fetch the id,title using RestQuery,Itterate those results(data.d.results) using for Loop, and Stores data.d.results[i]["Title"]; in variable StrJob..
- DlJob+='<option value='+data.d.results[i].ID+'>'+StrJob+'</option>';}}
The above function stores the option values from data.d.results and Stored in Variable DlJob again Check (data.d.results[i].Title!= null) If True create Dropdown values Using Below Code,
- ddlSample='<select id="txtlev"> <option value=0>Select</option> '+DlJob+' </select>';
After that append the above created dropdown values(ddlSample) in ur html dropdown(select id="txtlev') using below Code
- $("#txtlev").html(ddlSample)
Step 3
For 2nd Dropdown(dependent DropDown) Create Seperate function(function jsFunction(value)) As Like 1st Dropdown do the same Steps again,but here we are using lookup field from another list so we want to expand that (i.e.)(URURL/_api/web/Lists/getByTitle('SubProducts')/items$select=Title,ID,Product/ID,Product/Title&$expand=Product/ID&$filter=Product/ID%20eq%20"+val+"";))
Step 4
- function CreateListItem() {
- var clientContext = new SP.ClientContext('URURL');
- var List = clientContext.get_web().get_lists().getByTitle('MasterList');
- var itemCreateInfo = new SP.ListItemCreationInformation();
- this.ListItem = List.addItem(itemCreateInfo);
- var title = document.getElementById("adtitle").value;
- var category = document.getElementById("txtlev").value;
- var subcategory = document.getElementById("category").value;
- ListItem.set_item('Title', title);
- ListItem.set_item('category', category);
- ListItem.set_item('subcategory', subcategory);
- ListItem.update();
- clientContext.load(ListItem);
The above Code is Used to Save the Data in the List ..This is Written using ECMA Script
Step 5
- <select id="txtlev" style="width: 99%; height: 90%" onmousedown="this.value='';" onchange="jsFunction
- (this.value);">
The avove is the Heart of the concept for Dependent Dropdown list,if we click 1st dropdown and select some value,that values trigger to function jsFunction(value) regarding that value 2nd dropdown intialize(its acts as a filter)
Step 6
Create Necessary Html Fields (Buttons, TextBox ) for User Input..
Step 7
Save the Entire file as .html file and Call it to ur Content editor Webpart...