Problem
In one of the my project, I have to hide the columns in SharePoint OOTB Datasheet view and Gannt view, using JavaScript/jQuery.
Solution
After search many times, on the above problem, I have found that SharePoint OOTB Datasheet view and Gannt View are used JSGrid to quickly edit the list items in SharePoint.
To hide the columns in both the views, we need to first identify View ID for Gannt View or DataSheet View.
The guid is of the Gannt View. You can snoop it. e.g. from page source. Search for g_ViewIfToViewCounterMap.
There will be only one such place, in case you have one Gannt View or DataSheet View on the page. The GUID to the right is exactly, what you need.
After getting GUID for Gannt or Datasheet view, we need to write the code, given below, in document.ready or window.load function to hide the columns in Gannt view.
Code for DataSheet View
- $(document).ready(function() {
- var viewId = "{E41180E8-CCE3-424A-A525-9A0A6F19DF13}";
- var jsGridContainer = $get("spgridcontainer_" + g_SPGridInitInfo[viewId].jsInitObj.qualifier);
- var jsGrid = jsGridContainer.jsgrid;
- var columns = jsGrid.GetColumns();
- for (var i in columns) {
- columns[i].isSortable = false;
- columns[i].isAutoFilterable = false;
- }
- jsGrid.UpdateColumns(new SP.JsGrid.ColumnInfoCollection(columns));
-
- jsGrid.HideColumn("Column Name");
- });
Code for Gannt View
- $(document).ready(function()
- {
- $("div [id$='_ListViewWebPartJSGrid']")[0].jsgrid.HideColumn("Column Name");
- });