jsgrid.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/tables/jsgrid", ["jquery", "Site"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(require("jquery"), require("Site"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(global.jQuery, global.Site);
  11. global.tablesJsgrid = mod.exports;
  12. }
  13. })(this, function (_jquery, _Site) {
  14. "use strict";
  15. _jquery = babelHelpers.interopRequireDefault(_jquery);
  16. (0, _jquery.default)(document).ready(function ($$$1) {
  17. (0, _Site.run)();
  18. });
  19. jsGrid.setDefaults({
  20. tableClass: "jsgrid-table table table-striped table-hover"
  21. });
  22. jsGrid.setDefaults("text", {
  23. _createTextBox: function _createTextBox() {
  24. return (0, _jquery.default)("<input>").attr("type", "text").attr("class", "form-control input-sm");
  25. }
  26. });
  27. jsGrid.setDefaults("number", {
  28. _createTextBox: function _createTextBox() {
  29. return (0, _jquery.default)("<input>").attr("type", "number").attr("class", "form-control input-sm");
  30. }
  31. });
  32. jsGrid.setDefaults("textarea", {
  33. _createTextBox: function _createTextBox() {
  34. return (0, _jquery.default)("<input>").attr("type", "textarea").attr("class", "form-control");
  35. }
  36. });
  37. jsGrid.setDefaults("control", {
  38. _createGridButton: function _createGridButton(cls, tooltip, clickHandler) {
  39. var grid = this._grid;
  40. return (0, _jquery.default)("<button>").addClass(this.buttonClass).addClass(cls).attr({
  41. type: "button",
  42. title: tooltip
  43. }).on("click", function (e) {
  44. clickHandler(grid, e);
  45. });
  46. }
  47. });
  48. jsGrid.setDefaults("select", {
  49. _createSelect: function _createSelect() {
  50. var $result = (0, _jquery.default)("<select>").attr("class", "form-control input-sm"),
  51. valueField = this.valueField,
  52. textField = this.textField,
  53. selectedIndex = this.selectedIndex;
  54. _jquery.default.each(this.items, function (index, item) {
  55. var value = valueField ? item[valueField] : index,
  56. text = textField ? item[textField] : item;
  57. var $option = (0, _jquery.default)("<option>").attr("value", value).text(text).appendTo($result);
  58. $option.prop("selected", selectedIndex === index);
  59. });
  60. return $result;
  61. }
  62. }); // Example Basic
  63. // -------------------
  64. (function () {
  65. (0, _jquery.default)('#exampleBasic').jsGrid({
  66. height: "500px",
  67. width: "100%",
  68. filtering: true,
  69. editing: true,
  70. sorting: true,
  71. paging: true,
  72. autoload: true,
  73. pageSize: 15,
  74. pageButtonCount: 5,
  75. deleteConfirm: "Do you really want to delete the client?",
  76. controller: db,
  77. fields: [{
  78. name: "Name",
  79. type: "text",
  80. width: 150
  81. }, {
  82. name: "Age",
  83. type: "number",
  84. width: 70
  85. }, {
  86. name: "Address",
  87. type: "text",
  88. width: 200
  89. }, {
  90. name: "Country",
  91. type: "select",
  92. items: db.countries,
  93. valueField: "Id",
  94. textField: "Name"
  95. }, {
  96. name: "Married",
  97. type: "checkbox",
  98. title: "Is Married",
  99. sorting: false
  100. }, {
  101. type: "control"
  102. }]
  103. });
  104. })(); // Example Static Data
  105. // -------------------
  106. (function () {
  107. (0, _jquery.default)('#exampleStaticData').jsGrid({
  108. height: "500px",
  109. width: "100%",
  110. sorting: true,
  111. paging: true,
  112. data: db.clients,
  113. fields: [{
  114. name: "Name",
  115. type: "text",
  116. width: 150
  117. }, {
  118. name: "Age",
  119. type: "number",
  120. width: 50
  121. }, {
  122. name: "Address",
  123. type: "text",
  124. width: 200
  125. }, {
  126. name: "Country",
  127. type: "select",
  128. items: db.countries,
  129. valueField: "Id",
  130. textField: "Name"
  131. }, {
  132. name: "Married",
  133. type: "checkbox",
  134. title: "Is Married"
  135. }]
  136. });
  137. })(); // Example OData Service
  138. // -------------------
  139. (function () {
  140. (0, _jquery.default)('#exampleOData').jsGrid({
  141. height: "500px",
  142. width: "100%",
  143. sorting: true,
  144. paging: false,
  145. autoload: true,
  146. controller: {
  147. loadData: function loadData() {
  148. var d = _jquery.default.Deferred();
  149. _jquery.default.ajax({
  150. url: "http://services.odata.org/V3/(S(3mnweai3qldmghnzfshavfok))/OData/OData.svc/Products",
  151. dataType: "json"
  152. }).done(function (response) {
  153. d.resolve(response.value);
  154. });
  155. return d.promise();
  156. }
  157. },
  158. fields: [{
  159. name: "Name",
  160. type: "text"
  161. }, {
  162. name: "Description",
  163. type: "textarea",
  164. width: 150
  165. }, {
  166. name: "Rating",
  167. type: "number",
  168. width: 50,
  169. align: "center",
  170. itemTemplate: function itemTemplate(value) {
  171. return (0, _jquery.default)("<div>").addClass("rating text-nowrap").append(Array(value + 1).join('<i class="icon wb-star orange-600 mr-3"></i>'));
  172. }
  173. }, {
  174. name: "Price",
  175. type: "number",
  176. width: 50,
  177. itemTemplate: function itemTemplate(value) {
  178. return value.toFixed(2) + "$";
  179. }
  180. }]
  181. });
  182. })(); // Example Sorting
  183. // ---------------
  184. (function () {
  185. (0, _jquery.default)('#exampleSorting').jsGrid({
  186. height: "500px",
  187. width: "100%",
  188. autoload: true,
  189. selecting: false,
  190. controller: db,
  191. fields: [{
  192. name: "Name",
  193. type: "text",
  194. width: 150
  195. }, {
  196. name: "Age",
  197. type: "number",
  198. width: 50
  199. }, {
  200. name: "Address",
  201. type: "text",
  202. width: 200
  203. }, {
  204. name: "Country",
  205. type: "select",
  206. items: db.countries,
  207. valueField: "Id",
  208. textField: "Name"
  209. }, {
  210. name: "Married",
  211. type: "checkbox",
  212. title: "Is Married"
  213. }]
  214. });
  215. (0, _jquery.default)("#sortingField").on('change', function () {
  216. var field = (0, _jquery.default)(this).val();
  217. (0, _jquery.default)("#exampleSorting").jsGrid("sort", field);
  218. });
  219. })(); // Example Loading Data by Page
  220. // ----------------------------
  221. (function () {
  222. (0, _jquery.default)('#exampleLoadingByPage').jsGrid({
  223. height: "500px",
  224. width: "100%",
  225. autoload: true,
  226. paging: true,
  227. pageLoading: true,
  228. pageSize: 15,
  229. pageIndex: 2,
  230. controller: {
  231. loadData: function loadData(filter) {
  232. var startIndex = (filter.pageIndex - 1) * filter.pageSize;
  233. return {
  234. data: db.clients.slice(startIndex, startIndex + filter.pageSize),
  235. itemsCount: db.clients.length
  236. };
  237. }
  238. },
  239. fields: [{
  240. name: "Name",
  241. type: "text",
  242. width: 150
  243. }, {
  244. name: "Age",
  245. type: "number",
  246. width: 50
  247. }, {
  248. name: "Address",
  249. type: "text",
  250. width: 200
  251. }, {
  252. name: "Country",
  253. type: "select",
  254. items: db.countries,
  255. valueField: "Id",
  256. textField: "Name"
  257. }, {
  258. name: "Married",
  259. type: "checkbox",
  260. title: "Is Married"
  261. }]
  262. });
  263. (0, _jquery.default)("#pager").on("change", function () {
  264. var page = parseInt((0, _jquery.default)(this).val(), 10);
  265. (0, _jquery.default)("#exampleLoadingByPage").jsGrid("openPage", page);
  266. });
  267. })(); // Example Custom View
  268. // -------------------
  269. (function () {
  270. (0, _jquery.default)('#exampleCustomView').jsGrid({
  271. height: "500px",
  272. width: "100%",
  273. filtering: true,
  274. editing: true,
  275. sorting: true,
  276. paging: true,
  277. autoload: true,
  278. pageSize: 15,
  279. pageButtonCount: 5,
  280. controller: db,
  281. fields: [{
  282. name: "Name",
  283. type: "text",
  284. width: 150
  285. }, {
  286. name: "Age",
  287. type: "number",
  288. width: 70
  289. }, {
  290. name: "Address",
  291. type: "text",
  292. width: 200
  293. }, {
  294. name: "Country",
  295. type: "select",
  296. items: db.countries,
  297. valueField: "Id",
  298. textField: "Name"
  299. }, {
  300. name: "Married",
  301. type: "checkbox",
  302. title: "Is Married",
  303. sorting: false
  304. }, {
  305. type: "control",
  306. modeSwitchButton: false,
  307. editButton: false
  308. }]
  309. });
  310. (0, _jquery.default)(".views").on("change", function () {
  311. var $cb = (0, _jquery.default)(this);
  312. (0, _jquery.default)("#exampleCustomView").jsGrid("option", $cb.attr("value"), $cb.is(":checked"));
  313. });
  314. })(); // Example Custom Row Renderer
  315. // ---------------------------
  316. (function () {
  317. (0, _jquery.default)('#exampleCustomRowRenderer').jsGrid({
  318. height: "500px",
  319. width: "100%",
  320. autoload: true,
  321. paging: true,
  322. controller: {
  323. loadData: function loadData() {
  324. var deferred = _jquery.default.Deferred();
  325. _jquery.default.ajax({
  326. url: 'http://api.randomuser.me/?results=40',
  327. dataType: 'json',
  328. success: function success(data) {
  329. deferred.resolve(data.results);
  330. }
  331. });
  332. return deferred.promise();
  333. }
  334. },
  335. rowRenderer: function rowRenderer(item) {
  336. var $photo = (0, _jquery.default)("<div>").addClass("pr-20").append((0, _jquery.default)('<a>').addClass('avatar avatar-lg').attr('href', 'javascript:void(0)').append((0, _jquery.default)("<img>").attr("src", item.picture.medium)));
  337. var $info = (0, _jquery.default)("<div>").addClass("media-body").append((0, _jquery.default)("<p>").append((0, _jquery.default)("<strong>").text(item.name.first.capitalize() + " " + item.name.last.capitalize()))).append((0, _jquery.default)("<p>").text("Location: " + item.location.city.capitalize() + ", " + item.location.street)).append((0, _jquery.default)("<p>").text("Email: " + item.email)).append((0, _jquery.default)("<p>").text("Phone: " + item.phone)).append((0, _jquery.default)("<p>").text("Cell: " + item.cell));
  338. return (0, _jquery.default)("<tr>").append((0, _jquery.default)('<td>').append((0, _jquery.default)('<div class="media">').append($photo).append($info)));
  339. },
  340. fields: [{
  341. title: "Clients"
  342. }]
  343. });
  344. String.prototype.capitalize = function () {
  345. return this.charAt(0).toUpperCase() + this.slice(1);
  346. };
  347. })(); // Example Batch Delete
  348. // --------------------
  349. (function () {
  350. (0, _jquery.default)('#exampleBatchDelete').jsGrid({
  351. height: "500px",
  352. width: "100%",
  353. autoload: true,
  354. confirmDeleting: false,
  355. paging: true,
  356. controller: {
  357. loadData: function loadData() {
  358. return db.clients;
  359. }
  360. },
  361. fields: [{
  362. headerTemplate: function headerTemplate() {
  363. return (0, _jquery.default)("<button>").attr("type", "button").attr("class", "btn btn-primary btn-xs").text("Delete").on("click", function () {
  364. deleteSelectedItems();
  365. });
  366. },
  367. itemTemplate: function itemTemplate(_, item) {
  368. return (0, _jquery.default)("<input>").attr("type", "checkbox").on("change", function () {
  369. (0, _jquery.default)(this).is(":checked") ? selectItem(item) : unselectItem(item);
  370. });
  371. },
  372. align: "center",
  373. width: 50
  374. }, {
  375. name: "Name",
  376. type: "text",
  377. width: 150
  378. }, {
  379. name: "Age",
  380. type: "number",
  381. width: 50
  382. }, {
  383. name: "Address",
  384. type: "text",
  385. width: 200
  386. }]
  387. });
  388. var selectedItems = [];
  389. var selectItem = function selectItem(item) {
  390. selectedItems.push(item);
  391. };
  392. var unselectItem = function unselectItem(item) {
  393. selectedItems = _jquery.default.grep(selectedItems, function (i) {
  394. return i !== item;
  395. });
  396. };
  397. var deleteSelectedItems = function deleteSelectedItems() {
  398. if (!selectedItems.length || !confirm("Are you sure?")) return;
  399. var $grid = (0, _jquery.default)("#exampleBatchDelete");
  400. _jquery.default.each(selectedItems, function (_, item) {
  401. $grid.jsGrid("deleteItem", item);
  402. });
  403. selectedItems = [];
  404. };
  405. })(); // Example Rows Reordering
  406. // -----------------------
  407. (function () {
  408. (0, _jquery.default)('#exampleRowsReordering').jsGrid({
  409. height: "500px",
  410. width: "100%",
  411. autoload: true,
  412. rowClass: function rowClass(item, itemIndex) {
  413. return "client-" + itemIndex;
  414. },
  415. controller: {
  416. loadData: function loadData() {
  417. return db.clients.slice(0, 15);
  418. }
  419. },
  420. fields: [{
  421. name: "Name",
  422. type: "text",
  423. width: 150
  424. }, {
  425. name: "Age",
  426. type: "number",
  427. width: 50
  428. }, {
  429. name: "Address",
  430. type: "text",
  431. width: 200
  432. }, {
  433. name: "Country",
  434. type: "select",
  435. items: db.countries,
  436. valueField: "Id",
  437. textField: "Name"
  438. }, {
  439. name: "Married",
  440. type: "checkbox",
  441. title: "Is Married",
  442. sorting: false
  443. }]
  444. });
  445. var $gridData = (0, _jquery.default)("#exampleRowsReordering .jsgrid-grid-body tbody");
  446. $gridData.sortable({
  447. update: function update(e, ui) {
  448. // array of indexes
  449. var clientIndexRegExp = /\s+client-(\d+)\s+/;
  450. var indexes = _jquery.default.map($gridData.sortable("toArray", {
  451. attribute: "class"
  452. }), function (classes) {
  453. return clientIndexRegExp.exec(classes)[1];
  454. });
  455. alert("Reordered indexes: " + indexes.join(", ")); // arrays of items
  456. var items = _jquery.default.map($gridData.find("tr"), function (row) {
  457. return (0, _jquery.default)(row).data("JSGridItem");
  458. });
  459. console && console.log("Reordered items", items);
  460. }
  461. });
  462. })(); // Example Custom Grid Field
  463. // -------------------------
  464. (function () {
  465. var MyDateField = function MyDateField(config) {
  466. jsGrid.Field.call(this, config);
  467. };
  468. MyDateField.prototype = new jsGrid.Field({
  469. sorter: function sorter(date1, date2) {
  470. return new Date(date1) - new Date(date2);
  471. },
  472. itemTemplate: function itemTemplate(value) {
  473. return new Date(value).toDateString();
  474. },
  475. insertTemplate: function insertTemplate() {
  476. if (!this.inserting) return "";
  477. var $result = this.insertControl = this._createTextBox();
  478. return $result;
  479. },
  480. editTemplate: function editTemplate(value) {
  481. if (!this.editing) return this.itemTemplate(value);
  482. var $result = this.editControl = this._createTextBox();
  483. $result.val(value);
  484. return $result;
  485. },
  486. insertValue: function insertValue() {
  487. return this.insertControl.datepicker("getDate");
  488. },
  489. editValue: function editValue() {
  490. return this.editControl.datepicker("getDate");
  491. },
  492. _createTextBox: function _createTextBox() {
  493. return (0, _jquery.default)("<input>").attr("type", "text").addClass('form-control input-sm').datepicker({
  494. autoclose: true
  495. });
  496. }
  497. });
  498. jsGrid.fields.myDateField = MyDateField;
  499. (0, _jquery.default)("#exampleCustomGridField").jsGrid({
  500. height: "500px",
  501. width: "100%",
  502. inserting: true,
  503. editing: true,
  504. sorting: true,
  505. paging: true,
  506. data: db.users,
  507. fields: [{
  508. name: "Account",
  509. width: 150,
  510. align: "center"
  511. }, {
  512. name: "Name",
  513. type: "text"
  514. }, {
  515. name: "RegisterDate",
  516. type: "myDateField",
  517. width: 100,
  518. align: "center"
  519. }, {
  520. type: "control",
  521. editButton: false,
  522. modeSwitchButton: false
  523. }]
  524. });
  525. })();
  526. });