Created
February 24, 2014 13:39
-
-
Save extstopcodepls/9188584 to your computer and use it in GitHub Desktop.
jqGrid brokren
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function LoadGrid(tableName, isSearch) { | |
| var colsModel = []; | |
| var colsNames = []; | |
| $.post('@Url.Action("GetColModel", "DataBase")?tableName=' + tableName, function (data) { | |
| var totalWidth = 0; | |
| $.each(data.data, function (index, object) { | |
| var widthToShow = object.Value.ColumnName.length * 9.35; | |
| var alignColumnText = 'left'; | |
| if (object.Value.IsNumeric) { | |
| alignColumnText = 'center'; | |
| } | |
| totalWidth += widthToShow; | |
| colsNames.push(object.Value.ColumnName); | |
| colsModel.push({ name: camelCase(object.Key), index: object.Value.ColumnName, sortable: false, /*height: 50,*/ resizable: false, align: alignColumnText }); | |
| }); | |
| var gr = '#' + tableName + 'ContentDiv'; | |
| var dataTableGridName = 'datatableGrid' + tableName; | |
| var pagerName = 'pager' + tableName; | |
| if (isSearch) { | |
| gr = '#displayedContent'; | |
| } | |
| $(gr).jqGrid('GridDestroy'); | |
| $(gr).empty().append('<div style="overflow: auto"><table id="' + dataTableGridName + '"></table><div id="' + pagerName + '"></div><div>'); | |
| $(gr).append('<div style="">') | |
| if (data.description != null && data.description != undefined && data.description != '') { | |
| $(gr).append('<p>Opis: ' + data.description + '</p>'); | |
| } | |
| if (data.source != null && data.source != undefined && data.source != '') { | |
| $(gr).append('<p>Źródło: ' + data.source + '</p>'); | |
| } | |
| $(gr).append('</div>'); | |
| var newUrl = '@Url.Action("GetDataTable", "DataBase")' + '?tableName=' + tableName; | |
| $(("#" + dataTableGridName)).jqGrid({ | |
| url: newUrl, | |
| datatype: "json", | |
| colNames: colsNames, | |
| colModel: colsModel, | |
| rowNum: 20, | |
| rowList: [20, 50, 100], | |
| pager: '#' + pagerName, | |
| viewrecords: true, | |
| sortorder: "desc", | |
| caption: data.tableNameTranslation, | |
| jsonReader: { repeatitems: false }, | |
| height: "auto", | |
| autowidth: true, | |
| shrinkToFit: true, | |
| gridview: true, | |
| resizeStop: autoresizeGrid, | |
| gridComplete: function () { | |
| // Testowy kod - dobrze dopasowuje szerokosc danych, ale nagłówki mają źle obliczoną szerokość | |
| //var $grid = $(this), | |
| // gridWidth = $grid.jqGrid('getGridParam', 'width'), | |
| // columns = $grid.jqGrid('getGridParam', 'colModel'), | |
| // columnMaxWidth = 0; | |
| //for (var i = 0; i < columns.length; i++) { | |
| // if (columnMaxWidth < columns[i].width) { | |
| // columnMaxWidth = columns[i].width; | |
| // $grid.setColProp(columns[i].name, { width: columnMaxWidth, widthOrg: columnMaxWidth }); | |
| // } | |
| //} | |
| //$grid.jqGrid('setGridWidth', gridWidth, false); | |
| //***START*** : dopasowanie szerokości kolumy do najszerszego wiersza | |
| //var $grid = $(this); | |
| //var cell, cellLength = 0; | |
| //var ids = $grid.jqGrid('getDataIDs'); | |
| //var columns = $grid.jqGrid('getGridParam', 'colModel'); | |
| ////iterujemy po wszystkich kolumnach | |
| //$.each(data.data, function (index, object) { | |
| // var columnName = camelCase(object.Key); | |
| // var columnHeaderWidth = $grid.jqGrid('getColProp', columnName).width; | |
| // var contentMaxWidth = columnHeaderWidth; | |
| // //pobieramy wartość maksymalną dla wszystkich wierszy danej kolumny | |
| // for (var row_id = 0; row_id < ids.length; row_id++) { | |
| // cell = $grid.jqGrid('getCell', ids[row_id], columnName); | |
| // cellLength = cell.length * 8.5; | |
| // if (cellLength > contentMaxWidth) { | |
| // contentMaxWidth = cellLength; | |
| // } | |
| // } | |
| // // wartosc potrzebna to rozciągnięcia tych tabel, które nie przechodzą przez warunek niżej | |
| // var gridWidth = $grid.jqGrid('getGridParam', 'width'); | |
| // //jeżeli wartość maksymalna wierszy jest większa niż wartość nagłówna kolumny to poszerzamy całą kolumnę | |
| // if (contentMaxWidth != columnHeaderWidth) { | |
| // //oldWidth to zmienna dodana po to, aby zapobiec pomnijeszaniu kolumny | |
| // $grid.setColProp(columnName, { width: contentMaxWidth, widthOrg: contentMaxWidth, oldWidth: columnHeaderWidth }); | |
| // var colsTotalWidth = 0; | |
| // for (var i = 0; columns[i]; i++) { | |
| // colsTotalWidth += columns[i].width; | |
| // } | |
| // colsTotalWidth += 50; | |
| // $grid.jqGrid('setGridWidth', colsTotalWidth, true); | |
| // } else { | |
| // var gridWidthBasedOnGridHeader = data.tableNameTranslation.length * 9; | |
| // if (gridWidth < gridWidthBasedOnGridHeader) { | |
| // $grid.jqGrid('setGridWidth', gridWidthBasedOnGridHeader, true); | |
| // } else { | |
| // $grid.jqGrid('setGridWidth', gridWidth, true); | |
| // } | |
| // } | |
| //}); | |
| } | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment