Skip to content

Commit 4114fc6

Browse files
committed
Merge pull request bgultekin#97 from langabi/master
Added function set_index_column to set the DataTables row index (DT_Row_ID / http://datatables.net/reference/api/row%28%29.index%28%29)
2 parents b48ed29 + 5b24b52 commit 4114fc6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ It is better, you know these:
5656
- You can use Blade Template Engine in your $content values
5757
- The name of columns is set by returned array.
5858
- That means, for 'posts.id' it is 'id' and also for 'owner.name as ownername' it is 'ownername'
59+
- You can set the "index" column (http://datatables.net/reference/api/row%28%29.index%28%29) using set_index_column($name)
5960

6061

6162
### Examples

src/Bllim/Datatables/Datatables.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Datatables
4040

4141
protected $mDataSupport;
4242

43+
protected $index_column;
44+
4345

4446
/**
4547
* Gets query and returns instance of class
@@ -142,6 +144,17 @@ public function remove_column()
142144
return $this;
143145
}
144146

147+
/**
148+
* Sets the DataTables index column (as used to set, e.g., id of the <tr> tags) to the named column
149+
*
150+
* @param $name
151+
* @return $this
152+
*/
153+
public function set_index_column($name) {
154+
$this->index_column = $name;
155+
return $this;
156+
}
157+
145158
/**
146159
* Saves given query and determines its type
147160
*
@@ -203,7 +216,14 @@ private function regulate_array()
203216
unset($value[$evalue]);
204217
}
205218

206-
$this->result_array_r[] = array_values($value);
219+
$row = array_values($value);
220+
if ($this->index_column) {
221+
if (!array_key_exists($this->index_column, $value)) {
222+
throw new \Exception('Index column set to non-existent column "' . $this->index_column . '"');
223+
}
224+
$row['DT_RowId'] = $value[$this->index_column];
225+
}
226+
$this->result_array_r[] = $row;
207227
}
208228
}
209229
}

0 commit comments

Comments
 (0)