Mgallegos\LaravelJqgrid\Renders\JqGridRender::renderGrid PHP Method

renderGrid() public method

Main method that construct the html and javascript code of the grid.
public renderGrid ( boolean $script = true, boolean $createTableElement = true, boolean $createPagerElement = true, boolean $echo = true ) : mixed
$script boolean If true javascript tags will be included within the output. Default is true
$createTableElement boolean If true the table element is created automatically from this method. Default is true
$createPagerElement boolean If true the pager element is created automatically from this method. Default is true
$echo boolean If false the function return the string representing the grid. Default is true
return mixed String if $echo is set to false, void in any other case
    public function renderGrid($script = true, $createTableElement = true, $createPagerElement = true, $echo = true)
    {
        $this->options = array_add($this->options, 'colModel', $this->colModel);
        if (!isset($this->options['pager'])) {
            $this->options = array_add($this->options, 'pager', $this->gridId . 'Pager');
        }
        if (isset($this->options['filename'])) {
            $fileName = mb_substr($this->options['filename'], 0, 31);
        } else {
            $fileName = $this->gridId;
        }
        if (isset($this->options['groupingView'])) {
            $groupingView = $this->options['groupingView'];
        } else {
            $groupingView = array();
        }
        if (isset($this->groupHeaderOptions['groupHeaders'])) {
            $groupHeaders = $this->groupHeaderOptions['groupHeaders'];
        } else {
            $groupHeaders = array();
        }
        $html = '';
        $html .= '<form method="' . $this->options['mtype'] . '" action="' . $this->options['url'] . '" accept-charset="UTF-8" id="' . $this->gridId . 'ExportForm">
								<input name="_token" type="hidden" value="' . $this->token . '">
								<input id="' . $this->gridId . 'Name" name="name" type="hidden" value="' . $fileName . '">
								<input id="' . $this->gridId . 'Model" name="model" type="hidden">
								<input id="' . $this->gridId . 'Sidx" name="sidx" type="hidden">
								<input id="' . $this->gridId . 'Sord" name="sord" type="hidden">
								<input id="' . $this->gridId . 'ExportFormat" name="exportFormat" type="hidden" value="xls">
								<input id="' . $this->gridId . 'Filters" name="filters" type="hidden">
								<input id="' . $this->gridId . 'PivotFlag" name="pivot" type="hidden" value="' . $this->jqPivot . '">
								<input id="' . $this->gridId . 'Rows" name="pivotRows" type="hidden">
								<input name="fileProperties" type="hidden" value=\'' . json_encode($this->fileProperties) . '\'>
								<input name="sheetProperties" type="hidden" value=\'' . json_encode($this->sheetProperties) . '\'>
								<input name="groupingView" type="hidden" value=\'' . json_encode($groupingView) . '\'>
								<input name="groupHeaders" type="hidden" value=\'' . json_encode($groupHeaders) . '\'>
							</form>';
        if ($createTableElement) {
            $html .= '<table id="' . $this->gridId . '"></table>';
        }
        if ($createTableElement) {
            $html .= '<div id="' . $this->options['pager'] . '"></div>';
        }
        if ($this->jqPivot) {
            $mtype = $this->options['mtype'];
            unset($this->options['colModel'], $this->options['mtype'], $this->options['datatype']);
            $script = 'jQuery("#' . $this->gridId . '").jqGrid("jqPivot", "' . $this->options['url'] . '", ' . json_encode($this->pivotOptions) . ', ' . json_encode($this->options) . ', {async : false, type: "' . $mtype . '"});';
            $script .= 'jQuery("#' . $this->gridId . '").navGrid("#' . $this->options['pager'] . '", ' . json_encode($this->navigatorOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorEditOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorAddOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorDeleteOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorSearchOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorViewOptions, JSON_FORCE_OBJECT) . ' );';
        } else {
            $script = 'jQuery("#' . $this->gridId . '").jqGrid(' . json_encode($this->options) . ')';
            $script .= '.navGrid("#' . $this->options['pager'] . '", ' . json_encode($this->navigatorOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorEditOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorAddOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorDeleteOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorSearchOptions, JSON_FORCE_OBJECT) . ', ' . json_encode($this->navigatorViewOptions, JSON_FORCE_OBJECT) . ' );';
        }
        /*
        		$script .= 'jQuery("#' . $this->gridId . '").jqGrid("navButtonAdd", "#' .  $this->options['pager'] . '",{"id": "' . $this->gridId . 'XlsButton", "caption":"' . $this->exportButtonsOptions['xlsButtonText'] . '", "buttonicon":"' . $this->exportButtonsOptions['xlsIcon'] . '", "onClickButton":function(){ ' . $this->getJavascriptExportFunctionCode() . ' jQuery("#' . $this->gridId . 'ExportFormat").val("xls"); jQuery("#' . $this->gridId . 'ExportForm").submit();} });';
        		$script .= 'jQuery("#' . $this->gridId . '").jqGrid("navButtonAdd", "#' .  $this->options['pager'] . '",{"id": "' . $this->gridId . 'CsvButton", "caption":"' . $this->exportButtonsOptions['csvButtonText'] . '", "buttonicon":"' . $this->exportButtonsOptions['csvIcon'] . '", "onClickButton":function(){ ' . $this->getJavascriptExportFunctionCode() . ' jQuery("#' . $this->gridId . 'ExportFormat").val("csv"); jQuery("#' . $this->gridId . 'ExportForm").submit();} });';
        
        		if($this->exportButtonsOptions['xlsButtonVisible'] || $this->exportButtonsOptions['csvButtonVisible'])
        		{
        			$script .= 'jQuery("#' . $this->gridId . '").jqGrid("navSeparatorAdd", "#' .  $this->options['pager'] . '");';
        		}
        
        		if(!$this->exportButtonsOptions['xlsButtonVisible'])
        		{
        			$script .= 'jQuery("#' . $this->gridId . 'XlsButton").hide();';
        		}
        
        		if(!$this->exportButtonsOptions['csvButtonVisible'])
        		{
        			$script .= 'jQuery("#' . $this->gridId . 'CsvButton").hide();';
        		}
        */
        foreach ($this->exportButtonsOptions as $key => $value) {
            if (preg_match('/ButtonVisible$/', $key) && gettype($value) == 'boolean') {
                $script .= "\n\n\t// Add button and hendler for " . strtoupper(substr($key, 0, -1 * strlen('ButtonVisible'))) . "-export : \n";
                $script .= $this->getJavascriptExportFunctionCode(substr($key, 0, -1 * strlen('ButtonVisible')));
            }
        }
        if ($this->exportButtonsVisible) {
            $script .= 'jQuery("#' . $this->gridId . '").jqGrid("navSeparatorAdd", "#' . $this->options['pager'] . '");';
        }
        if ($this->filterToolbarButtonsOptions['filterToolbar']) {
            $script .= 'jQuery("#' . $this->gridId . '").jqGrid("filterToolbar", ' . json_encode($this->filterToolbarOptions, JSON_FORCE_OBJECT) . ');';
            if ($this->filterToolbarButtonsOptions['toggleButton']) {
                $script .= 'jQuery("#' . $this->gridId . '").jqGrid("navButtonAdd", "#' . $this->options['pager'] . '",{"caption":"' . $this->filterToolbarButtonsOptions['toggleButtonText'] . '", "buttonicon":"ui-icon-pin-s", "onClickButton":function(){ jQuery("#' . $this->gridId . '")[0].toggleToolbar();} });';
            }
            if ($this->filterToolbarButtonsOptions['clearButton']) {
                $script .= 'jQuery("#' . $this->gridId . '").jqGrid("navButtonAdd", "#' . $this->options['pager'] . '",{"caption":"' . $this->filterToolbarButtonsOptions['clearButtonText'] . '", "buttonicon":"ui-icon-refresh", "onClickButton":function(){ jQuery("#' . $this->gridId . '")[0].clearToolbar();} });';
            }
            $script .= 'jQuery("#' . $this->gridId . '").jqGrid("navSeparatorAdd", "#' . $this->options['pager'] . '");';
        }
        if (!empty($this->groupHeaderOptions)) {
            $script .= 'setTimeout(function () {jQuery("#' . $this->gridId . '").jqGrid("setGroupHeaders", ' . json_encode($this->groupHeaderOptions) . ');}, 500);';
        }
        if ($this->frozenColumn) {
            $script .= 'jQuery("#' . $this->gridId . '").jqGrid("setFrozenColumns");';
        }
        $script = str_replace(array('"###', '###"', '\\"', '"JS>>>', '<<<JS"'), array('', '', '"', '', ''), $script);
        $this->reset();
        if ($script) {
            $script = '<script type="text/javascript">' . $script . '</script>';
        }
        if ($echo) {
            echo $html . $script;
        } else {
            return $html . $script;
        }
    }