http://www.jquery-bootgrid.com/
https://github.com/rstaib/jquery-bootgrid
http://localhost/~smr/jquery-bootgrid/demo/index.htm
http://www.jquery-bootgrid.com/Documentation
http://www.jquery-bootgrid.com/Examples
clone repo, run sample in localhost
http://localhost/~smr/jquery-bootgrid/demo/index-work.htm
basics
basics: take a bootstrap table and turn it into a grid control
There are
two ways to populate jQuery Bootgrid. Either you use the server-side way (e.g. a REST service) for querying a dynamic data source like a database or you use the client-side way for simple in-memory data visualization.
To use the
client-side way you need almost no configuration. Just add your rows via HTML or use the append method to append rows dynamically (see here).
For using the
server-side way you must set at least the ajax option to true and pass an URL to the url option (see here).
All settings can also be set via the data API on the table tag for general settings and th tags for column specific settings.
simple example (client-side data) in the rails4sandbox
N.B. you need a table head section with this structure:
<table>
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
for the grid to work properly.
http://localhost:3001/boot_grid/index
http://localhost:3001/boot_grid/simple
install with bower:
http://www.jquery-bootgrid.com/GettingStarted
in app js:
//= require jquery.bootgrid
in application.scss:
@import "jquery.bootgrid/dist/jquery.bootgrid.css";
make a new rails controller
/Users/smr/current_projects/rails4sandbox/app/views/boot_grid/index.html.erb
copy table data from /Users/smr/Sites/jquery-bootgrid/demo/index-work.htm
<table id="grid" class="table table-condensed table-hover table-striped" data-selection="true" data-multi-select="true" data-row-select="true" data-keep-selection="true">
<thead>
<tr>
<th data-column-id="id" data-identifier="true" data-type="numeric" data-align="right" data-width="40">ID</th>
<th data-column-id="sender" data-order="asc" data-align="center" data-header-align="center">Sender</th>
<th data-column-id="received" data-css-class="cell" data-header-css-class="column" data-filterable="true">Received</th>
<th data-column-id="link" data-formatter="link" data-sortable="false" data-width="75px">Link</th>
<th data-column-id="status" data-type="numeric" data-visible="false">Status</th>
<th data-column-id="hidden" data-visible="false" data-visible-in-selection="false">Hidden</th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
init with normal js embedded in the page:
<script>
$(function()
{
function init()
{
$("#grid").bootgrid({
formatters: {
"link": function(column, row)
{
return "<a href=\"#\">" + column.id + ": " + row.id + "</a>";
}
},
rowCount: [-1, 10, 50, 75]
});
}
init();
});
</script>
the coffeescript equivalent:
jQuery ->
if $('table#grid_simple').length
init = ->
console.log("bootgrid: init grid_simple")
$("#grid_simple").bootgrid({
formatters: {
"link": (column, row) ->
return "<a href=\"#\">" + column.id + ": " + row.id + "</a>"
},
rowCount: [-1, 10, 50, 77]
})
init()
ajax example (server-side data) in the rails4sandbox
need to set ajax=true and set the url that will provide the row data.
create an empty table (will be filled with the ajax call):
<table id="grid_data" class="table table-condensed table-hover" data-ajax="true" data-url="<%= get_bootgrid_table_data_path %>">
<thead>
<tr>
<th data-column-id="id" data-identifier="true" data-type="numeric" data-align="right" data-width="40">ID</th>
<th data-column-id="sender" data-order="asc" data-align="center" data-header-align="center">Sender</th>
<th data-column-id="received" data-css-class="cell" data-header-css-class="column" data-filterable="true">Received</th>
<th data-column-id="link" data-formatter="link" data-sortable="false" data-width="75px">Link</th>
<th data-column-id="statusRENAMED" data-type="numeric" data-visible="true">Status</th>
<th data-column-id="hidden" data-visible="false" data-visible-in-selection="false">Hidden</th>
</tr>
</thead>
<tbody></tbody>
</table>
data url can specified via the data api:
<table id="grid_data" class="table table-condensed table-hover" data-ajax="true" data-url="<%= get_bootgrid_table_data_path %>">
or in coffeescript
if $('table#grid_data').length
init = ->
console.log("bootgrid: init grid_data")
$("#grid_data").bootgrid({
rowCount: [-1, 10, 50, 75],
ajax: true,
url: "/get_bootgrid_table_data"
})
init()
stub controller method returns data that will be used to will the table:
def get_bootgrid_table_data
dict = {
current:1,
rowCount: 10,
rows: [
{
id: 1,
sender: "me@rafaelstaib.com",
received: "",
},
{
id: 2,
sender: "me@rafaelstaib.com",
received: "",
},
{
id: 3,
sender: "",
received: "stephen@fretlessmultimedia.com",
},
{
id: 4,
sender: "",
received: "stephen@prepware.com",
},
],
total: 4
}
render json: dict
end
the controller method gets passed parameters for sort, search, etc:
sort parameter (along with column names) get passed when a header is clicked:
Started POST "/get_bootgrid_table_data" for ::1 at 2016-02-19 08:46:32 -0500
Processing by BootGridController#get_bootgrid_table_data as */*
Parameters: {"current"=>"1", "rowCount"=>"-1", "sort"=>{"sender"=>"desc"}, "searchPhrase"=>""}
if a row count is specified:
Parameters: {"current"=>"1", "rowCount"=>"50", "sort"=>{"sender"=>"desc"}, "searchPhrase"=>""}
isitemtemplateand changed the code like this :$.each(that.columns, function (i, column) {
if(column.isitemtemplate){
row[column.id] = column.converter.from(cells.eq(i).html());
} else {
row[column.id] = column.converter.from(cells.eq(i).text());
}
});