Skip to content

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) #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ It is better, you know these:
- You can use Blade Template Engine in your $content values
- The name of columns is set by returned array.
- That means, for 'posts.id' it is 'id' and also for 'owner.name as ownername' it is 'ownername'
- You can set the "index" column (http://datatables.net/reference/api/row%28%29.index%28%29) using set_index_column($name)


### Examples
Expand Down
22 changes: 21 additions & 1 deletion src/Bllim/Datatables/Datatables.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Datatables

protected $mDataSupport;

protected $index_column;


/**
* Gets query and returns instance of class
Expand Down Expand Up @@ -142,6 +144,17 @@ public function remove_column()
return $this;
}

/**
* Sets the DataTables index column (as used to set, e.g., id of the <tr> tags) to the named column
*
* @param $name
* @return $this
*/
public function set_index_column($name) {
$this->index_column = $name;
return $this;
}

/**
* Saves given query and determines its type
*
Expand Down Expand Up @@ -203,7 +216,14 @@ private function regulate_array()
unset($value[$evalue]);
}

$this->result_array_r[] = array_values($value);
$row = array_values($value);
if ($this->index_column) {
if (!array_key_exists($this->index_column, $value)) {
throw new \Exception('Index column set to non-existent column "' . $this->index_column . '"');
}
$row['DT_RowId'] = $value[$this->index_column];
}
$this->result_array_r[] = $row;
}
}
}
Expand Down