Yii2: Create Grouping in Gridview from Scracth with Jquery

xxxBase on question from Mr xxx on group Yii Framework Indonesia, about data grouping in Yii2 Gridview.. then I interest to create a code snippet to do that.

I know in Yii1, Yii booster have a widgets to do that, but we are not see in Yii2. Okey I only use Javascript (Jquery) to do that. sure..

	<?= GridView::widget([
			'dataProvider' => $dataProvider,
			'filterModel' => $searchModel,
		...
		...
	?>
	<?php
	// You only need add this,
	$this->registerJs('
		var gridview_id = ""; // specific gridview
		var columns = [2]; // index column that will grouping, start 1

		/*
		DON\'T EDIT HERE

http://www.hafidmukhlasin.com

		*/
		var column_data = [];
			column_start = [];
			rowspan = [];

		for (var i = 0; i < columns.length; i++) {
			column = columns[i];
			column_data[column] = "";
			column_start[column] = null;
			rowspan[column] = 1;
		}

		var row = 1;
		$(gridview_id+" table > tbody  > tr").each(function() {
			var col = 1;
			$(this).find("td").each(function(){
				for (var i = 0; i < columns.length; i++) {
					if(col==columns[i]){
						if(column_data[columns[i]] == $(this).html()){
							$(this).remove();
							rowspan[columns[i]]++;
							$(column_start[columns[i]]).attr("rowspan",rowspan[columns[i]]);
						}
						else{
							column_data[columns[i]] = $(this).html();
							rowspan[columns[i]] = 1;
							column_start[columns[i]] = $(this);
						}
					}
				}
				col++;
			})
			row++;
		});
	');
	?>

done.. serious only it

CMIIW

8 Comments
  1. Gafur
  2. tony
    • Hafid Mukhlasin
  3. tony
  4. ankit
  5. Anim
  6. Anim

Leave a Reply

Your email address will not be published. Required fields are marked *