项目作者: lucrod1

项目描述 :
Datagrid component that supports several types of value editing. It is possible to set up columns of type, checkbox, href, html content, combobox, chosen, multi-chosen, input fields with mask (string, number, money, br-phone, br-cpf, br- cnpj) and unmasked.
高级语言: CSS
项目地址: git://github.com/lucrod1/angular-rs-datagrid.git
创建时间: 2016-12-12T16:36:56Z
项目社区:https://github.com/lucrod1/angular-rs-datagrid

开源协议:

下载


Directive angular-rs-datagrid

Directive is under development, version beta

Installation

With Bower:

  1. bower install --save angular-rs-datagrid

With npm:

  1. npm install --save angular-rs-datagrid

Datagrid component that supports several types of value editing. It is possible to set up columns of type, checkbox, href, html content, combobox, chosen, multi-chosen, popover, input fields with mask (string, number, money, br-phone, br-cpf, br- cnpj) and unmasked.

In this version this only implemented pagination in the backend.

This component works with two dependencies, angular-input-mask and angular-ui-select

How to use

include module into app

  1. rs.datagrid

HTML template

  1. <rs-datagrid config="config"></rs-datagrid>

Defines property config in controller

  1. $scope.config =
  2. {
  3. "classTable": "table table-bordered table-striped", // optional, default: "table table-bordered table-striped" --->
  4. "messageLoading": "Loading...", // optional, default: "loading..."
  5. "messageEmpty" : "No results",
  6. "sumLabel" : "Total: ", // optional, default: "Total: "
  7. "sort": true, // optional, default: false
  8. "defaultSort": "id,asc", // optional, default is first "collumn.index", asc
  9. "classRow": "disabled" || function(row){}, // optional, class on <tr> is possible used calback function(row)
  10. "collumns": [{ // required
  11. "title": "ID", // title of collumn
  12. "index": "id", // Property that will print in the column
  13. "class": "text-center" || function(row){}, // optional, class od <th> and <td> is Possible used calback function(row)
  14. "style": { // optional
  15. "width": "60px" // It is possible to define some properties in html
  16. }
  17. }],
  18. "data": function(){
  19. return [{
  20. "id": 1,
  21. "name": "Lucas Rodrigues"
  22. }];
  23. }

Defines a property search inside property config

  1. $scope.config =
  2. {
  3. ...
  4. "search": {
  5. "label": "Search: " //optional
  6. }
  7. ...
  8. }

How configure columns show visibles

Defines a property search inside property config

  1. $scope.config =
  2. {
  3. ...
  4. "setupColumns": {
  5. "id": "name_of_table_with_cod_user_logged" //id tables with id user for persist in local storage
  6. }
  7. ...
  8. }

How to set a value in the search field

Use the auxiliary method “_setSearch” specifying the value

  1. $scope.config._setSearch(yourValue)

How listener click in row

Defines onClickRow callback function

  1. $scope.config =
  2. {
  3. ...
  4. "collumns:"[],
  5. "onClickRow": function(row){
  6. }
  7. ...
  8. }

How listener click in cell

Atention: You can not use onClickCell along with onClickRow, choose which one is best for you.

Defines onClickCell callback function

  1. $scope.config =
  2. {
  3. ...
  4. "collumns:"[],
  5. "onClickCell": function(row, index){
  6. }
  7. ...
  8. }

How override output collumn

Use the function render to override the output

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [{ // required
  5. "title": "Render", // title of collumn
  6. "index": "name", // Property that will print in the column
  7. "render": function(row){ // callback for override the output
  8. return row.id+' - '+row.name;
  9. },
  10. }]
  11. ...
  12. }

Is possible render html content, for this defines property isHtml

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [{ // required
  5. "title": "Render", // title of collumn
  6. "index": "name", // Property that will print in the column
  7. "isHtml": true,
  8. "render": function(row){ // callback for override the output
  9. return "<b>"+row.id+' - '+row.name+"</b>";
  10. },
  11. }]
  12. ...
  13. }

How disable sort in collumn

Defines sort = false in collumn property

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [
  5. {
  6. "title": "No Sort",
  7. "index": "lastName",
  8. "sort": false
  9. }]
  10. ...
  11. }

How Stylize collumn

