본문 바로가기

IT/기타

[Javascript] Jqgrid Custom

Jqgrid 4.7.1

function CustomGrid(option) {

    var {gridId, form, url, data} = option;
    var postData;

    if (nonEmpty(form) && nonEmpty(data)) {
        postData = $.extend({}, serializeObject(form), data);
    } else if (isEmpty(form)) {
        postData = data;
    } else {
        postData = serializeObject(form);
    }

    console.log(postData);

    var defaultOption = {
        datatype: 'json',
        mtype: 'POST',
        postData: postData,
        loadError: function (xhr, status, error) {
        },
        loadonce: false,
        rownumbers: true,
        autowidth: true,
        page: 1,
        multiselect: true,
        rowNum: 20,
        rowList: [10, 20, 50, 100],
        pager: $(`#${gridId}Pager`),
        viewrecords: true,
        sortable: true,
        height: 'auto',
        autoencode: true,
        shrinkToFit: true,
        rownumWidth: 50,
        gridview: true
    };

    var gridOption = $.extend({}, defaultOption, option);
    var grid = $(`#${gridId}`).jqGrid(gridOption);

    function reload() {
        jQuery(grid).jqGrid().setGridParam(
                {
                    datatype: 'json',
                    postData: isEmpty(form) ? {} : serializeObject(form),
                    url: url,
                    page: this.getPageNo()
                }
        ).trigger("reloadGrid");
    }


    function serializeObject(form) {
        var unindexed_array = form.serializeArray();
        var indexed_array = {};

        $.map(unindexed_array, function (n, i) {
            indexed_array[n['name']] = n['value'];
        });

        return indexed_array;
    }

    function isEmpty(str) {
        return str === "" || str === undefined || str === null;
    }
    function nonEmpty(str) {
        return !isEmpty(str);
    }

    function isEmptyObject(obj) {
        return Object.keys(obj).length === 0 && obj.constructor === Object;
    }

    this.selectRowDelete = function () {
        var selectId = this.getSelectId();
        grid.jqGrid('delRowData', selectId);

        return selectId;
    };

    this.selectRowsDelete = function () {
        var selectIds = this.getSelectIds();
        var ids = [];

        for (var index = selectIds.length - 1; index > -1; index--) {
            ids.push(selectIds[index]);
            grid.jqGrid('delRowData', selectIds[index]);
        }

        return ids;
    };

    this.getPageNo = function () {
        return grid[0].p.page;
    };

    this.getRowIds = function () {
        return grid.jqGrid('getDataIDs');
    };

    this.getRowById = function (id) {
        return grid.jqGrid('getRowData', id);
    };

    this.getSelectRow = function () {
        return this.getRowById(this.getSelectId());
    };
    this.getSelectRows = function () {
        var selectIds = this.getSelectIds();
        var rows = [];
        for (var id of selectIds) {
            rows.push(this.getRowById(id));
        }
        return rows;
    };

    this.getSelectId = function () {
        return grid.jqGrid('getGridParam', 'selrow');
    };
    this.getSelectIds = function () {
        return grid.jqGrid('getGridParam', 'selarrrow');
    };

    this.getCell = function (id, colName) {
        return grid.jqGrid('getCell', id, colName);
    };

    this.addRow = function (id, rowData, loc) {
        if (isEmptyObject(rowData)) {
            return false;
        } else {
            if (isEmpty(loc)) {
                loc = 'last';
            }
            grid.jqGrid("addRowData", id, rowData, loc); // 마지막 행에 Row 추가
            return true;
        }
    };

    this.addUniqueRow = function (id, rowData, loc) {
        var row = this.getRowById(id);

        if (isEmptyObject(row)) {
            this.addRow(id, rowData, loc);
            return true;
        } else {
            return false;
        }
    };

    this.dragAndDrop = function () {
        grid.sortableRows();
        grid.jqGrid('gridDnD');
    };

    this.grid = grid;
    this.reload = reload;

    this.changeRowColor = function (id, color) {
        grid.jqGrid("setRowData", id, false, {background: color});
    };

}

'IT > 기타' 카테고리의 다른 글

[JWT] Json Web Token이란?  (0) 2020.10.02
[DEV] 컴파일과 빌드란  (0) 2020.09.10
[보안] 공개키 암호화 방식  (0) 2020.07.20
XSS란?  (0) 2020.07.16
OAuth2 란?  (1) 2020.02.29