PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/index.html

https://github.com/antonyraj/backgrid
HTML | 1884 lines | 1766 code | 103 blank | 15 comment | 0 complexity | 6a8929c0a7a3a7a7f99376fd16f961c2 MD5 | raw file
Possible License(s): MIT

Large files files are truncated, but you can click here to view the full file

  1. <!doctype html>
  2. <!--
  3. backgrid
  4. http://github.com/wyuenho/backgrid
  5. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  6. Licensed under the MIT @license.
  7. -->
  8. <html lang="en">
  9. <head>
  10. <meta charset="utf-8" />
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
  12. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
  13. <meta name="keywords" content="backgrid,backgrid.js,backbone,backbone.js,datagrid,grid,paginator,javascript" />
  14. <meta name="description" content="A powerful widget set for building data grids with Backbone.js" />
  15. <title>Backgrid.js - A powerful widget set for building data grids with Backbone.js</title>
  16. <link rel="canonical" href="http://backgridjs.com" />
  17. <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap.min.css" />
  18. <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/css/bootstrap-responsive.min.css" />
  19. <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.3.2/select2.min.css" />
  20. <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/codemirror.min.css" />
  21. <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/2.36.0/ambiance.min.css" />
  22. <link rel="stylesheet" href="lib/backgrid.min.css" />
  23. <link rel="stylesheet" href="lib/extensions/paginator/backgrid-paginator.min.css" />
  24. <link rel="stylesheet" href="lib/extensions/text-cell/backgrid-text-cell.min.css" />
  25. <link rel="stylesheet" href="lib/extensions/moment-cell/backgrid-moment-cell.min.css" />
  26. <link rel="stylesheet" href="lib/extensions/select2-cell/backgrid-select2-cell.min.css" />
  27. <link rel="stylesheet" href="lib/extensions/select-all/backgrid-select-all.min.css" />
  28. <link rel="stylesheet" href="lib/extensions/filter/backgrid-filter.min.css" />
  29. <link rel="stylesheet" href="assets/css/doc.css" />
  30. <!--[if lt IE 9]>
  31. <script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
  32. <![endif]-->
  33. </head>
  34. <body>
  35. <div id="main">
  36. <header>
  37. <div class="navbar navbar-fixed-top">
  38. <div class="navbar-inner">
  39. <div class="container">
  40. <a href="."
  41. class="brand" title="A powerful widget set for building data grids with Backbone.js">Backgrid.js</a>
  42. <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
  43. <span class="icon-bar"></span>
  44. <span class="icon-bar"></span>
  45. <span class="icon-bar"></span>
  46. </a>
  47. <div class="nav-collapse collapse">
  48. <ul class="nav nav-pills">
  49. <li class="dropdown">
  50. <a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">
  51. Introduction
  52. <b class="caret"></b>
  53. </a>
  54. <ul class="dropdown-menu">
  55. <li><a href="#features">Features</a></li>
  56. <li><a href="#getting-started">Getting Started</a></li>
  57. <li><a href="#examples">Examples</a></li>
  58. </ul>
  59. </li>
  60. <li class="dropdown">
  61. <a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">
  62. API Reference
  63. <b class="caret"></b>
  64. </a>
  65. <ul class="dropdown-menu">
  66. <li><a href="#api-grid">Grid</a></li>
  67. <li><a href="#api-column">Column</a></li>
  68. <li><a href="#api-cell">Cell</a></li>
  69. <li><a href="#api-formatter">Formatter</a></li>
  70. <li><a href="#api-header">Header</a></li>
  71. <li><a href="#api-row">Row</a></li>
  72. <li><a href="#api-body">Body</a></li>
  73. <li><a href="#api-footer">Footer</a></li>
  74. <li class="divider"></li>
  75. <li><a href="#extensions"><strong class="muted">Extensions</strong></a></li>
  76. <li class="divider"></li>
  77. <li><a href="#api-select-all">SelectAll</a></li>
  78. <li><a href="#api-paginator">Paginator</a></li>
  79. <li><a href="#api-filter">Filter</a></li>
  80. <li><a href="#api-text-cell">TextCell</a></li>
  81. <li><a href="#api-moment-cell">MomentCell</a></li>
  82. <li><a href="#api-select2-cell">Select2Cell</a></li>
  83. </ul>
  84. </li>
  85. <li class="dropdown">
  86. <a class="dropdown-toggle" data-toggle="dropdown" data-target="#" href="#">
  87. More Info
  88. <b class="caret"></b>
  89. </a>
  90. <ul class="dropdown-menu">
  91. <li><a href="#styling">Styling</a></li>
  92. <li class="divider"></li>
  93. <li><a href="#faq">FAQ</a></li>
  94. <li><a href="https://github.com/wyuenho/backgrid/blob/master/CHANGELOG.md" target="_blank">Change Log</a></li>
  95. <li><a href="#license">License</a></li>
  96. </ul>
  97. </li>
  98. <li id="social-media">
  99. <a href="https://twitter.com/share" class="twitter-share-button" data-url="http://backgridjs.com" data-counturl="wyuenho.github.com/backgrid" data-via="wong_jim" data-hashtags="backgridjs,backbone_js">Tweet</a>
  100. <div class="g-plusone" data-size="medium"></div>
  101. <iframe src="http://ghbtns.com/github-btn.html?user=wyuenho&repo=backgrid&type=watch&count=true"
  102. allowtransparency="true" frameborder="0" scrolling="0" width="110" height="20"></iframe>
  103. <a href="https://coderwall.com/wyuenho"><img alt="Endorse wyuenho on Coderwall" src="http://api.coderwall.com/wyuenho/endorsecount.png" /></a>
  104. <iframe allowtransparency="true" frameborder="0" scrolling="no"
  105. style="border: 0; margin: 0 0 0 15px; padding: 0;"
  106. src="https://www.gittip.com/wyuenho/widget.html" width="48pt" height="22pt"></iframe>
  107. </li>
  108. </ul>
  109. </div>
  110. </div>
  111. <a class="visible-desktop" href="https://github.com/wyuenho/backgrid">
  112. <img style="position: absolute; top: 0; right: 0; border: 0;" src="assets/img/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub">
  113. </a>
  114. </div>
  115. </div>
  116. </header>
  117. <div class="container">
  118. <section class="hero-unit">
  119. <h1>Backgrid.js</h1>
  120. <p class="lead">
  121. Backgrid.js is a set of components for
  122. building <strong>semantic</strong> and <strong>easily
  123. stylable</strong> <strong>data grid</strong> widgets.</p>
  124. <p class="lead">It offers a simple, intuitive
  125. programming interface that makes easy things easy, but <em>hard things possible</em>
  126. when dealing with tabular data.</p>
  127. <p>
  128. <div class="btn-group">
  129. <a class="btn btn-large btn-success" href="#examples">See Examples</a>
  130. <a class="btn btn-large btn-primary hidden-phone hidden-tablet"
  131. href="https://github.com/wyuenho/backgrid/tags">Download v0.2.6</a>
  132. <a class="btn btn-large btn-info"
  133. href="https://github.com/wyuenho/backgrid/blob/0.2.6/CHANGELOG.md">What's new?</a>
  134. </div>
  135. </p>
  136. </section>
  137. <div id="features">
  138. <div class="page-header">
  139. <h1>Features</h1>
  140. <p>The goal of Backgrid.js is to produce a set of core Backbone UI
  141. elements that offer you all the basic displaying, sorting and
  142. editing functionalities you&apos;d expect, and to create an
  143. elegant API that makes extending Backgrid.js with extra
  144. functionalities easy.</p>
  145. </div>
  146. <div class="row-fluid">
  147. <div class="span6">
  148. <h2>Advantages</h2>
  149. <ul>
  150. <li>No Hungarian notations.</li>
  151. <li>Solid foundation. Based
  152. on <a href="http://backbonejs.org">Backbone.js</a>.</li>
  153. <li>Semantic and <a href="#styling">easily stylable</a>. Just
  154. style with plain CSS like you would a normal HTML table.</li>
  155. <li>Low learning curve. Works with plain old Backbone models and
  156. collections. Easy things are easy, hards things possible.</li>
  157. <li>Highly modular and customizable. Componenets are just simple
  158. Backbone View classes, customization is easy if you already
  159. know Backbone.</li>
  160. <li>Lightweight. Extra features are separated
  161. into <a href="#extensions">extensions</a>, which keeps the
  162. bloat away.</li>
  163. <li>Good documentation.</li>
  164. <li>Well tested. Comes with <a href="test/" title="Jasmine Tests">100s of test cases</a>.</li>
  165. </ul>
  166. </div>
  167. <div class="span6">
  168. <h2>Supported browsers: <b style="font-size: 14px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: normal;">[1]</b></h2>
  169. <ul>
  170. <li>Internet Explorer 8+</li>
  171. <li>Chrome 4+</li>
  172. <li>Safari 4+</li>
  173. <li>Firefox 4+</li>
  174. <li>Opera 9+</li>
  175. </ul>
  176. <aside class="note">
  177. <h3>Notes:</h3>
  178. <div>1. Both the desktop and mobile versions of the above browsers are supported.</div>
  179. </aside>
  180. </div>
  181. </div>
  182. </div>
  183. <section id="getting-started">
  184. <div class="page-header">
  185. <h1>Getting Started</h1>
  186. </div>
  187. <div class="row-fluid">
  188. <div class="span6">
  189. <p>
  190. Backgrid.js depends on 3 libraries to function:
  191. </p>
  192. <ul>
  193. <li><a href="http://jquery.com">jquery &gt;= 1.7.0</a></li>
  194. <li><a href="http://underscorejs.org">underscore.js ~ 1.4.0</a></li>
  195. <li><a href="http://backbonejs.org">backbone.js >= 0.9.10</a></li>
  196. </ul>
  197. <aside class="note">
  198. <h5>Note:</h5> If you don&apos;t care about having the latest
  199. versions of the dependencies, you can use the bundled libraries
  200. in the download package.
  201. </aside>
  202. </div>
  203. <div class="span6">
  204. <p>
  205. Something like the following will get
  206. the <strong>Backgrid.js</strong> core loaded:
  207. </p>
  208. <textarea class="code-snippet" data-mode="htmlmixed">
  209. &lt;link rel="stylesheet" href="lib/backgrid.css" /&gt;
  210. &lt;script src="assets/js/jquery.js"&gt;&lt;/script&gt;
  211. &lt;script src="assets/js/underscore.js"&gt;&lt;/script&gt;
  212. &lt;script src="assets/js/backbone.js"&gt;&lt;/script&gt;
  213. &lt;script src="lib/backgrid.js"&gt;&lt;/script&gt;</textarea>
  214. <p>Adjust the paths as needed.</p>
  215. </div>
  216. </div>
  217. </section>
  218. <section id="examples">
  219. <div class="page-header">
  220. <h1>Examples</h1>
  221. </div>
  222. <div class="row-fluid">
  223. <div class="span12">
  224. <div class="row-fluid">
  225. <div class="span6">
  226. <h2>Collection and Model</h2>
  227. <p>Before you can display any tabular data in a grid, you must
  228. first obtain the data.</p>
  229. <p>At the most basic level, Backgrid pretends that every row
  230. is a model object and the whole table is backed by a simple
  231. Backbone collection.</p>
  232. <p>Suppose we have a list of territory info objects:</p>
  233. </div>
  234. <div class="span6">
  235. <textarea class="code-snippet" data-mode="javascript" data-eval="yes">
  236. var Territory = Backbone.Model.extend({});
  237. var Territories = Backbone.Collection.extend({
  238. model: Territory,
  239. url: "examples/territories.json"
  240. });
  241. var territories = new Territories();</textarea>
  242. </div>
  243. </div>
  244. <div class="row-fluid">
  245. <div class="span12">
  246. <h2 id="example-1">Grid</h2>
  247. <p>
  248. The main entry point of the Backgrid package is the
  249. Backgrid.Grid class. You can create a default Backgrid by
  250. first defining some columns, and then put that list of
  251. columns and the collection of data into the Grid constructor
  252. like this:
  253. </p>
  254. <textarea class="code-snippet" data-mode="javascript" data-eval="yes">
  255. var columns = [{
  256. name: "id", // The key of the model attribute
  257. label: "ID", // The name to display in the header
  258. editable: false, // By default every cell in a column is editable, but *ID* shouldn&apos;t be
  259. // Defines a cell type, and ID is displayed as an integer without the &apos;,&apos; separating 1000s.
  260. cell: Backgrid.IntegerCell.extend({
  261. orderSeparator: &apos;&apos;
  262. })
  263. }, {
  264. name: "name",
  265. label: "Name",
  266. // The cell type can be a reference of a Backgrid.Cell subclass, any Backgrid.Cell subclass instances like *id* above, or a string
  267. cell: "string" // This is converted to "StringCell" and a corresponding class in the Backgrid package namespace is looked up
  268. }, {
  269. name: "pop",
  270. label: "Population",
  271. cell: "integer" // An integer cell is a number cell that displays humanized integers
  272. }, {
  273. name: "percentage",
  274. label: "% of World Population",
  275. cell: "number" // A cell type for floating point value, defaults to have a precision 2 decimal numbers
  276. }, {
  277. name: "date",
  278. label: "Date",
  279. cell: "date",
  280. }, {
  281. name: "url",
  282. label: "URL",
  283. cell: "uri" // Renders the value in an HTML anchor element
  284. }];
  285. // Initialize a new Grid instance
  286. var grid = new Backgrid.Grid({
  287. columns: columns,
  288. collection: territories
  289. });
  290. // Render the grid and attach the root to your HTML document
  291. $("#example-1-result").append(grid.render().$el);
  292. // Fetch some countries from the url
  293. territories.fetch({reset: true});</textarea>
  294. </div>
  295. </div>
  296. <div class="row-fluid">
  297. <div class="span12">
  298. <h3>Result</h3>
  299. <div id="example-1-result" class="backgrid-container"></div>
  300. <aside class="note">See <a href="http://en.wikipedia.org/wiki/List_of_countries_by_population">Wikipedia</a></aside>
  301. <br />
  302. <p>The list of column definitions Backgrid.Grid expects is
  303. simply a list of JSON objects, which you can hardcode into
  304. your HTML templates or retrieve from a server when the DOM
  305. is ready. Backgrid.js doesn&apos;t care where the column
  306. definitions come from, as long as you supply the list to the
  307. Grid constructor.</p>
  308. <p>As expected, you now have a basic editable data grid
  309. displayed. All the columns headers are labeled and sortable by
  310. default. ID cells are not editable, and all other cell types
  311. have reasonable validation built in. If the table gets too
  312. large, you get a scroll bar.</p>
  313. <p>Backgrid.js comes with <a href="#api-cell" title="Cells">10
  314. basic cell types</a> in the core
  315. and <a href="#api-text-cell" title="TextCell">many others
  316. as extensions</a>. Cell types such as
  317. <a href="#api-select2-cell"
  318. title="Select2Cell">Select2Cell</a>
  319. and <a href="#api-moment-cell"
  320. title="MomentCell">MomentCell</a> give you a much richer
  321. editing interface for option lists and datetime values on
  322. desktop browsers. In addition, there&apos;s a wide range
  323. of possibilities with how the data is converted for
  324. display and persistence by using <a href="#api-formatter"
  325. title="Formatter">formatters</a> or
  326. customizing <a href="#api-cell">cell classes</a>.</p>
  327. </div>
  328. </div>
  329. <div class="row-fluid">
  330. <div class="span12">
  331. <h2 id="example-2">A More Complete Example</h2>
  332. <p>If you have a large result set like the above, you&apos;d
  333. probably want to be able to paginate and filter your
  334. results. This is easily achieved in Backgrid.js.</p>
  335. <p>Backgrid.js comes with a number of filters and a paginator
  336. extension which you can load by including the following into
  337. your <code>head</code> tag:</p>
  338. <textarea class="code-snippet" data-mode="htmlmixed">
  339. &lt;link rel="stylesheet" href="lib/extensions/filter/backgrid-filter.css" /&gt;
  340. &lt;link rel="stylesheet" href="lib/extensions/paginator/backgrid-paginator.css" /&gt;
  341. &lt;script src="assets/js/backbone-pageable.js"&gt;&lt;/script&gt;
  342. &lt;script src="lib/extensions/filter/backgrid-filter.js"&gt;&lt;/script&gt;
  343. &lt;script src="lib/extensions/paginator/backgrid-paginator.js"&gt;&lt;/script&gt;</textarea>
  344. <p>To use the paginator, you must first declare your
  345. collections to be
  346. a <a href="https://github.com/wyuenho/backbone-pageable/"
  347. title="Backbone.PageableCollection">Backbone.PageableCollection</a>,
  348. which is a simple subclass of the Backbone.js Collection with added
  349. pagination behavior.</p>
  350. <textarea class="code-snippet" data-mode="javascript" data-eval="yes">
  351. var PageableTerritories = Backbone.PageableCollection.extend({
  352. model: Territory,
  353. url: "examples/pageable-territories.json",
  354. state: {
  355. pageSize: 15
  356. },
  357. mode: "client" // page entirely on the client side
  358. });
  359. var pageableTerritories = new PageableTerritories();
  360. // Set up a grid to use the pageable collection
  361. var pageableGrid = new Backgrid.Grid({
  362. columns: columns,
  363. collection: pageableTerritories
  364. });
  365. // Render the grid
  366. var $example2 = $("#example-2-result");
  367. $example2.append(pageableGrid.render().$el)
  368. // Initialize the paginator
  369. var paginator = new Backgrid.Extension.Paginator({
  370. collection: pageableTerritories
  371. });
  372. // Render the paginator
  373. $example2.append(paginator.render().$el);
  374. // Initialize a client-side filter to filter on the client
  375. // mode pageable collection's cache.
  376. var filter = new Backgrid.Extension.ClientSideFilter({
  377. collection: pageableTerritories.fullCollection,
  378. fields: ['name']
  379. });
  380. // Render the filter
  381. $example2.prepend(filter.render().$el);
  382. // Add some space to the filter and move it to the right
  383. filter.$el.css({float: "right", margin: "20px"});
  384. // Fetch some data
  385. pageableTerritories.fetch({reset: true});</textarea>
  386. </div>
  387. </div>
  388. <div class="row-fluid">
  389. <div class="span12">
  390. <h3>Result</h3>
  391. <div id="example-2-result" class="backgrid-container"></div>
  392. </div>
  393. </div>
  394. </div>
  395. </div>
  396. </section>
  397. <section>
  398. <div class="page-header">
  399. <h1>API Reference</h1>
  400. </div>
  401. <div class="row-fluid">
  402. <div class="span2">
  403. <h2 id="api-grid">Grid</h2>
  404. <ul class="nav">
  405. <li><a href="api/index.html#!/api/Backgrid.Grid">Backgrid.Grid</a></li>
  406. </ul>
  407. <h3>Events</h3>
  408. <ul class="nav">
  409. <li><a href="api/index.html#!/api/Backgrid.Grid-method-render">backgrid:rendered</a></li>
  410. </ul>
  411. </div>
  412. <div class="span10">
  413. <h3>How to Use the Grid</h3>
  414. <p>As described in the <a href="#examples">examples</a> above, a
  415. basic grid needs only a collection and a list of column
  416. definitions.</p>
  417. <h3>Manipulating Columns and Rows</h3>
  418. <p>It is very easy to insert or remove a row in a grid, you just
  419. have to pass a model reference
  420. to <a href="api/index.html#!/api/Backgrid.Grid-method-insertRow">Grid#insertRow</a>
  421. or <a href="api/index.html#!/api/Backgrid.Grid-method-removeRow">Grid#removeRow</a>.</p>
  422. <textarea class="code-snippet" data-mode="javascript">
  423. // Inserting rows
  424. grid.insertRow([{
  425. // model 1
  426. }, {
  427. // model 2 ... etc
  428. }]);
  429. // Remove rows
  430. var musketeers = grid.collection.where({ job: "Musketeer" });
  431. grid.removeRow(musketeers);</textarea>
  432. <p>Inserting and remove columns is similarly easy. You just need
  433. to pass some definitions
  434. to <a href="api/index.html#!/api/Backgrid.Grid-method-insertColumn">Grid#insertColumn</a>
  435. or
  436. <a href="api/index.html#!/api/Backgrid.Grid-method-removeColumn">Grid#removeColumn</a>.</p>
  437. <textarea class="code-snippet" data-mode="javascript">
  438. // Insert a few new columns. Make sure your model has these attributes though.
  439. grid.insertColumn([{
  440. name: "selected",
  441. label: "",
  442. cell: "boolean",
  443. sortable: false,
  444. headerCell: MySelectAllCell
  445. }, {
  446. name: "address",
  447. label: "Address",
  448. cell: "string"
  449. }]);
  450. // Remove a column
  451. var genderCol = grid.columns.where({ name: "gender" });
  452. grid.removeColumn(genderCol);</textarea>
  453. <h3>Customization</h3>
  454. <p>The various ways of customizing a grid are described in the
  455. following sections.</p>
  456. </div>
  457. </div>
  458. <div class="row-fluid">
  459. <div class="span2">
  460. <h2 id="api-column">Columns</h2>
  461. <ul class="nav">
  462. <li><a href="api/index.html#!/api/Backgrid.Column" title="Backgrid.Column">Backgrid.Column</a></li>
  463. <li><a href="api/index.html#!/api/Backgrid.Columns" title="Backgrid.Columns">Backgrid.Columns</a></li>
  464. </ul>
  465. </div>
  466. <div class="span10">
  467. <h3>Column Defaults</h3>
  468. <p>Column defaults and required parameters are defined in
  469. the <a href="api/index.html#!/api/Backgrid.Column-method-initialize">Backgrid.Column#initialize</a>
  470. method.</p>
  471. <h3>Column Definition</h3>
  472. <p>A Column is a placeholder for a column definition.</p>
  473. <p>You usually don&apos;t need to create an instance of this class
  474. yourself, as a collection of column instances will be created for
  475. you from a list of column object literals you provide to the
  476. Backgrid.js view class constructors.</p>
  477. <p>Internally, columns are stored as a collection in the form of
  478. Backgrid.Columns. In addition, all parent views will convert the
  479. column definition into
  480. a <a href="api/index.html#!/api/Backgrid.Columns">Backgrid.Columns</a>
  481. collection and pass a reference to any subviews that require
  482. it.</p>
  483. <h3>Listening to Column Attribute Changes</h3>
  484. <p>Occasionally, you may want to listen to column attribute change
  485. events. In that case, you can choose to initialize a
  486. Backgrid.Columns collection and listen to events from the
  487. individual models.</p>
  488. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  489. var myColumns = new Backgrid.Columns({
  490. name: "id", label: "ID", cell: "string"
  491. }, {
  492. // ...
  493. });
  494. myColumns.on("change:renderable", function (col, colAttr) {
  495. if (!colAttr) {
  496. // hide the column
  497. }
  498. });</textarea>
  499. </div>
  500. </div>
  501. <div class="row-fluid">
  502. <div class="span2">
  503. <h2 id="api-cell">Cell</h2>
  504. <ul class="nav">
  505. <li><a href="api/index.html#!/api/Backgrid.Cell">Backgrid.Cell</a></li>
  506. <li><a href="api/index.html#!/api/Backgrid.DatetimeCell">Backgrid.DatetimeCell</a></li>
  507. <li><a href="api/index.html#!/api/Backgrid.DateCell">Backgrid.DateCell</a></li>
  508. <li><a href="api/index.html#!/api/Backgrid.TimeCell">Backgrid.TimeCell</a></li>
  509. <li><a href="api/index.html#!/api/Backgrid.NumberCell">Backgrid.NumberCell</a></li>
  510. <li><a href="api/index.html#!/api/Backgrid.IntegerCell">Backgrid.IntegerCell</a></li>
  511. <li><a href="api/index.html#!/api/Backgrid.StringCell">Backgrid.StringCell</a></li>
  512. <li><a href="api/index.html#!/api/Backgrid.UriCell">Backgrid.UriCell</a></li>
  513. <li><a href="api/index.html#!/api/Backgrid.EmailCell">Backgrid.EmailCell</a></li>
  514. <li><a href="api/index.html#!/api/Backgrid.BooleanCell">Backgrid.BooleanCell</a></li>
  515. <li><a href="api/index.html#!/api/Backgrid.SelectCell">Backgrid.SelectCell</a></li>
  516. <li><a href="api/index.html#!/api/Backgrid.CellEditor">Backgrid.CellEditor</a></li>
  517. <li><a href="api/index.html#!/api/Backgrid.InputCellEditor">Backgrid.InputCellEditor</a></li>
  518. <li><a href="api/index.html#!/api/Backgrid.SelectCellEditor">Backgrid.SelectCellEditor</a></li>
  519. </ul>
  520. <h3>Events</h3>
  521. <ul class="nav">
  522. <li><a href="api/index.html#!/api/Backgrid.Cell-method-enterEditMode">backgrid:edit</a></li>
  523. <li><a href="api/index.html#!/api/Backgrid.Cell-method-enterEditMode">backgrid:editing</a></li>
  524. <li><a href="api/index.html#!/api/Backgrid.InputCellEditor-method-saveOrCancel">backgrid:edited</a></li>
  525. <li><a href="api/index.html#!/api/Backgrid.InputCellEditor-method-saveOrCancel">backgrid:error</a></li>
  526. </ul>
  527. </div>
  528. <div class="span10">
  529. <h3>Demo</h3>
  530. <p class="label label-success">Try them</p>
  531. <div id="cell-demo-grid-1" class="backgrid-container" style="height: auto"></div>
  532. <div id="cell-demo-grid-2" class="backgrid-container" style="height: auto"></div>
  533. <aside class="note">
  534. <h5>Note:</h5>
  535. <p>Backgrid.js and its author are not associated with Santa and
  536. santaclaus.com in any way.</p>
  537. </aside>
  538. <h3>Configuring Cells</h3>
  539. <p>While many built-in cells provide reasonable defaults, you may
  540. choose to configure them to suit your own needs.</p>
  541. <p>Cell types that you are most likely to configure are the
  542. NumberCell, DatetimeCell and SelectCell classes. Once
  543. configured, you may use them as the cell attribute values in
  544. column definitions.</p>
  545. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  546. var grid = new Backgrid.Grid({
  547. columns: [{
  548. name: "id",
  549. label: "ID",
  550. editable: false,
  551. // Dynamically defines a new cell type with new defaults.
  552. // ID is displayed as an integer without &apos;,&apos;s.
  553. cell: Backgrid.IntegerCell.extend({
  554. orderSeparator: ''
  555. })
  556. }, {
  557. name: "lastaccessed",
  558. label: "Last Login Time",
  559. editable: false,
  560. cell: Backgrid.DatetimeCell.extend({
  561. includeMilli: true
  562. })
  563. }, {
  564. name: "gender",
  565. label: "Gender",
  566. cell: Backgrid.SelectCell.extend({
  567. // It's possible to render an option group or use a
  568. // function to provide option values too.
  569. optionValues: [["Male", "m"], ["Female", "f"]]
  570. })
  571. }],
  572. collection: col
  573. });
  574. </textarea>
  575. <p class="label label-info">Pro Tip</p>
  576. <p>SelectCell treats all option values as strings by default, if
  577. you need to persist a different type of values into your model,
  578. you should extend SelectCell to provide your
  579. own <a href="#api-formatter"
  580. title="Formatter">formatter</a>.</p>
  581. <p>See the <a href="api/index.html" title="API Doc">JSDoc</a> for
  582. the various Cell classes for details on what you can configure
  583. using this method.</p>
  584. <h3>Custom Cell</h3>
  585. <p>If the built-in and extension cell classes are not enough for
  586. you, you may choose to create your own cell class and supply it
  587. to a column definition.</p>
  588. <p>If your custom cell will still use a
  589. <code>&lt;input type="text" /&gt;</code> like the predefined
  590. ones for editing, you may choose to
  591. subclass <a href="api/index.html#!/api/Backgrid.Cell">Cell</a>
  592. or one of the predefined classes and simply define a className
  593. and a <a href="#api-formatter"
  594. title="Formatter">formatter</a>. In fact, most of the core cell
  595. classes are done this way.</p>
  596. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  597. // This is how StringCell is defined.
  598. Backgrid.StringCell = Cell.extend({
  599. // Cell default class names are the lower-cased and dasherized
  600. // form of the the cell class names by convention.
  601. className: "string-cell"
  602. formatter: Backgrid.StringFormatter
  603. });</textarea>
  604. <p>If your cell class will render differently in display mode, you
  605. may simply
  606. override <a href="api/index.html#!/api/Backgrid.Cell-method-render">Cell#render()</a>
  607. in your subclass.</p>
  608. <h3>Custom CellEditor</h3>
  609. <p class="label label-warning">Advanced Usage</p>
  610. <p>Some cell types, like the
  611. <a href="#api-text-cell" title="TextCell">TextCell</a>
  612. extension, may only make sense if the editor is rendered in a
  613. modal dialog or a form element in a different part of the
  614. page. In that case the
  615. default <a href="api/index.html#!/api/Backgrid.InputCellEditor"
  616. title="InputCellEditor">InputCellEditor</a>, which renders a
  617. <code>&lt;input type="text" /&gt;</code>, will not be suitable
  618. and a new <a href="api/index.html#!/api/Backgrid.CellEditor"
  619. title="CellEditor">CellEditor</a> must be defined.</p>
  620. <p>A custom cell editor should subclass <a
  621. href="api/index.html#!/api/Backgrid.CellEditor">CellEditor</a>
  622. as it defines a number of required parameters in its initializer
  623. and clean up operations that are necessary for most cell
  624. editors. When a cell class enters edit mode, a new editor
  625. instance is constructed by given it the required parameters, and
  626. then a Backbone event <code>backgrid:edit</code> is fired from
  627. the cell instance itself. A custom cell class can act on this
  628. event to do anything before the cell editor is rendered.</p>
  629. <p>Once the cell has entered edit mode, a Backbone event
  630. <code>backgrid:editing</code> is fired. A custom cell class can
  631. then act on it to do anything after the cell editor has been
  632. rendered, e.g. placing the focus inside the editor.</p>
  633. <p>During editing, if an error is encountered (see the <a
  634. href="#api-formatter">formatter protocol</a> below), a cell
  635. editor should fire a Backbone event <code>backgrid:error</code>
  636. so that listeners&mdash;usually a cell instance&mdash;can
  637. respond appropriately. When editing is done, a cell editor
  638. should fire a Backbone <code>backgrid:done</code> event. A cell
  639. should be listening to this event so it can remove its editor
  640. and re-render itsef in display mode.</p>
  641. <p class="label label-important">Truely Advanced Hacking</p>
  642. <p>At the most basic level, Cells and CellEditors are simply
  643. <a href="http://backbonejs.org/#View"
  644. title="Backbone.View">Backbone.View</a> classes that are
  645. guaranteed to be given a number of parameters when constructed
  646. by <a href="api/index.html#!/api/Backgrid.Row"
  647. title="Row">Row</a>. You can use any Backbone.View as your Cell
  648. and CellEditor.</p>
  649. </div>
  650. </div>
  651. <div class="row-fluid">
  652. <div class="span2">
  653. <h2 id="api-formatter">Formatter</h2>
  654. <ul class="nav">
  655. <li><a href="api/index.html#!/api/Backgrid.CellFormatter">Backgrid.CellFormatter</a></li>
  656. <li><a href="api/index.html#!/api/Backgrid.DatetimeFormatter">Backgrid.DatetimeFormatter</a></li>
  657. <li><a href="api/index.html#!/api/Backgrid.NumberFormatter">Backgrid.NumberFormatter</a></li>
  658. </ul>
  659. </div>
  660. <div class="span10">
  661. <h3>Custom Formatters</h3>
  662. <p>In Backgrid.js, cell formatters serves the purpose of converting values
  663. between JSON value types and strings, and validation. Writing
  664. formatters for value conversion and validation is easy as you only
  665. have to conform to a very simple protocol.</p>
  666. <p>Any formatters must have the following two methods defined:</p>
  667. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  668. var formatter = {
  669. // function (*): string
  670. fromRaw: function (rawData) { },
  671. // function (string): *|undefined
  672. toRaw: function (formattedData) { }
  673. };</textarea>
  674. <p><code>fromRaw()</code> is called by Backgrid.Cell and its
  675. subclasses whenever a raw model value needs to be formatted into
  676. a humanized form for display.</p>
  677. <p><code>toRaw()</code> is called by Backgrid.CellEditor and its
  678. subclasses whenever a user input string needs to be converted back
  679. to a raw JSON value for model persistence.</p>
  680. <h3>Validation</h3>
  681. <p>In addition to user input conversion, toRaw() also validates
  682. the user input during conversion. If the user input is invalid
  683. or cannot be converted to a JSON value,
  684. toRaw() <strong>MUST</strong> return <code>undefined</code>
  685. instead of throwing an Error.</p>
  686. <p>In addition to using formatters to do simple yes or no
  687. validations, if your model class has
  688. a <a href="http://backbonejs.org/#Model-validate"
  689. title="Backbone.Model#validate">validate()</a> method defined,
  690. it will also be used for validation after trying with the
  691. formatter.</p>
  692. <h3>Using Custom Formatters</h3>
  693. <p>A custom formatter can be used instead of the cell's default by
  694. extending the cell:</p>
  695. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  696. var grid = new Backgrid.Grid({
  697. columns: [{
  698. name: "url",
  699. cell: "uri",
  700. formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
  701. fromRaw: function (rawValue) {
  702. return rawValue.replace("http://", '');
  703. }
  704. })
  705. }],
  706. collection: col
  707. });</textarea>
  708. </div>
  709. </div>
  710. <div class="row-fluid">
  711. <div class="span2">
  712. <h2 id="api-header">Header</h2>
  713. <ul class="nav">
  714. <li><a href="api/index.html#!/api/Backgrid.Header">Backgrid.Header</a></li>
  715. <li><a href="api/index.html#!/api/Backgrid.HeaderRow">Backgrid.HeaderRow</a></li>
  716. <li><a href="api/index.html#!/api/Backgrid.HeaderCell">Backgrid.HeaderCell</a></li>
  717. </ul>
  718. <h3>Events</h3>
  719. <ul class="nav">
  720. <li><a href="api/index.html#!/api/Backgrid.HeaderCell-method-sort">backgrid:sort</a></li>
  721. </ul>
  722. </div>
  723. <div class="span10">
  724. <h3>Understanding the Default Header</h3>
  725. <p>Backgrid.js comes with a default header section, a header row
  726. and a header cell implementation that renders a sorter if the
  727. column is sortable. The text inside the header cells comes from
  728. the column definitions. If a <em>label</em> is not defined in a
  729. column definition, its name is used as the label instead.</p>
  730. <p>The default header cell implementation supports sorting in
  731. ascending or descending order, using the column&apos;s natural
  732. ordering. The sorter will also allow cycling back to the
  733. table&apos;s default sorting order, which is sorting by the
  734. model <a href="http://backbonejs.org/#Model-id"
  735. title="Backbone.Model#id">server IDs</a>
  736. and <a href="http://backbonejs.org/#Model-cid"
  737. title="Backbone.Model#cid">client IDs</a>.</p>
  738. <h3>Customizing the Header</h3>
  739. <p class="label label-warning">Advanced Usage</p>
  740. <p>You are allow to use a different header cell class on
  741. columns. There is no restriction on what a header cell must do. In fact,
  742. any <a href="http://backbonejs.org/#View"
  743. title="Backbone.View">Backbone.View</a> class can be
  744. used. However, if you wish to modify how the sorter behaves, you
  745. must implement the sorting protocol. See
  746. the <a href="api/index.html#!/api/Backgrid.HeaderCell"
  747. title="Backgrid.HeaderCell API">JSDoc</a> for details.</p>
  748. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  749. var SelectAllHeaderCell = Backgrid.HeaderCell.extend({
  750. // Implement your "select all" logic here
  751. });
  752. var grid = new Backgrid.Grid({
  753. columns: [{
  754. name: "selected",
  755. label: "",
  756. sortable: false,
  757. cell: "boolean",
  758. headerCell: SelectAllHeaderCell
  759. }],
  760. collection: col
  761. });</textarea>
  762. </div>
  763. </div>
  764. <div class="row-fluid">
  765. <div class="span2">
  766. <h2 id="api-row">Row</h2>
  767. <ul class="nav">
  768. <li><a href="api/index.html#!/api/Backgrid.Row">Backgrid.Row</a></li>
  769. </ul>
  770. </div>
  771. <div class="span10">
  772. <h3>Customizing Row</h3>
  773. <p class="label label-warning">Advanced Usage</p>
  774. <p>A row is simply an intermediary view class that constructs the
  775. appropriate Cell class instances for rendering the model
  776. columns.</p>
  777. <p>If you would like to override how a row is rendered, you may
  778. define your own Row subclass and give it to the Grid constructor
  779. as an option:</p>
  780. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  781. var ZebraStrippingRow = Backgrid.Row.extend({
  782. // ...
  783. });
  784. var grid = new Backgrid.Grid({
  785. row: ZebraStrippingRow, // <-- Tell the new Body class to use ZebraStrippingRow to render rows.
  786. columns: [{
  787. //...
  788. }],
  789. collection: col
  790. });</textarea>
  791. </div>
  792. </div>
  793. <div class="row-fluid">
  794. <div class="span2">
  795. <h2 id="api-body">Body</h2>
  796. <ul class="nav">
  797. <li><a href="api/index.html#!/api/Backgrid.Body">Backgrid.Body</a></li>
  798. </ul>
  799. <h3>Events</h3>
  800. <ul class="nav">
  801. <li><a href="api/index.html#!/api/Backgrid.Body-method-refresh">backgrid:refresh</a></li>
  802. </ul>
  803. </div>
  804. <div class="span10">
  805. <h3>Customizing Body</h3>
  806. <p class="label label-important">Truely Advanced Hacking</p>
  807. <p>Body is the intermediary view that coordinates between the
  808. various parts of the grid. Specifically, the default
  809. implementation is responsible for re-rendering the rows when any
  810. model is inserted into, removed from, or reordered in the
  811. underlying collection. See
  812. the <a href="api/index.html#!/api/Backgrid.Body">JSDoc</a> for
  813. details.</p>
  814. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  815. var MyBody = Backgrid.Body.extend({
  816. // I really don't know why you would do this, but you can if you want
  817. });
  818. var grid = new Backgrid.Grid({
  819. body: MyBody,
  820. columns: [{
  821. // ...
  822. }],
  823. collection: col
  824. });</textarea>
  825. </div>
  826. </div>
  827. <div class="row-fluid">
  828. <div class="span2">
  829. <h2 id="api-footer">Footer</h2>
  830. <ul class="nav">
  831. <li><a href="api/index.html#!/api/Backgrid.Footer">Backgrid.Footer</a></li>
  832. </ul>
  833. </div>
  834. <div class="span10">
  835. <h3>Putting Things at The End of a Table</h3>
  836. <p>The default Footer class is an abstract class that only defines
  837. a number of required constructor parameters. If you wish to
  838. append additional information to the end of a table you must
  839. subclass Footer and supply the class to the Grid
  840. constructor.</p>
  841. <textarea class="code-snippet" data-mode="javascript" data-eval="no">
  842. var CaptionFooter = Backgrid.Footer.extend({
  843. render: function () {
  844. this.$el.html("&lt;tr&gt;&lt;td colspan=&apos;6&apos;&gt;Hello World!&lt;/td&gt;&lt;/tr&gt;");
  845. return this;
  846. }
  847. });
  848. var grid = new Backgrid.Grid({
  849. columns: [{
  850. //...
  851. }],
  852. collection: col,
  853. footer: CaptionFooter // <--
  854. });</textarea>
  855. <p>Very often, you&apos;ll want to append a paginator to the end
  856. of your table if there are too many rows. For this, there&apos;s already
  857. a <a href="#api-paginator">paginator extension</a> provided for you.</p>
  858. </div>
  859. </div>
  860. </section>
  861. <section id="extensions">
  862. <div class="page-header">
  863. <h1>Extensions</h1>
  864. <p>It is not necessary to create an extension project if you simply
  865. want to customize or extend some Backgrid.js classes for your own
  866. private use; however, if you have one or more Backgrid.js
  867. components that add new features to the core, you may consider
  868. packaging them up to share with the world.</p>
  869. <p>A Backgrid.js extension is a directory structure that packages
  870. the necessary JS, CSS, tests, and document files.</p>
  871. <p>To create an extension, clone the tree into your file system and
  872. type:</p>
  873. <textarea class="code-snippet" data-mode="shell">
  874. $ cd backgrid
  875. $ make extension</textarea>
  876. <p>A new extension directory structure should have been created for
  877. you under <code>src/extensions</code>. The current implementation
  878. of <code>make extension</code> only creates a blank directory
  879. filled with a number of blank files for you. You should take a
  880. look at other extensions to copy what you need. e.g. Makefile,
  881. .gitignore.</p>
  882. <h2>Extension Development Guide</h2>
  883. <p>The following is a guideline for extension development:</p>
  884. <ul>
  885. <li>There should be 1 .js file and 1 .css file. If your code base
  886. gets too large, consider breaking it into multiple extensions.</li>
  887. <li>There should be 1 .min.js file and 1 .min.css file produced
  888. for distribution.</li>
  889. <li>Your Makefile should emulate other extensions as closely
  890. as possible. Specifically, you should have a <code>dist</code>
  891. rule that will take a <code>DIST_DIR</code> variable from
  892. top-level make invocations and copy the output files to the
  893. destination directory.</li>
  894. <li>You should use recess to lint your CSS file(s).</li>
  895. <li>You should follow the JS coding style defined <a
  896. href="https://github.com/wyuenho/backgrid/blob/gh-pages/CONTRIBUTING.md#coding-style">here</a>.</li>
  897. <li>Your .gitignore file inside the extension directory should
  898. ignore all output files.</li>
  899. <li>You should wrap your JS file in an immediately invoked
  900. anonymous function that lists out your dependencies in the
  901. parameter list.</li>
  902. <li>Your extension should live under
  903. the <code>Backgrid.Extension</code> module.</li>
  904. <li>You should clearly specify your dependencies

Large files files are truncated, but you can click here to view the full file