Defines class in collumn

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [
  5. {
  6. "title": "Class",
  7. "index": "city",
  8. "class": "text-center"
  9. }]
  10. ...
  11. }

How render checkbox in collumn

Defines a property type = ‘checkbox’ in collumn.action.type

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [
  5. {
  6. "title": "", // title of collumn
  7. "index": "enable", // Property that will print in the column
  8. "class": "text-center", // optional, class od <th> and <td>
  9. "sort": false, // optional default: true
  10. "action": {
  11. "type": "checkbox", //required
  12. "checkInHeader": true, // Boolean, for render checkbox in header <th>
  13. isDisabled: function(row){ // callback
  14. if (expression){
  15. return true;
  16. }else{
  17. return false;
  18. }
  19. },
  20. "onCheckHeader": function(checked) { // callback when clicked in checkHeader
  21. console.log('checked: ' + checked);
  22. },
  23. "onCheck": function(obj, checked) { // callback when clicked in check row table
  24. console.log('checked: ' + checked + ', obj:' + obj.id);
  25. }
  26. }],
  27. ...
  28. }

How render href in collumn

Defines a property type = ‘href’ in collumn.action.type

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [{
  5. "title": "Href",
  6. "index": "github",
  7. "render": function(obj) { // in action href, this property is required, callback(currentRow) override collumn, for label the href
  8. return "open github";
  9. },
  10. "action": {
  11. "type": "href",
  12. "onClick: function(obj) { // callback(currentRow) when click in href
  13. window.open(row.github, "_blank");
  14. }
  15. }
  16. }],
  17. ...
  18. }

How render input in collumn

Defines a property type = ‘input’ in collumn.action.type

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [{
  5. "title": "Input",
  6. "index": "nickName",
  7. "action": {
  8. "type": "input",
  9. "class": "input-rs", // optional
  10. "style": { // optional
  11. "width": "100px"
  12. },
  13. "maxlength": 10, // optional
  14. "trigger": "blur", // required, default: 'blur', avaliables Triggers 'blur', 'change'
  15. "isDisabled": function(obj) { // callback
  16. if (obj.id === 1) {
  17. return true;
  18. } else {
  19. return false;
  20. }
  21. },
  22. "onChange": function(row) { // callback when exec trigger
  23. console.log('Row actual: '+row);
  24. }
  25. }
  26. }],
  27. ...
  28. }

How render input with mask ‘Number’ in collumn

