Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
app_filtre.js
(function(module,$j) {
"use strict";
window.pd = window.pd || {};
pd.MeetingsViewModel = function() {
/* observable state */
this.FilterTextIm = ko.observable('');
this.FilterText = ko.computed(this.FilterTextIm)
.extend({ throttle: 400 });
this.LibraryTitle = ko.observable('');
this.LibrarySubTitle = ko.observable('');
this.FilterYears = new ko.observableArray([]);
this.FilterMonths = new ko.observableArray(['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre']);
this.SelectedYear = ko.observable('');
this.SelectedMonth = ko.observable('');
this.Working = ko.observable(true);
/* populate years filter as Current Year..-=4 */
for(var cy = parseInt(new Date().getFullYear()), y = cy; y > (cy-3); y--) { this.FilterYears.push(y.toString()); }
/* model actions */
this.filterByYear = function(year) {
this.FilterTextIm(''); // zap the filter text
year = (year === this.SelectedYear()) ? '' : year;
this.SelectedYear(year);
}.bind(this);
this.filterByMonth = function(month) {
this.FilterTextIm(''); // zap the filter text
month = (month === this.SelectedMonth()) ? '' : month;
this.SelectedMonth(month);
}.bind(this);
/* model behaviour */
this.isSelectedYear = function (year) {
var y = this.SelectedYear();
return (y == year);
}.bind(this);
this.isSelectedMonth = function (month) {
var m = this.SelectedMonth();
return (m == month);
}.bind(this);
}
pd.FilterLibraryApp = function() {
var
viewModel = new pd.MeetingsViewModel(),
debug = true,
wpCtx = null,
wpEvent = null,
filterControlId = null,
listViewTableId = null,
_module = {
start: start
};
return _module;
function locateListView() {
var fn = function() {
InitAllClvps();
// find the CTX for the LVWP
for(var k in g_ctxDict) {
if (debug && window.console) console.log(g_ctxDict[k]);
if (g_ctxDict[k].ListTitle === module.LibraryTitle) {
wpCtx = window['ctx'+g_ctxDict[k].ctxId];
break;
}
}
if (debug && window.console) {
console.log(wpCtx.clvp);
console.log(wpCtx.clvp.ctx);
console.log(wpCtx.clvp.ctx.view);
}
/* this isn't used in the 2010 version, not figured out if/how to use it yet
var clvp = (wpCtx && wpCtx.clvp) ? wpCtx.clvp : null;
wpEvent = { clvp: clvp, event: { currentCtx: { clvp: clvp } } };
*/
// find the LVWP and table containing the rows
var
$wpnode = $j('#'+wpCtx.clvp.wpq).first(),
$wpTableNode = $wpnode.find("table[id^='onetidDoclibViewTbl']");
listViewTableId = $wpTableNode.attr('id');
//Il faut avoir préalablement coché la case "Afficher le bouton de raffraichisement" sur la LVWP
// find the postback control id; e.g. ctl00$m$g_109629d4_d78b_4c9e_8ec0_90078b6e444e$ctl02
var
$wpinput = $wpnode.find('> :first-child > input:first-child'),
ctrlid = $wpinput.attr('id');
ctrlid = ctrlid && ctrlid.length
? ctrlid.replace(/^(ctl\d\d)_/gi,'$1$')
.replace(/_g_/g,'$g_')
.replace(/_(ctl\d\d)$/gi,'$$$1')
: '';
if (debug && window.console) { console.log(">>locateListView: controlid="+ctrlid); }
filterControlId = ctrlid;
viewModel.LibraryTitle(wpCtx.ListTitle);
viewModel.LibrarySubTitle('All');
viewModel.Working(false);
};
EnsureScript("inplview", typeof InitAllClvps, fn);
}
function onFilterChange(filterType) {
if (!filterControlId) {
alert('Unable to filter: the list view webpart could not be located or the list view contains a Person/Group column!');
return;
}
var
filterColumn = null,
filterValue = '';
if (filterType.match(/year/gi)) {
filterValue = this.SelectedYear();
filterColumn = 'PublishedDateYear'; //À ajuste selon votre filtre
} else if (filterType.match(/month/gi)) {
filterValue = this.SelectedMonth();
filterColumn = 'PublishedDateMonth'; //À ajuste selon votre filtre
}
if (!filterValue) filterValue = '##dvt_all##';
var filterCall = "__doPostBack('"+filterControlId+"','NotUTF8;__filter={"+filterColumn+"="+filterValue+"}')"
if (debug && window.console) {
console.log(">>Filter by " + filterType + " on ["+filterColumn+"] = ["+filterValue+"]");
console.log(" >> " + filterCall);
}
eval(filterCall);
}
function start() {
ko.applyBindings(viewModel, $j('#cdtm').get(0));
viewModel.SelectedYear.subscribe(onFilterChange.bind(viewModel, 'year'));
viewModel.SelectedMonth.subscribe(onFilterChange.bind(viewModel, 'month'));
locateListView();
}
}();
$j(pd.FilterLibraryApp.start);
})({ Name: 'Module', LibraryTitle: 'Billets' },jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.