-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPCSOMGrid.js
525 lines (466 loc) · 22.5 KB
/
SPCSOMGrid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
https://gist.github.com/shyamjyothi
Author: Shyam Jyothi
Date: 27-Nov-2016
Version 1.0
Parameter Description (* ~ Mandatory Parameters)
listName*: Name of the List which needs to be queried
camlQuery*: string - CamlQuery for querying the List. Just mention the <Where> conditions
heading: string - Heading for the Grid
columns*: array - Columns information
HeaderText* : Column Header
InternalName* : List Field Internal Value
IsAnchor* : true/false, if the column is hyperlinked
ColumnType*: Text,Multiuser,Date
sorting: string - true/false
verticalScroll: string - true/false
horizontalScroll: string - true/false
dateFormat: Date Format
href: url (ID values is defaulted as QueryString)
sortColumn : string Internal Name of the column for sorting - Doesnt support Date and User
SPPaginate : true/false SharePoint Paged Querying for faster Loading
showSearch : true/false shows search box, this useful when SPPaginate is false
loadMessage : Custom Loading message
rowLimit*: no of rows to be displayed
*/
(function ($) {
$.fn.SPCSOMGrid = function (userOptions) {
var Maindiv = $(this);
//Declaring Private Variables
$.fn.SPCSOMGrid.defaults =
{
listName: "",
camlQuery: "",
heading: "",
columns: "",
MainDiv: Maindiv,
sorting: false,
verticalScroll: false,
horizontalScroll: false,
clientContext: null,
includeColumns: '',
dataSrc: '',
rowLimit: 10,
sortColumn: '',
SPItem: null,
SPList: null,
gridDataArray: [], AllDataArray: [],
SPCamlQuery: "",
waitDialog: null,
tableHtml: '',
queryStringColumn : "ID",
href: '',
dateFormat: "dd-MMM-yyyy",
loadMessage: "Loading search results",
pageNo: 1,
position: "",
currPostion: "",
nextPos: "",
prevPos: "",
firstLoad: true,
ListItemCount: 1,
ListItemCounter: 0,
showSearch: false,
SPPaginate: true,
overrideThrottling: false,
displayRowCount: 10,
qsParam: "ID",
hostedApp: false,
hostweburl: "",
appweburl: "",
scriptbase: ""
};
var obj = $.extend({}, $.fn.SPCSOMGrid.defaults, userOptions);
return this.each(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function Render() {
RenderTable();
});
});
function RenderTable(){
obj.waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(obj.loadMessage, '', 90, 450);
if(Validate()) {
try {
RenderFirstTime();
}
catch(err) {
obj.waitDialog.close();
obj.MainDiv.empty();
var msgHtml = MessageHtml(err.message);
obj.MainDiv.append(msgHtml);
}
}
}
function RenderFirstTime() {
if(obj.ListItemCount > 0) {
RenderHeading(true);
QuerySP(false);
}
else {
obj.waitDialog.close();
obj.MainDiv.empty();
var msgHtml = MessageHtml(NO_DATA());
obj.MainDiv.append(msgHtml)
}
}
// Validates the Options
function Validate() {
var success = true;
if(obj.camlQuery == "") { success = false; ShowError(NO_Caml()) }
if(obj.MainDiv == null) { success = false; ShowError(NO_MAIN_DIV()) }
if(obj.listName == "") { success = false;ShowError(NO_LIST()); }
if(obj.columns.length == 0) { success = false; ShowError(NO_COLS()); }
if(obj.rowLimit == 0) { success = false; ShowError(NO_ROW_LIMIT()) }
if(obj.overrideThrottling == true && obj.SPPaginate == false) {sucess = false; ShowError(THROTTLE_EXP()); }
if(obj.SPPaginate == true) { obj.displayRowCount = obj.rowLimit;}
return success;
}
//Generates the Table Heading nased on the columns entered
function RenderHeading(isFirst){
obj.tableHtml = "";
if (obj.columns.length > 0) {
var headerHtml = "";
headerHtml = '<table id="tblData" class="stripe cell-border" cellspacing="0" width="100%">';
var includeString = '';
headerHtml = headerHtml + '<thead>';
headerHtml = headerHtml + '<tr>';
//bind headers
for (var i = 0; i < obj.columns.length; i++) {
var headerText = obj.columns[i].HeaderText;
var internalName = obj.columns[i].InternalName;
var columnType = obj.columns[i].ColumnType;
if (internalName.toLowerCase() != "id" && internalName.toLowerCase() != "contenttype" && columnType != "Image") {
includeString = includeString == '' ? internalName : includeString + ',' + internalName;
}
headerHtml = headerHtml + '<th>' + headerText + '</th>';
}
includeString = includeString + ',ID,ContentType';
obj.includeColumns = "Include(" + includeString + ")";
headerHtml = headerHtml + "</tr></thead>";
obj.tableHtml = obj.tableHtml + headerHtml;
}
}
//Queries the SharePoint List to fetch results
//Sample CamlQuery
////<View><Query><Where><Geq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Geq></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy></Query><RowLimit>10</RowLimit></View>";
function QuerySP(isPaged) {
var orderby = '';
var query = '';
if(obj.overrideThrottling == true) {
orderby = "<OrderBy UseIndexForOrderBy='TRUE' Override='TRUE'>";
}
if(obj.sortColumn != '') {
orderby = "<OrderBy><FieldRef Name='" + obj.sortColumn + "' Ascending='False'/></OrderBy>" ;
}
else orderby = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>" ;
var camlStr = "<View><Query>" ;
camlStr = camlStr + obj.camlQuery + orderby + "</Query>";
camlStr = camlStr + "<RowLimit>" + obj.rowLimit + "</RowLimit>";
camlStr = camlStr + "</View>" ;
//Sets the Client Context based on AppModel
GetClientContext()
if(obj.clientContext !== null){
obj.SPList = obj.clientContext.get_web().get_lists().getByTitle(obj.listName);
obj.SPCamlQuery = new SP.CamlQuery();
obj.SPCamlQuery.set_viewXml(camlStr);
if(isPaged) {
position = new SP.ListItemCollectionPosition();
position.set_pagingInfo(obj.position);
obj.SPCamlQuery.set_listItemCollectionPosition(position);
}
obj.spItems = obj.SPList.getItems(obj.SPCamlQuery);
obj.clientContext.load(obj.spItems, obj.includeColumns);
obj.clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySuccess), Function.createDelegate(this, onQueryFailure));
}
}
//Delegate for Success
function onQuerySuccess(sender, args) {
var listEnumerator = obj.spItems.getEnumerator();
var item; obj.gridDataArray = [];
while (listEnumerator.moveNext()) {
item = listEnumerator.get_current();
obj.gridDataArray.push(item);
}
obj.ListItemCounter = obj.ListItemCounter + obj.gridDataArray.length;
if(obj.gridDataArray.length == 0) {
if(obj.firstLoad == false) { //Was at Last Page
obj.pageNo = obj.pageNo - 1;
obj.waitDialog.close();
}
else {
ShowError(NO_DATA())
}
}
else {
BindData();
obj.waitDialog.close();
}
}
//Delegate for failure
function onQueryFailure(sender, args) {
obj.waitDialog.close();
obj.MainDiv.empty();
var msgHtml = MessageHtml(args.get_message());
obj.MainDiv.append(msgHtml);
}
//Loops the data queried and writed the Table Elements
function BindData() {
var gridData = obj.gridDataArray;
if(gridData.length > 0) {
var rowHtml = "<tbody>";
for (var i = 0; i < gridData.length ; i ++) {
obj.MainDiv.attr("SPItem",gridData[i]);
rowHtml = rowHtml + "<tr>";
for (var j = 0; j < obj.columns.length; j++) {
var Id = String(gridData[i].get_item(obj.queryStringColumn));
var internalName = obj.columns[j].InternalName;
var isAnchor= obj.columns[j].IsAnchor;
var columnType = obj.columns[j].ColumnType;
var aHref = obj.href;
if (columnType != "Image") {
var columnValue = "";
var item = null;
item = internalName.toLowerCase() == "contenttype" ? gridData[i].get_contentType() : gridData[i].get_item(internalName);
switch (columnType) {
case 'Lookup':
columnValue = item != null ? String(item.get_lookupValue()) : "";
break;
case 'User':
columnValue = item != null ? String(item.get_lookupValue()) : "";
break;
case 'MultiUser':
columnValue = item != null ? GetLookupInfo(item) : "";
break;
case 'Date':
columnValue = item != null ? String(item.format("dd-MMM-yyyy")) : "";
break;
case 'Computed':
columnValue = item != null ? item.get_name() : "";
break;
case 'File' :
columnValue = item != null ? String(item) : "";
break;
default:
columnValue = item != null ? String(item) : "";
}
if(isAnchor){
var qsParam;
if(obj.qsParam === '') qsParam = "ID";
else qsParam = obj.qsParam;
aHref = aHref + "?" + qsParam + "=" + Id
rowHtml = rowHtml + "<td>"+ "<a href='"+ aHref + "'>" + columnValue + "</a></td>";
}
else if(columnType.toLowerCase() == 'file') {
aHref = aHref + columnValue;
rowHtml = rowHtml + "<td>"+ "<a href='"+ aHref + "' target='_blank'>" + RemoveFileExtn(columnValue) + "</a></td>";
}
else {rowHtml = rowHtml + "<td>" + columnValue + "</td>";}
}
}
rowHtml = rowHtml + "</tr>";
}
obj.tableHtml = obj.tableHtml + rowHtml + "</tbody></table>";
obj.MainDiv.html(obj.tableHtml);
//Binding of jQueryDatatable
$("#tblData").DataTable( {
"ordering": obj.sorting,
"info": false,
"scrollX": obj.horizontalScroll,
"scrolly" : obj.verticalScroll,
"pageLength": obj.displayRowCount,
"dom": '<"toolbar">frtip'
});
if(obj.heading != '') $("div.toolbar").html(obj.heading);
if(!obj.showSearch) $("#tblData_filter").hide();
//Custom Pagination Logic
if(obj.SPPaginate) SetPagination();
}
else {
if(SPPaginate) {
if(obj.pageNo ==1 && obj.firstLoad) {
obj.MainDiv.empty();
var msgHtml = "";
var msgHtml = MessageHtml(NO_DATA());
obj.MainDiv.append(msgHtml);
}
}
else {
obj.MainDiv.empty();
var msgHtml = "";
var msgHtml = MessageHtml(NO_DATA());
obj.MainDiv.append(msgHtml);
}
}
}
function SetPagination() {
//Clearing the Pagingation of jQuery Tables
$("#tblData_info").hide();
$("#tblData_paginate").hide();
//add hidden cntrls
var collection = obj.gridDataArray;
var start = (obj.pageNo * obj.rowLimit) - (obj.rowLimit - 1);
var end; var disableNext = false; var pclassname; var nclassname
if(collection.length < obj.rowLimit) { //this is the Last Page
if(obj.firstLoad == true) { // items are less than rowlimit
end = obj.rowLimit ;
disableNext = true;
}
else {
end = ((obj.pageNo - 1) * obj.rowLimit) + collection.length ;
disableNext = true;
}
}
if(collection.length == obj.rowLimit) { //may or maynot have LastPage
end = (obj.pageNo) * obj.rowLimit;
}
var pageMsg = start + " - " + end;
if(obj.pageNo == 1) pclassname = "paginate_button previous disabled";
else pclassname = "paginate_button previous"
if(disableNext) nclassname = "paginate_button next disabled";
else nclassname = "paginate_button next "
var sortColumn = obj.sortColumn == '' ? "ID" : obj.sortColumn
var ncolumnValue = String(collection[collection.length - 1].get_item(sortColumn));
var pcolumnValue = String(collection[0].get_item(sortColumn));
var npageFirstRow = end + 1;
var ppageFirstRow = start;
obj.nextPos = "Paged=TRUE&p_ID=" + String(collection[collection.length - 1].get_item('ID')) + "&p_" + sortColumn + "=" + ncolumnValue;
obj.prevPos = "Paged=TRUE&PagedPrev=TRUE&p_ID=" + String(collection[0].get_item('ID')) + "&p_" + sortColumn + "=" + pcolumnValue;
//footerdiv
var footerDiv = $('<div class="dataTables_paginate paging_simple_numbers" id="tblData_SPCSOM"><a tabindex="0" class=" ' + pclassname +'" id="tblData_previous_spcsom" aria-controls="tblData" data-dt-idx="0" href="#">Previous</a><span><a tabindex="0" class="paginate_button current" aria-controls="tblData" data-dt-idx="1">' + pageMsg + '</a></span><a tabindex="0" class="' + nclassname + '" id="tblData_next_spcsom" aria-controls="tblData" data-dt-idx="7" href="#">Next</a></div>');
$("#tblData_wrapper").append(footerDiv);
$("#tblData_previous_spcsom").click(function () {
if(obj.pageNo > 1) {
PreviousPage();
}
});
$("#tblData_next_spcsom").click(function () {
if(!disableNext) {
NextPage();
}
});
}
function NextPage() {
var pageNo = obj.pageNo
obj.position = obj.nextPos;
obj.pageNo = parseInt(pageNo) + 1;
obj.firstLoad = false;
try {
obj.waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(obj.loadMessage, '', 90, 450);
RenderHeading(false);
QuerySP(true);
}
catch(err) {
obj.waitDialog.close();
obj.MainDiv.empty();
var msgHtml = MessageHtml(err.message);
obj.MainDiv.append(msgHtml);
}
}
function PreviousPage() {
var pageNo = obj.pageNo;
obj.position = obj.prevPos;
obj.pageNo = parseInt(pageNo) - 1;
obj.firstLoad = false;
try {
obj.waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(obj.loadMessage, '', 90, 450);
RenderHeading(false);
QuerySP(true);
}
catch(err) {
obj.waitDialog.close();
obj.MainDiv.empty();
var msgHtml = MessageHtml(err.message);
obj.MainDiv.append(msgHtml);
}
}
function GetClientContext(){
if(!obj.hostedApp) {
obj.clientContext = SP.ClientContext.get_current();
}
else {
if(obj.clientContext !== null) {
hostweburl = GetQueryStringParameter("SPHostUrl");
appweburl = GetQueryStringParameter("SPAppWebUrl");
hostweburl = decodeURIComponent(hostweburl);
appweburl = decodeURIComponent(appweburl);
scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function () {
$.getScript(scriptbase + "SP.js", function () {
$.getScript(scriptbase + "SP.RequestExecutor.js", GetConextFromApp); });
});
}
}
}
function GetConextFromApp() {
obj.clientContext = new SP.ClientContext(appweburl);
var factory =
new SP.ProxyWebRequestExecutorFactory(
appweburl
);
obj.clientContext.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite( obj.clientContext, hostweburl);
var web = appContextSite.get_web();
context.load(web);
}
function GetLookupInfo(userInfo) {
var Users = "";
if (typeof userInfo === "undefined") {
Users = "";
}
else {
if (typeof userInfo.length != "undefined") {
for (var i = 0; i < userInfo.length; i++) {
Users = Users != "" ? Users + " ; " + String(userInfo[i].get_lookupValue()) : String(userInfo[i].get_lookupValue());
}
}
else {
Users = String(userInfo.get_lookupValue());
}
}
}
function RemoveFileExtn(item) {
var fileName = String(item)
fileName = fileName.substr(0, fileName.lastIndexOf('.')) || fileName;
return fileName;
}
function MessageHtml(msg) {
var messageHtml = "<div style='";
messageHtml = messageHtml + "border: 1px solid;";
messageHtml = messageHtml + "margin: 10px 0px;";
messageHtml = messageHtml + "padding:15px 10px 15px 50px;";
messageHtml = messageHtml + "background-repeat: no-repeat;";
messageHtml = messageHtml + "background-position: 10px center;";
messageHtml = messageHtml + "color: #9F6000;";
messageHtml = messageHtml + "background-color: #FEEFB3;";
messageHtml = messageHtml + "'>";
messageHtml = messageHtml + msg;
messageHtml = messageHtml + "</div>";
return messageHtml;
}
function ShowError(msg) {
obj.waitDialog.close();
obj.MainDiv.empty();
var msgHtml = MessageHtml(msg);
obj.MainDiv.append(msgHtml)
}
function GetQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
//Constant values
function NO_DATA() { return "No Data Returned."}
function NO_Caml() { return "CamlQuery parameter is empty."}
function NO_LIST() { return "ListName is empty."}
function NO_MAIN_DIV() { return "MainDiv is not specified."}
function NO_COLS() { return "Columns not specified."}
function NO_ROW_LIMIT() { return "RowLimit not specified."}
function THROTTLE_EXP() { return "If the List is throttled please set SPPaginate to TRUE."}
function ROWLIMIT_EXP() { return "If the List is throttled please set CAMLQuery Rowlimit and Display Row Count to be same."}
//Ends
};
})(jQuery)