Defines a property type = ‘input’ and mask in collumn.action

  1. $scope.config =
  2. {
  3. ...
  4. "collumns": [{
  5. "title": "Input",
  6. "index": "nickName",
  7. "action": {
  8. "type": "input",
  9. "trigger": "blur", // required, default: 'blur', avaliables Triggers 'blur', 'change'
  10. "mask": {
  11. "use": "number", // avaliables uses: 'number', 'money', 'br-phone','br-cep','br-cpf','br-cpfcnpj'
  12. "decimalPlace": 2, // number of decimals
  13. "maxlength": 11,
  14. "negative": true // optional, default false
  15. },
  16. "isDisabled": function(obj) { // callback
  17. if (obj.id === 1) {
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. },
  23. "onChange": function(row) { // callback when exec trigger
  24. console.log("Row actual: "+row);
  25. }
  26. }
  27. }],
  28. ...
  29. }

Mask Money

  1. $scope.config =
  2. {
  3. ...
  4. mask: {
  5. use: 'money',
  6. decimalPlace: 2, // number of decimals
  7. maxlength: 11
  8. },
  9. ...
  10. }

Mask br-phone

  1. $scope.config =
  2. {
  3. ...
  4. mask: {
  5. use: 'br-phone'
  6. },
  7. ...
  8. }

Mask br-cep

  1. $scope.config =
  2. {
  3. ...
  4. mask: {
  5. use: 'br-cep'
  6. },
  7. ...
  8. }

Mask br-cpf

Callback has two parameters, row and isValid, isValid is result validation cpf

  1. $scope.config =
  2. {
  3. ...
  4. mask: {
  5. use: 'br-cpf'
  6. },
  7. onChange: function(row, isValid) { //callback
  8. if(isValid){
  9. console.log('execute action here');
  10. }else{
  11. console.log('CPF inválido');
  12. }
  13. }
  14. ...
  15. }

Mask br-cnpj

Callback has two parameters, row and isValid, isValid is result validation cnpj

  1. $scope.config =
  2. {
  3. ...
  4. mask: {
  5. use: 'br-cnpj'
  6. },
  7. onChange: function(row, isValid) { //callback
  8. if(isValid){
  9. console.log('execute action here');
  10. }else{
  11. console.log('CNPJ inválido');
  12. }
  13. }
  14. ...
  15. }

Mask br-cpfcnpj

Callback has two parameters, row and isValid, isValid is result validation

  1. $scope.config =
  2. {
  3. ...
  4. mask: {
  5. use: 'br-cpfcnpj'
  6. },
  7. onChange: function(row, isValid) { //callback
  8. if(isValid){
  9. console.log('execute action here');
  10. }else{
  11. console.log('Campo inválido');
  12. }
  13. }
  14. ...
  15. }

How render comboBox in collumn

Defines a property type = ‘combo’ and mask in collumn.action

  1. $scope.config =
  2. {
  3. ...
  4. collumns: [{
  5. title: "Combo",
  6. index: 'status',
  7. action: {
  8. type: 'combo',
  9. class: '', // optional
  10. style: { // optional
  11. width: '100px'
  12. },
  13. avaliablesChoises: ["ACTIVE","INACTIVE"], // required, Collection for populate combo, not use array of object for this use "chosen
  14. labelChoose: 'Select...', // optional, if defined, create a empty option
  15. isDisabled: function(obj) { // optional, callback for disable the combo
  16. if (obj.id === 1) {
  17. return true;
  18. } else {
  19. return false;
  20. }
  21. },
  22. onChange: function(obj) {
  23. console.log('execute action here');
  24. }
  25. }
  26. }],
  27. ...
  28. }

How render chosen in collumn

Defines a property type = ‘chosen’ and mask in collumn.action

  1. $scope.config =
  2. {
  3. ...
  4. collumns: [{
  5. title: 'Chosen',
  6. index: 'tag',
  7. action: {
  8. type: 'chosen',
  9. placeholder: 'Selecione um tag...',
  10. class: '', // optional
  11. style: { // optional
  12. width: '100px'
  13. },
  14. theme: 'select2',
  15. searchIn: ['id','nome'], // property the object for search
  16. allowClear: false, // [x] button clear - default is false
  17. selectedRender: function(item){ // optional
  18. return item.nome;
  19. },
  20. itemRender: function(item){
  21. var ret = '<small>';
  22. ret += 'id:'+item.id+'<br/>';
  23. ret += 'nome: '+item.nome+'<br/>';
  24. ret += '</small>';
  25. return ret; // is possible return html content
  26. },
  27. isDisabled: function(obj){
  28. if (obj.id == 1){
  29. return true;
  30. }
  31. return false;
  32. },
  33. avaliablesChoises: [{
  34. id: 1,
  35. nome: 'tag 1'
  36. },{
  37. id: 2,
  38. nome: 'tag 2'
  39. }],
  40. onChange: function(obj, newValue) {
  41. console.log('execute action here: ' + obj.id);
  42. }
  43. }],
  44. ...
  45. }

How render multiChosen in collumn

Defines a property type = ‘multiChosen’ and mask in collumn.action

  1. $scope.config =
  2. {
  3. ...
  4. collumns: [{
  5. title: 'Multi-Chosen',
  6. index: 'nome',
  7. action: {
  8. type: 'multiChosen',
  9. placeholder: 'Selecione um tag...',
  10. theme: 'select2',
  11. searchIn: ['id','nome'],
  12. selectedsRender: function(item){
  13. return item.nome;
  14. },
  15. itemRender: function(item){
  16. return item.nome;
  17. },
  18. onRemove : function(row, item, model){
  19. console.log(item);
  20. },
  21. isDisabled: function(obj){
  22. if (obj.id == 1){
  23. return true;
  24. }
  25. return false;
  26. },
  27. avaliablesChoises: [{
  28. id: 1,
  29. nome: 'tag 1'
  30. },{
  31. id: 2,
  32. nome: 'tag 2'
  33. },{
  34. id: 3,
  35. nome: 'tag 3'
  36. },{
  37. id: 4,
  38. nome: 'tag 4'
  39. },{
  40. id: 5,
  41. nome: 'tag 5'
  42. },{
  43. id: 6,
  44. nome: 'tag 6'
  45. }],
  46. onSelect: function(row, item, model) {
  47. console.log('execute action here: ' + item.id);
  48. }
  49. }],
  50. ...
  51. }

How render buttons

The buttons are always rendered in the last column

Defines a property buttons in config

  1. $scope.config =
  2. {
  3. ...
  4. collumns: [{...}],
  5. buttons: [
  6. {
  7. text: 'Excluir', //optional
  8. classButton: 'btn btn-xs btn-primary', // class ex: <button class="btn btn-xs btn-primary"></button>
  9. onClick: function(obj) { // callback when clicked
  10. console.log('Execute action here obj clicked:' + obj.id);
  11. }
  12. },
  13. {
  14. text: function(row){ //callback for condition text
  15. return row.action.name;
  16. }
  17. ...
  18. },
  19. {
  20. tooltip: function(row){ //callback for condition text tooltip
  21. return row.action.name;
  22. }
  23. ...
  24. },
  25. {
  26. tooltip: 'Tooltip', //optional
  27. classIcon: 'glyphicon glyphicon-remove', // class for icon in button
  28. classButton: 'btn btn-xs btn-danger', // class ex: <button class="btn btn-xs btn-danger"></button>
  29. isVisible: function(obj) { // callback for handle when the button will be rendered according a boolean expression
  30. if (obj.id == 1) {
  31. return false;
  32. } else {
  33. return true;
  34. }
  35. },
  36. isDisabled: function(obj) { // callback for handle when the button will be disabled according a boolean expression
  37. if (obj.id == 1) {
  38. return false;
  39. } else {
  40. return true;
  41. }
  42. },
  43. }
  44. ]
  45. ...
  46. }

Render popover for Row

Defines a property popoverRow, trigger is hover in row

  1. $scope.config =
  2. {
  3. ...
  4. popoverRow: { // optional
  5. titleRender: function (row){ // optional, callback(currentRow) for render title in popover
  6. return row.name+' '+row.lastName;
  7. },
  8. templateUrl: 'template-popover.html', // required type: String "popover-template.html"
  9. ngModel: 'popover', // required type: String (that presents the model in popover template)
  10. getModel: function(row){ // Callback must return an object, this object will be sent to the popover template, if it is not defined a callback will be sent the object of row
  11. return row;
  12. }
  13. },
  14. ...
  15. }

template-popover.html

  1. <div>
  2. <p>Nick Name: `{{this.popover.nickName}}</p>
  3. <p>City: `{{this.popover.city.name}}`</p>
  4. </div>

How force refresh datagrid

Call broadcast

  1. $scope.$broadcast("rsDatagrid:refresh");

How use pagination in component

This version only implements pagination in backend

Defines property pagination in config

  1. $scope.config =
  2. {
  3. ...
  4. pagination: { // optional
  5. labelSize: 'Registros por página: ', // optional, default "Page size: ""
  6. defaultSize: 10, // optional, default first item in avaliableSizes
  7. avaliableSizes: [10, 25, 50, 100, 500], // optional, default [10,25,50,100]
  8. positionBottom: true // optional, default is true, position over top
  9. },
  10. ...
  11. }

When using paging, you must implement a lazyData function within the configuration, this function is responsive to update or components automatically, page sorts and other

  1. $scope.config =
  2. {
  3. ...
  4. lazyData: function(page, size, sort, search) {
  5. var params = {
  6. page: page,
  7. size: size,
  8. sort: sort,
  9. search: search
  10. };
  11. return $http({
  12. url: "http://localhost:8080/acoes",
  13. method: 'GET',
  14. params: params,
  15. }).then(function(result) {
  16. return result.data;
  17. });
  18. },
  19. ...
  20. };

Then JSON RESPONSE for pagination in back-end, this example the implementation in spring data.

  1. {
  2. "content": [{
  3. ...
  4. ...
  5. }],
  6. "totalElements": 10,
  7. "last": false,
  8. "totalPages": 1,
  9. "first": true,
  10. "sort": [{
  11. "direction": "ASC",
  12. "property": "id"
  13. }],
  14. "numberOfElements": 10,
  15. "size": 10,
  16. "number": 0
  17. }

How to set the current page

Use the auxiliary method “_setCurrentPage” specifying the value

  1. $scope.config._setCurrentPage(yourValue)

License

MIT