How to connect two field in Sharepoint? - sharepoint

I am trying to connect 2 field. example In particular list there are 2 column (Car and model) Value: Car -> Audi, BMW, Mercedes Model -> Audi A3, Audi A5, BMW X3, BMW X5, Mercedes c250, Mercedes c300
lets say user select Audi then in model list should only display Audi A3, AudiA5 in list box. your advice will be highly appreciated.

You could use jQuery for this requirement, insert script editor webpart into your list form and insert below sample script into script editor webpart.
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(function () {
var defaultCar = $('select[Title="Car"] option:selected').val();
$('select[Title="Model"] option').each(function () {
if ($(this).val().indexOf(defaultCar)<0) {
$(this).attr('disabled', 'disabled').hide();
}
})
$('select[Title="Car"]').change(function () {
var Car = this.value;
console.log(Car);
if (Car) {
$('select[Title="Model"] option').each(function () {
if ($(this).val().indexOf(Car) < 0) {
$(this).attr('disabled', 'disabled').hide();
} else {
$(this).removeAttr('disabled').show();
}
})
}
})
})
</script>

Related

SharePoint online show or hide columns multiple fields required with same functionality

I'm working on the SharePoint online form with show or hide columns with the below code. it is working well but not able to combine three codes together and on the edit item form by default all the fields are getting hide even though as per the code it should show the fields based on selected dropdown value. if we change the values in the edit form again the show values are working fine.
along with this code i want the make all the visible fields are mandate fields with, any assistance would be really appreciated.
<script src="/sites/XXXXXX/XXXXXXXX/SiteAssets/jquery-1.7.2.min%20-%20Show%20or%20Hide.js" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function () {
$('nobr:contains("Person1")').closest('tr').hide();
$('nobr:contains("Person2")').closest('tr').hide();
$('nobr:contains("Dropdown")').closest('tr').hide();
$('nobr:contains("Dropdown1")').closest('tr').hide();
$('nobr:contains("Text")').closest('tr').hide();
$('nobr:contains("Dropdown2")').closest('tr').hide();
$('nobr:contains("Date column")').closest('tr').hide();
$('nobr:contains("Number filed")').closest('tr').hide();
$('nobr:contains("Dropdown3")').closest('tr').hide();
$('nobr:contains("Remarks")').closest('tr').hide();
$('nobr:contains("Dropdown4")').closest('tr').hide();
$('nobr:contains("number2)")').closest('tr').hide();
$('nobr:contains("Dropdown5")').closest('tr').hide();
$('nobr:contains("Dropdown6")').closest('tr').hide();
$("select[title='Status Required Field']").change(function () {
console.log("selection changed", $("[title='Status Required Field'] option:selected").text());
alert($("[title='Status Required Field'] option:selected").text());
if ($("[title='Status Required Field'] option:selected").text() != "submitted") {
$('nobr:contains("Person1")').closest('tr').hide();
$('nobr:contains("Person2")').closest('tr').hide();
$('nobr:contains("Dropdown")').closest('tr').hide();
$('nobr:contains("Dropdown1")').closest('tr').hide();
$('nobr:contains("Text")').closest('tr').hide();
$('nobr:contains("Dropdown2")').closest('tr').hide();
$('nobr:contains("Date column")').closest('tr').hide();
$('nobr:contains("Number filed")').closest('tr').hide();
$('nobr:contains("Dropdown3")').closest('tr').hide();
$('nobr:contains("Remarks")').closest('tr').hide();
$('nobr:contains("Dropdown4")').closest('tr').hide();
$('nobr:contains("number2")').closest('tr').hide();
$('nobr:contains("Dropdown5")').closest('tr').hide();
$('nobr:contains("Dropdown6")').closest('tr').hide();
}
else {
$('nobr:contains("Person1")').closest('tr').show();
$('nobr:contains("Person2")').closest('tr').show();
$('nobr:contains("Dropdown")').closest('tr').show();
$('nobr:contains("Dropdown1")').closest('tr').show();
$('nobr:contains("Text")').closest('tr').show();
$('nobr:contains("Dropdown2")').closest('tr').show();
$('nobr:contains("Date column")').closest('tr').show();
$('nobr:contains("Number filed")').closest('tr').show();
$('nobr:contains("Dropdown3")').closest('tr').show();
$('nobr:contains("Remarks")').closest('tr').show();
$('nobr:contains("Dropdown4")').closest('tr').show();
}
});
});
</script>
<script src="/sites/XXXXXX/XXXXXXXX/SiteAssets/jquery-1.7.2.min%20-%20Show%20or%20Hide.js" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function () {
$('nobr:contains("number2")').closest('tr').hide();
$('nobr:contains("Dropdown5")').closest('tr').hide();
$('nobr:contains("Dropdown6")').closest('tr').hide();
$("select[title='Dropdown4']").change(function () {
console.log("selection changed", $("[title='Dropdown4'] option:selected").text());
alert($("[title='Dropdown4'] option:selected").text());
if ($("[title='Dropdown4'] option:selected").text() != "Active") {
$('nobr:contains("number2")').closest('tr').hide();
$('nobr:contains("Dropdown5")').closest('tr').hide();
$('nobr:contains("Dropdown6")').closest('tr').hide();
}
else {
$('nobr:contains("number2")').closest('tr').show();
$('nobr:contains("Dropdown5")').closest('tr').show();
}
});
});
</script>
<script src="/sites/XXXXXX/XXXXXXXX/SiteAssets/jquery-1.7.2.min%20-%20Show%20or%20Hide.js" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function () {
$('nobr:contains("Dropdown6")').closest('tr').hide();
$("select[title='Dropdown5").change(function () {
console.log("selection changed", $("[title='Dropdown5'] option:selected").text());
alert($("[title='Dropdown5'] option:selected").text());
if ($("[title='Dropdown5'] option:selected").text() != "Yes") {
$('nobr:contains("Dropdown6")').closest('tr').hide();
}
else {
$('nobr:contains("Dropdown6")').closest('tr').show();
}
});
});
</script>```
As far as fields hidden on Edit form your document ready function is hiding the fields. There are alot of ways to get the form state. You can simply check location contains New ,Display or Edit form and hide your fields accordingly.
var isNewMode = document.location.pathname.indexOf("/NewForm.aspx") > -1;
var isDisplayMode = document.location.pathname.indexOf("/DispForm.aspx") > -1;
var isEditMode = document.location.pathname.indexOf("/EditForm.aspx") > -1;
if(isNewMode)
{
alert("New");
}
if(isDisplayMode)
{
alert("Display");
}
if( isEditMode)
{
alert("Edit");
}
You can use PreSaveAction() function to do your custom validation. This function is executed once you click on the submit button. return true to submit the form or else return false to stay in the same page.
For Validation Add the code as follows:
function PreSaveAction(){
if($('nobr:contains("someid")').val() == ''){
alert('Required field'); // some custom validation
return false;
}else{
return true;
}
}

Datatables make all bordered cells when export to excel

Thanks in advance. How to make all bordered excel cells when export? I could not find documantation or manual about that.
To add a border around each Excel cell, you can use the following:
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'excelHtml5',
title: '', // no title row in excel sheet
text: 'Excel', // label for the export button
customize: function ( xlsx ) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$( 'row c', sheet ).attr( 's', '25' );
}
}
]
} );
} );
</script>
This takes advantage of one of the built-in styles available to DataTables when exporting data to Excel. Specifically, the attr( 's', '25' ) function uses style number 25 (normal text with thin black border).
Here is an example of the output:

show/hide tr on dropdown selection for a list in sharepoint

I have created a list in sharepoint now in newform I'm trying to hide and show 3 tr alternatively when i click on dropdown value. for eg: I have 3 option in dropdown A, B, C and my tr have ids(A, B, C) click on A only A is there when B only B is there and when C only C is there. same for edit form how to achieve this?
Sample tested script for a previous thread( I can't remember the link), you could update the script based on your fields' definition.
SPUtility.js
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="/siteassets/sputility.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function () {
var employeetype = SPUtility.GetSPField('Employee Type');
var showOrHideField = function () {
var employeeValue = employeetype.GetValue();
if (employeeValue == 'Existing Employee') {
SPUtility.GetSPField('Employee Name').Show();
SPUtility.GetSPField('Employee ID').Show();
SPUtility.GetSPField('Candidate Name').Hide();
}
else {
SPUtility.GetSPField('Employee Name').Hide();
SPUtility.GetSPField('Employee ID').Hide();
SPUtility.GetSPField('Candidate Name').Show();
}
}
// run at startup (for edit form)
showOrHideField();
// make sure if the user changes the value we handle it
$(employeetype.Dropdown).on('change', showOrHideField);
});

SharePoint Column fields show or hide depending on Drop down selection

For one of the lists, I have to hide column Project (single line of text) based on the choice column 'Customer' drop down selection. I have two values in customer drop down: Customer W Project and Customer WO Project. If the user selects Customer W Project, I want to hide Project column field on the new item click form. Below is the code I am using, please let me know if anything is wrong:
Also, I am working on SharePoint 2016 online/office 365.
Any help will be greatly appreciated. Thanks.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js></script><script src="/sites/lcpatest/Style%20Library/sputility.js"></script>
<script> $(document).ready(function(){
var customer = SPUtility.GetSPField('Customer');var HideOrShowOthersField=function(){var customerValue = customer.GetValue();
if(customerValue=='Customer W Project'){SPUtility.GetSPField('Project').Hide();
}else {SPUtility.GetSPField('Project').Show();}};HideOrShowOthersField();
$(customer.Dropdown).on('change',HideOrShowOthersField);});</script>
Your jQuery library is so old.
Below code works based on my testing.
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="/sites/Developer/SiteAssets/sputility.js"></script>
<script>
$(document).ready(function () {
var customer = SPUtility.GetSPField('Customer');
var HideOrShowOthersField = function () {
var customerValue = customer.GetValue();
if (customerValue == 'Customer W Project') {
SPUtility.GetSPField('Project').Hide();
}
else {
SPUtility.GetSPField('Project').Show();
}
};
HideOrShowOthersField();
$(customer.Dropdown).on('change', HideOrShowOthersField);
});
</script>

Lunch Count Daily Save and Wipe Script

I work for a school system looking to have their lunch numbers for the day saved each night and then the numbers wiped. We have tried to find someone to write us a script with triggers but no one has been able to figure it out thus far. Any help would be highly appreciated. This is our last hope. https://docs.google.com/a/bradleyschools.org/spreadsheets/d/1KZ8LuABtUE1I4jFKVzroXKaJVOcD9LArewRl5wRFRbA/edit?usp=sharing
Lunch Counter WebApp
I took a stab at the problem and here it is. I have it working as a webapp contained in a spreadsheet and the lunchcount gets posted and reset every night around 10:00 PM. Every time you do a get on the url it checks to see if the trigger is set and if it's not then it creates a new one. But it won't create more than one. I checked it last night and the trigger is working fine. Don't forget to put in the SpreadSheetID for the postLunchCount and also run setupNightlyTrigger() once to get the trigger installed. It run's by itself after that.
LunchCounter.html
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
google.script.run
.withSuccessHandler(displayCount)
.getCount();
});
function incrementCount()
{
google.script.run
.withSuccessHandler(displayCount)
.lunchCounter();
}
function displayCount(n)
{
$('#cnt').text(n);
}
console.log('My Code');
</script>
</head>
<body>
<div id="cnt"></div>
<input type="button" id="btn1" value="Increment" onClick="incrementCount();" style="width:200px;height:200px;"/>
</body>
</html>
Code.gs
function onOpen()
{
SpreadsheetApp.getUi().createMenu('My Tools')
.addItem('Lunch Counter','lunchCounter')
.addItem('Load SideBar', 'loadSideBar')
.addItem('Reset Lunch Count','resetLunchCount')
.addItem('Post Lunch','postLunchCount')
.addToUi();
}
function lunchCounter()
{
var prop=PropertiesService.getScriptProperties();
var count=Number(prop.getProperty('LunchCount'));
count++;
prop.setProperty('LunchCount', count);
return(count);
}
function getCount()
{
var prop=PropertiesService.getScriptProperties();
var count=Number(prop.getProperty('LunchCount'));
return(count);
}
function resetLunchCount()
{
postLunchCount();
PropertiesService.getScriptProperties().setProperty('LunchCount', '0');
}
function postLunchCount()
{
var ss=SpreadsheetApp.openById('SpreadSheetID');
var sht=ss.getSheetByName('DailyLunchCount');
var dt=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd");
var cnt=getCount();
var row=[dt,cnt];
sht.appendRow(row);
}
function setupNightlyTrigger()
{
if(!isTrigger('resetLunchCount'))
{
ScriptApp.newTrigger('resetLunchCount').timeBased().atHour(22).everyDays(1).create();
}
}
function loadSideBar()
{
var html = HtmlService.createHtmlOutputFromFile('LunchCounter');
SpreadsheetApp.getUi().showSidebar(html);
}
function doGet()
{
setupNightlyTrigger();
var html = HtmlService.createHtmlOutputFromFile('LunchCounter');
html.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}
function isTrigger(handlerName)
{
var r=false;
var triggers = ScriptApp.getProjectTriggers();
for (var i=0;i<triggers.length;i++)
{
if (handlerName==triggers[i].getHandlerFunction())
{
r=true;
break;
}
}
return r;
}
This is what the webapp looks like on an Iphone:
It's really not that big. I did it as a screen shot. On my iphone the button size is about 3/4" and the lunch count is diplayed above it. I'm don't really know what you wanted but I figured a single button is simple User Interface.
Spreadsheet Looks Like this:

Resources