Default headerCell Template in ng-grid

纵然是瞬间 提交于 2019-12-20 05:40:11

问题


Consider the following snippet of angularJs code:

var myHeaderCellTemplate = '<div class="ngHeaderSortColumn {{col.headerClass}}" ng-style="{\'cursor\': col.cursor}" ng-class="{ \'ngSorted\': !noSortVisible }">' +
                           '<div ng-click="col.sort($event)" ng-class="\'colt\' + col.index" class="ngHeaderText">{{col.displayName}} <img src="PLUS-ICON.png" /></div>' +
                           '<div class="ngSortButtonDown" ng-show="col.showSortButtonDown()"></div>' +
                           '<div class="ngSortButtonUp" ng-show="col.showSortButtonUp()"></div>' +
                           '<div class="ngSortPriority">{{col.sortPriority}}</div>' +
                           '<div ng-class="{ ngPinnedIcon: col.pinned, ngUnPinnedIcon: !col.pinned }" ng-click="togglePin(col)" ng-show="col.pinnable"></div>' +
                           '</div>' +
                           '<div ng-show="col.resizable" class="ngHeaderGrip" ng-click="col.gripClick($event)" ng-mousedown="col.gripOnMouseDown($event)"></div>';
$scope.gridOptions = {
    data: self.myData,
    columnDefs: [
        { field: 'firstField', displayName: 'First Column', headerCellTemplate: myHeaderCellTemplate },
        { field: 'secondField', displayName: 'Second Column', headerCellTemplate: myHeaderCellTemplate },
        { field: 'lastField', displayName: 'Last Column', headerCellTemplate: myHeaderCellTemplate }
    ]
};

I am trying to display an excel in browser using ng-grid. I need a headerCellTemplate common for all my fields. Since the fields are coming from an excel, they can be random, dynamic and any number.

How to set a default headerCellTemplate to all the fields, so that I do not have to mention fields in the columnDefs section of gridOptions? (Since fields are uncontrolled)

P.S. Refer to this question asked by me as well. This is the reason I can't use template on a row, because I want fields to be individually draggable.


回答1:


Did something like this in Java(backend):

public static String generateHeaderCellTemplate(List<String> excelFields) {

        String headerCellTemplate = "{ \"data\": \"myData\", \"columnDefs\": [";
        for (int i = 0; i < excelFields.size(); i++) {
            String field = excelFields.get(i);
            if (i != 0) {
                headerCellTemplate += ",";
            }
            headerCellTemplate += ("{ \"field\": \"" + field);
            headerCellTemplate += ("\", \"width\":80," + " \"headerCellTemplate\":" + "\"myHeaderCellTemplate\"}");
        }
        headerCellTemplate += "]}";
        return headerCellTemplate;
    }

Controller:

@RequestMapping(value= "/myHeaderCellTemplate", method= RequestMethod.GET)
    public @ResponseBody String headerTemplate() {
        return "<div class=\"ngHeaderSortColumn {{col.headerClass}}\" ng-style=\"{cursor: col.cursor}\" ng-class=\"{ ngSorted: !noSortVisible }\">"+
                "<div class=\"ngHeaderText item\" id=\"{{col.displayName}}\" draggable value=\"{{col.displayName}}\">{{col.displayName}}</div>"+
                "<div class=\"ngSortButtonDown\" ng-show=\"col.showSortButtonDown()\"></div>"+

        "<div class=\"ngSortButtonUp\" ng-show=\"col.showSortButtonUp()\"></div>"+
        "<div class=\"ngSortPriority\">{{col.sortPriority}}</div>"+
        "</div>"+
 "<div ng-show=\"col.resizable\" class=\"ngHeaderGrip\" ng-click=\"col.gripClick($event)\" ng-mousedown=\"col.gripOnMouseDown($event)\"></div>";
    }

App.js:

    $scope.myData = excelAsJson.excelData;
    $scope.colDefs = columnDefsJson.columnDefs;
    $scope.gridOptions = { data: 'myData', columnDefs: 'colDefs'};

where $scope.colDefs contains the output of generateHeaderCellTemplate() List<String> excelFields has all the column names groupped together



来源:https://stackoverflow.com/questions/30187845/default-headercell-template-in-ng-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!