0
我有一個名爲「ViewItem」的函數調用2個更多的功能:「成功」和「失敗」。當調用「成功」時,它會檢查一個值是否存在,如果該值存在,則返回true;如果該值不存在,則返回false。最後,我有一個名爲「PresaveAction」的第四個函數,這個函數的作用是檢查一個值是「是」還是「否」,如果是「否」,它返回true並允許我保存並且我想實現的是如果值是「是」從之前稱爲「成功」功能,並取決於「成功」返回真或假允許我保存。那麼如何將PreSaveAction函數傳遞給「成功」?Sharepoint的javascript列表表單驗證上的預測
function ViewItem()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('demoTrainingRoom2');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title, EventDate, time2)');
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
for(var i = 0; i < this.allItems.get_count(); i++){
\t \t \t \t \t var item = this.allItems.get_item(i);
\t \t \t \t \t console.log(item.get_item('time2') + ' - ' + currentTitle);
\t \t \t \t \t
if (currentTitle == item.get_item('time2')){
alert('There is an event with the same Start Date on DemoTrainingRoom2' + ' ' + item.get_item('time2') + ' - ' + currentTitle);
return true; // or item
\t \t \t \t }
\t \t \t \t }
\t \t \t \t
return false;
\t \t \t \t }
\t \t \t \t
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}
function PreSaveAction() {
var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
alert(time + " Current Start Time");
if(SPUtility.GetSPField('demoField').GetValue() == "no")
{
alert('No need for validation');
return true; // save file
}
else
{
\t alert('Need to validate date');
//here is where i need to call the result from success
\t return false; // don't save file
\t }
\t
}
@Thriggle你的建議是這樣的
var result; //global variable
function ViewItem()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('demoTrainingRoom2');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title, EventDate, time2)');
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success() {
var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
for(var i = 0; i < this.allItems.get_count(); i++){
\t \t \t \t \t var item = this.allItems.get_item(i);
\t \t \t \t \t console.log(item.get_item('time2') + ' - ' + currentTitle);
\t \t \t \t \t
if (currentTitle == item.get_item('tiempo2')){
alert('There is an event with the same Start Date on DemoTrainingRoom2' + ' ' + item.get_item('time2') + ' - ' + currentTitle);
var result = "Yes";
return true; // or item
\t \t \t \t }
\t \t \t \t }
\t \t \t \t var result = "No";
\t \t \t \t
\t \t \t \t return false;
\t \t \t \t }
\t \t \t \t
function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}
function PreSaveAction() {
var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
alert(time + " Current Start Time");
if(SPUtility.GetSPField('demoField').GetValue() == "no")
{
alert('No need for validation');
return true;
}
else if(SPUtility.GetSPField('demoField').GetValue() == "yes" && result == "Yes")
{
alert(result);
//return false;
} \t
else if(SPUtility.GetSPField('demoField').GetValue() == "yes" && result == "No")
{
alert(result);
//return false;
} \t
}
您傳遞到回調函數'context.executeQueryAsync()'將異步運行,因此您的代碼不會暫停執行,並等待這些函數的返回值;我不認爲有什麼辦法可以在執行時從這些函數中獲取返回值。接下來最好的辦法是將這些值保存到全局變量中,然後可以在'PreSaveAction()'執行時檢查它們。 – Thriggle
@Thriggle thnx爲了你的利益,你會介意給我看看怎麼樣。我不知道如何在評論中格式化代碼,但不適當更新德問題與我所說的從你說什麼 – suizzak
是的,你更新的是類似於我的建議,但不是'var result = ...'嘗試'這個。結果= ...,在你的成功功能裏面。當您使用'var'關鍵字時,JavaScript會在當前範圍內創建一個新的局部變量。您還需要確保您的'ViewItem()'函數在用戶嘗試保存表單之前的某個時間運行,以確保您的'result'變量在該點之前具有一個值。祝你好運! – Thriggle