0

I have a small requirement where in i just need to populate Date Column Regarding that Project. The code below shows that i am populating the project name based on employee name.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" 
type="text/javascript"></script>
 <script type="text/javascript">
   $(function(){
var employeeName=$("input[title^='Employee Name']").val();
$("input[title^='Employee Name']").hide();
$("input[title^='Employee Name']").after("<select id='EmployeeField' class='ms-RadioText'><option value=''></option></select>");
$("input[title^='Project Name']").hide();
$("input[title^='Project Name']").after("<select id='ProjectField' class='ms-RadioText'><option value=''></option></select>");
var allEmployeeNames=getAllEmployeeNames();
    $.each(allEmployeeNames,function(i,employee){
    $.each(employee.Should_x0020_Cost_x0020_Modeler.results,function(j,item){
        if(employeeName==item.Title){
            $("#EmployeeField").append("<option selected='selected' value='"+item.Title+"'>"+item.Title+"</option>");
        }else{
            if(item.Title!=undefined){
                $("#EmployeeField").append("<option value='"+item.Title+"'>"+item.Title+"</option>");
            }           
        }   
    });         
});
$("#EmployeeField option").each(function() {
    $(this).siblings('[value="'+ $(this).val() +'"]').remove();
});
$("#EmployeeField").change(function(){
    $("input[title^='Employee Name']").val($(this).val());
    var projects=getAllProjects($(this).val());
    $("#ProjectField").html("<option value=''></option>");
    $.each(projects,function(i,project){
        $("#ProjectField").append("<option value='"+project.Project_x0020_Name+"'>"+project.Project_x0020_Name+"</option>");
    });
    $("#ProjectField option").each(function() {
        $(this).siblings('[value="'+ $(this).val() +'"]').remove();
    });
}); 
$("#ProjectField").change(function(){
    $("input[title^='Project Name']").val($(this).val());       
});
 })
 function getAllEmployeeNames(){
   var results;
    $.ajax({
     url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id",
    type: "GET",
    async:false,
    headers: {
        "Accept": "application/json;odata=verbose",
    },
    success: function (data) {
        if(data.d.results.length>0){
            results=data.d.results;
        }
    },
    error: function (data) {
        //alert("Error");
    }
});
   return results;
  }
   function getAllProjects(employeeName){
      var results;
      $.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Project_x0020_Name,Should_x0020_Cost_x0020_Modeler/Title&$filter=Should_x0020_Cost_x0020_Modeler/Title eq '"+employeeName+"'&$expand=Should_x0020_Cost_x0020_Modeler/Id",
    type: "GET",
    async:false,
    headers: {
        "Accept": "application/json;odata=verbose",
    },
    success: function (data) {
        if(data.d.results.length>0){
            results=data.d.results;
        }
    },
    error: function (data) {
        //alert("Error");
    }
});
return results;
}
</script>
1

If the end date column internal name is "End_x0020_Date", the following code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var employeeName=$("input[title^='Employee Name']").val();
    $("input[title^='Employee Name']").hide();
    $("input[title^='Employee Name']").after("<select id='EmployeeField' class='ms-RadioText'><option value=''></option></select>");
    $("input[title^='Project Name']").hide();
    $("input[title^='Project Name']").after("<select id='ProjectField' class='ms-RadioText'><option value=''></option></select>");
    var allEmployeeNames=getAllEmployeeNames();
        $.each(allEmployeeNames,function(i,employee){
        $.each(employee.Should_x0020_Cost_x0020_Modeler.results,function(j,item){
            if(employeeName==item.Title){
                $("#EmployeeField").append("<option selected='selected' value='"+item.Title+"'>"+item.Title+"</option>");
            }else{
                if(item.Title!=undefined){
                    $("#EmployeeField").append("<option value='"+item.Title+"'>"+item.Title+"</option>");
                }           
            }   
        });         
    });
    $("#EmployeeField option").each(function() {
        $(this).siblings('[value="'+ $(this).val() +'"]').remove();
    });
    $("#EmployeeField").change(function(){
        $("input[title^='Employee Name']").val($(this).val());
        var projects=getAllProjects($(this).val());
        $("#ProjectField").html("<option value=''></option>");
        $.each(projects,function(i,project){
            $("#ProjectField").append("<option value='"+project.Project_x0020_Name+"' pdate='"+project.End_x0020_Date+"'>"+project.Project_x0020_Name+"</option>");
        });
        $("#ProjectField option").each(function() {
            $(this).siblings('[value="'+ $(this).val() +'"]').remove();
        });
    }); 
    $("#ProjectField").change(function(){
        $("input[title^='Project Name']").val($(this).val());
        var pDate=$(this).find("option:selected").attr("pdate");    
        $("input[title^='End Date']").val(moment(pDate).format("DD/MM/YYYY"));
    });
})
function getAllEmployeeNames(){
    var results;
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Should_x0020_Cost_x0020_Modeler/Title&$expand=Should_x0020_Cost_x0020_Modeler/Id",
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            if(data.d.results.length>0){
                results=data.d.results;
            }
        },
        error: function (data) {
        //alert("Error");
        }
    });
    return results;
}
function getAllProjects(employeeName){
    var results;
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists(guid'4BFF0A20-43A2-4444-881C-18932D468E54')/items?$select=Project_x0020_Name,End_x0020_Date,Should_x0020_Cost_x0020_Modeler/Title&$filter=Status eq 'Completed' and Should_x0020_Cost_x0020_Modeler/Title eq '"+employeeName+"'&$expand=Should_x0020_Cost_x0020_Modeler/Id",
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            if(data.d.results.length>0){
                results=data.d.results;
            }
        },
        error: function (data) {
        //alert("Error");
        }
    });
    return results;
}
</script>

enter image description here

If the End Date field is a text field, we need modify the code as below.

$("#ProjectField").change(function(){
    $("input[title^='Project Name']").val($(this).val());
    var pDate=$(this).find("option:selected").attr("pdate");    
    $("input[title^='End Date']").val(pDate);
});
  • so if i want more columns like this, it is achievable through some changes. Lets just say i need a new field to be populated i can do that just as end date right? – jayant khushalani Oct 11 '18 at 10:03
  • hi, the projects name are not populated in this code – jayant khushalani Oct 11 '18 at 10:18
  • I am getting invalid date in my End Date Text Field. – jayant khushalani Oct 11 '18 at 11:28
  • Make sure the End Date internal is "End_x0020_Date", if the End Date is a text field, we don't need use moment library to convert it. – LZ_MSFT Oct 12 '18 at 1:18
  • its working. Just one question how do i filter projects based on a field say Status eq Completed – jayant khushalani Oct 12 '18 at 4:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.