diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e33deac..fb4c5429 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,3 +40,6 @@ ## v0.1.3 * reset the selectionAll state on page change + +## v0.1.4 +* fix sync issue with the content of an item and its smart-table row diff --git a/Smart-Table.debug.js b/Smart-Table.debug.js index 91d34422..b1c69960 100644 --- a/Smart-Table.debug.js +++ b/Smart-Table.debug.js @@ -50,7 +50,6 @@ })(window, angular); - /* Directives */ (function (angular) { "use strict"; @@ -199,16 +198,21 @@ link: function (scope, element) { var column = scope.column, + isSimpleCell = !column.isEditable, row = scope.dataRow, format = filter('format'), getter = parse(column.map), childScope; //can be useful for child directives - scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter); + scope.$watch('dataRow', function (value) { + scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter); + if (isSimpleCell === true) { + element.text(scope.formatedValue); + } + }, true); function defaultContent() { - //clear content if (column.isEditable) { element.html('
'); compile(element.contents())(scope); @@ -223,6 +227,8 @@ //we have to load the template (and cache it) : a kind of ngInclude http.get(value, {cache: templateCache}).success(function (response) { + isSimpleCell = false; + //create a scope childScope = scope.$new(); //compile the element with its new content and new scope @@ -268,7 +274,9 @@ //init values scope.isEditMode = false; - scope.value = getter(scope.row); + scope.$watch('row', function () { + scope.value = getter(scope.row); + }, true); scope.submit = function () { @@ -285,8 +293,8 @@ scope.isEditMode = scope.isEditMode !== true; }; - scope.$watch('isEditMode', function (newValue, oldValue) { - if (newValue) { + scope.$watch('isEditMode', function (newValue) { + if (newValue === true) { input[0].select(); input[0].focus(); } @@ -604,87 +612,86 @@ })(angular); - angular.module('smartTable.templates', ['partials/defaultCell.html', 'partials/defaultHeader.html', 'partials/editableCell.html', 'partials/globalSearchCell.html', 'partials/pagination.html', 'partials/selectAllCheckbox.html', 'partials/selectionCheckbox.html', 'partials/smartTable.html']); -angular.module("partials/defaultCell.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/defaultCell.html", - "{{formatedValue}}"); +angular.module("partials/defaultCell.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/defaultCell.html", + "{{formatedValue}}"); }]); -angular.module("partials/defaultHeader.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/defaultHeader.html", - "{{column.label}}"); +angular.module("partials/defaultHeader.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/defaultHeader.html", + "{{column.label}}"); }]); -angular.module("partials/editableCell.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/editableCell.html", - "
\n" + - " {{value | format:column.formatFunction:column.formatParameter}}\n" + - "\n" + - "
\n" + - " \n" + - "
\n" + - "
"); +angular.module("partials/editableCell.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/editableCell.html", + "
\n" + + " {{value | format:column.formatFunction:column.formatParameter}}\n" + + "\n" + + "
\n" + + " \n" + + "
\n" + + "
"); }]); -angular.module("partials/globalSearchCell.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/globalSearchCell.html", - "\n" + - ""); +angular.module("partials/globalSearchCell.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/globalSearchCell.html", + "\n" + + ""); }]); -angular.module("partials/pagination.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/pagination.html", - "
\n" + - " \n" + - "
"); +angular.module("partials/pagination.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/pagination.html", + "
\n" + + " \n" + + "
"); }]); -angular.module("partials/selectAllCheckbox.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/selectAllCheckbox.html", - ""); +angular.module("partials/selectAllCheckbox.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/selectAllCheckbox.html", + ""); }]); -angular.module("partials/selectionCheckbox.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/selectionCheckbox.html", - ""); +angular.module("partials/selectionCheckbox.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/selectionCheckbox.html", + ""); }]); -angular.module("partials/smartTable.html", []).run(["$templateCache", function($templateCache) { - $templateCache.put("partials/smartTable.html", - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "
\n" + - "
\n" + - "
\n" + - "\n" + - "\n" + - ""); +angular.module("partials/smartTable.html", []).run(["$templateCache", function ($templateCache) { + $templateCache.put("partials/smartTable.html", + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
\n" + + "
\n" + + "
\n" + + "\n" + + "\n" + + ""); }]); (function (angular) { @@ -726,7 +733,7 @@ angular.module("partials/smartTable.html", []).run(["$templateCache", function($ * @param index * @param item */ - insertAt = function (arrayRef, index, item) { + insertAt = function (arrayRef, index, item) { if (index >= 0 && index < arrayRef.length) { arrayRef.splice(index, 0, item); } else { @@ -740,7 +747,7 @@ angular.module("partials/smartTable.html", []).run(["$templateCache", function($ * @param oldIndex * @param newIndex */ - moveAt = function (arrayRef, oldIndex, newIndex) { + moveAt = function (arrayRef, oldIndex, newIndex) { var elementToMove; if (oldIndex >= 0 && oldIndex < arrayRef.length && newIndex >= 0 && newIndex < arrayRef.length) { elementToMove = arrayRef.splice(oldIndex, 1)[0]; @@ -756,7 +763,7 @@ angular.module("partials/smartTable.html", []).run(["$templateCache", function($ * @param reverse * @returns {*} */ - sort = function (arrayRef, sortAlgorithm, predicate, reverse) { + sort = function (arrayRef, sortAlgorithm, predicate, reverse) { if (!sortAlgorithm || !angular.isFunction(sortAlgorithm)) { return arrayRef; @@ -772,7 +779,7 @@ angular.module("partials/smartTable.html", []).run(["$templateCache", function($ * @param predicate * @returns {*} */ - filter = function (arrayRef, filterAlgorithm, predicate) { + filter = function (arrayRef, filterAlgorithm, predicate) { if (!filterAlgorithm || !angular.isFunction(filterAlgorithm)) { return arrayRef; } else { @@ -787,7 +794,7 @@ angular.module("partials/smartTable.html", []).run(["$templateCache", function($ * @param length * @returns {*} */ - fromTo = function (arrayRef, min, length) { + fromTo = function (arrayRef, min, length) { var out = [], limit, @@ -822,7 +829,6 @@ angular.module("partials/smartTable.html", []).run(["$templateCache", function($ })(angular); - (function (angular) { angular.module('ui.bootstrap.pagination.smartTable', ['smartTable.templateUrlList']) diff --git a/Smart-Table.min.js b/Smart-Table.min.js index e79f681b..1759b51d 100644 --- a/Smart-Table.min.js +++ b/Smart-Table.min.js @@ -1 +1 @@ -!function(a,b){"use strict";function c(a,c){function d(a){return this instanceof d?(b.extend(this,a),void 0):new d(a)}this.setDefaultOption=function(a){b.extend(d.prototype,a)},a.headerTemplateUrl=c.defaultHeader,this.setDefaultOption(a),this.$get=function(){return d}}var d=b.module("smartTable.column",["smartTable.templateUrlList"]).constant("DefaultColumnConfiguration",{isSortable:!0,isEditable:!1,type:"text",headerTemplateUrl:"",map:"",label:"",sortPredicate:"",formatFunction:"",formatParameter:"",filterPredicate:"",cellTemplateUrl:"",headerClass:"",cellClass:""});c.$inject=["DefaultColumnConfiguration","templateUrlList"],d.provider("Column",c),a.ColumnProvider=c}(window,angular),function(a){"use strict";a.module("smartTable.directives",["smartTable.templateUrlList","smartTable.templates"]).directive("smartTable",["templateUrlList","DefaultTableConfiguration",function(b,c){return{restrict:"EA",scope:{columnCollection:"=columns",dataCollection:"=rows",config:"="},replace:"true",templateUrl:b.smartTable,controller:"TableCtrl",link:function(d,e,f,g){var h;d.$watch("config",function(e){var f=a.extend({},c,e),h=void 0!==d.columns?d.columns.length:0;if(g.setGlobalConfig(f),"multiple"!==f.selectionMode||f.displaySelectionCheckbox!==!0)for(var i=h-1;i>=0;i--)d.columns[i].isSelectionColumn===!0&&g.removeColumn(i);else g.insertColumn({cellTemplateUrl:b.selectionCheckbox,headerTemplateUrl:b.selectAllCheckbox,isSelectionColumn:!0},0)},!0),d.$watch("columnCollection",function(){if(d.columnCollection)for(var b=0,c=d.columnCollection.length;c>b;b++)g.insertColumn(d.columnCollection[b]);else d.dataCollection&&d.dataCollection.length>0&&(h=d.dataCollection[0],a.forEach(h,function(a,b){"$"!=b[0]&&g.insertColumn({label:b,map:b})}))},!0),d.$watch("dataCollection.length",function(a,b){a!==b&&g.sortBy()})}}}]).directive("smartTableDataRow",function(){return{require:"^smartTable",restrict:"C",link:function(a,b,c,d){b.bind("click",function(){a.$apply(function(){d.toggleSelection(a.dataRow)})})}}}).directive("smartTableHeaderCell",function(){return{restrict:"C",require:"^smartTable",link:function(a,b,c,d){b.bind("click",function(){a.$apply(function(){d.sortBy(a.column)})})}}}).directive("smartTableSelectAll",function(){return{restrict:"C",require:"^smartTable",link:function(a,b,c,d){b.bind("click",function(){d.toggleSelectionAll(b[0].checked===!0)})}}}).directive("stopEvent",function(){return{restrict:"A",link:function(a,b,c){b.bind(c.stopEvent,function(a){a.stopPropagation()})}}}).directive("smartTableGlobalSearch",["templateUrlList",function(a){return{restrict:"C",require:"^smartTable",scope:{columnSpan:"@"},templateUrl:a.smartTableGlobalSearch,replace:!1,link:function(a,b,c,d){a.searchValue="",a.$watch("searchValue",function(a){d.search(a)})}}}]).directive("smartTableDataCell",["$filter","$http","$templateCache","$compile","$parse",function(a,b,c,d,e){return{restrict:"C",link:function(f,g){function h(){j.isEditable?(g.html('
'),d(g.contents())(f)):g.text(f.formatedValue)}var i,j=f.column,k=f.dataRow,l=a("format"),m=e(j.map);f.formatedValue=l(m(k),j.formatFunction,j.formatParameter),f.$watch("column.cellTemplateUrl",function(a){a?b.get(a,{cache:c}).success(function(a){i=f.$new(),g.html(a),d(g.contents())(i)}).error(h):h()})}}}]).directive("inputType",function(){return{restrict:"A",priority:1,link:function(a,b,c){var d=a.$eval(c.type);c.$set("type",d)}}}).directive("editableCell",["templateUrlList","$parse",function(b,c){return{restrict:"EA",require:"^smartTable",templateUrl:b.editableCell,scope:{row:"=",column:"=",type:"="},replace:!0,link:function(b,d,e,f){var g=a.element(d.children()[1]),h=a.element(g.children()[0]),i=c(b.column.map);b.isEditMode=!1,b.value=i(b.row),b.submit=function(){b.myForm.$valid===!0&&(f.updateDataRow(b.row,b.column.map,b.value),f.sortBy()),b.toggleEditMode()},b.toggleEditMode=function(){b.value=i(b.row),b.isEditMode=b.isEditMode!==!0},b.$watch("isEditMode",function(a){a&&(h[0].select(),h[0].focus())}),h.bind("blur",function(){b.$apply(function(){b.submit()})})}}}])}(angular),function(a){"use strict";a.module("smartTable.filters",[]).constant("DefaultFilters",["currency","date","json","lowercase","number","uppercase"]).filter("format",["$filter","DefaultFilters",function(b,c){return function(d,e,f){var g;return g=e&&a.isFunction(e)?e:-1!==c.indexOf(e)?b(e):function(a){return a},g(d,f)}}])}(angular),function(a){"use strict";a.module("smartTable.table",["smartTable.column","smartTable.utilities","smartTable.directives","smartTable.filters","ui.bootstrap.pagination.smartTable"]).constant("DefaultTableConfiguration",{selectionMode:"none",isGlobalSearchActivated:!1,displaySelectionCheckbox:!1,isPaginationEnabled:!0,itemsByPage:10,maxSize:5,sortAlgorithm:"",filterAlgorithm:""}).controller("TableCtrl",["$scope","Column","$filter","$parse","ArrayUtility","DefaultTableConfiguration",function(b,c,d,e,f,g){function h(){var a,c=b.displayedCollection.length;for(a=0;c>a;a++)if(b.displayedCollection[a].isSelected!==!0)return!1;return!0}function i(c){return a.isArray(c)?0===c.length||b.itemsByPage<1?1:Math.ceil(c.length/b.itemsByPage):1}function j(c,e){var g=(b.sortAlgorithm&&a.isFunction(b.sortAlgorithm))===!0?b.sortAlgorithm:d("orderBy");return e?f.sort(c,g,e.sortPredicate,e.reverse):c}function k(c,d,e,f){var g,i;if(a.isArray(c)&&("multiple"===d||"single"===d)&&e>=0&&ej;j++)i=c[j].isSelected,c[j].isSelected=!1,i===!0&&b.$emit("selectionChange",{item:c[j]});g.isSelected=f,b.holder.isAllSelected=h(),b.$emit("selectionChange",{item:g})}}b.columns=[],b.dataCollection=b.dataCollection||[],b.displayedCollection=[],b.numberOfPages=i(b.dataCollection),b.currentPage=1,b.holder={isAllSelected:!1};var l,m={};this.setGlobalConfig=function(c){a.extend(b,g,c)},this.changePage=function(c){var d=b.currentPage;a.isNumber(c.page)&&(b.currentPage=c.page,b.displayedCollection=this.pipe(b.dataCollection),b.holder.isAllSelected=h(),b.$emit("changePage",{oldValue:d,newValue:b.currentPage}))},this.sortBy=function(a){var c=b.columns.indexOf(a);-1!==c&&a.isSortable===!0&&(l&&l!==a&&(l.reverse="none"),a.sortPredicate=a.sortPredicate||a.map,a.reverse=a.reverse!==!0,l=a),b.displayedCollection=this.pipe(b.dataCollection)},this.search=function(a,c){if(c&&-1!==b.columns.indexOf(c))m.$="",c.filterPredicate=a;else{for(var d=0,e=b.columns.length;e>d;d++)b.columns[d].filterPredicate="";m.$=a}for(var d=0,e=b.columns.length;e>d;d++)m[b.columns[d].map]=b.columns[d].filterPredicate;b.displayedCollection=this.pipe(b.dataCollection)},this.pipe=function(c){var e,g=(b.filterAlgorithm&&a.isFunction(b.filterAlgorithm))===!0?b.filterAlgorithm:d("filter");return e=j(f.filter(c,g,m),l),b.numberOfPages=i(e),b.isPaginationEnabled?f.fromTo(e,(b.currentPage-1)*b.itemsByPage,b.itemsByPage):e},this.insertColumn=function(a,d){var e=new c(a);f.insertAt(b.columns,d,e)},this.removeColumn=function(a){f.removeAt(b.columns,a)},this.moveColumn=function(a,c){f.moveAt(b.columns,a,c)},this.toggleSelection=function(a){var c=b.dataCollection.indexOf(a);-1!==c&&k(b.dataCollection,b.selectionMode,c,a.isSelected!==!0)},this.toggleSelectionAll=function(a){var c=0,d=b.displayedCollection.length;if("multiple"===b.selectionMode)for(;d>c;c++)k(b.displayedCollection,b.selectionMode,c,a===!0)},this.removeDataRow=function(a){var c=f.removeAt(b.displayedCollection,a);f.removeAt(b.dataCollection,b.dataCollection.indexOf(c))},this.moveDataRow=function(a,c){f.moveAt(b.displayedCollection,a,c)},this.updateDataRow=function(a,c,d){var f,g=b.displayedCollection.indexOf(a),h=e(c),i=h.assign;-1!==g&&(f=h(b.displayedCollection[g]),f!==d&&(i(b.displayedCollection[g],d),b.$emit("updateDataRow",{item:b.displayedCollection[g]})))}}])}(angular),angular.module("smartTable.templates",["partials/defaultCell.html","partials/defaultHeader.html","partials/editableCell.html","partials/globalSearchCell.html","partials/pagination.html","partials/selectAllCheckbox.html","partials/selectionCheckbox.html","partials/smartTable.html"]),angular.module("partials/defaultCell.html",[]).run(["$templateCache",function(a){a.put("partials/defaultCell.html","{{formatedValue}}")}]),angular.module("partials/defaultHeader.html",[]).run(["$templateCache",function(a){a.put("partials/defaultHeader.html","{{column.label}}")}]),angular.module("partials/editableCell.html",[]).run(["$templateCache",function(a){a.put("partials/editableCell.html",'
\n {{value | format:column.formatFunction:column.formatParameter}}\n\n
\n \n
\n
')}]),angular.module("partials/globalSearchCell.html",[]).run(["$templateCache",function(a){a.put("partials/globalSearchCell.html",'\n')}]),angular.module("partials/pagination.html",[]).run(["$templateCache",function(a){a.put("partials/pagination.html",' ')}]),angular.module("partials/selectAllCheckbox.html",[]).run(["$templateCache",function(a){a.put("partials/selectAllCheckbox.html",'')}]),angular.module("partials/selectionCheckbox.html",[]).run(["$templateCache",function(a){a.put("partials/selectionCheckbox.html",'')}]),angular.module("partials/smartTable.html",[]).run(["$templateCache",function(a){a.put("partials/smartTable.html",'\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n
\n\n\n')}]),function(a){"use strict";a.module("smartTable.templateUrlList",[]).constant("templateUrlList",{smartTable:"partials/smartTable.html",smartTableGlobalSearch:"partials/globalSearchCell.html",editableCell:"partials/editableCell.html",selectionCheckbox:"partials/selectionCheckbox.html",selectAllCheckbox:"partials/selectAllCheckbox.html",defaultHeader:"partials/defaultHeader.html",pagination:"partials/pagination.html"})}(angular),function(a){"use strict";a.module("smartTable.utilities",[]).factory("ArrayUtility",function(){var b=function(a,b){return b>=0&&b=0&&b=0&&b=0&&c0?b.length-1:0),d=Math.max(0,d),e=Math.min(f+d,b.length);for(var h=f;e>h;h++)g.push(b[h]);return g};return{removeAt:b,insertAt:c,moveAt:d,sort:e,filter:f,fromTo:g}})}(angular),function(a){a.module("ui.bootstrap.pagination.smartTable",["smartTable.templateUrlList"]).constant("paginationConfig",{boundaryLinks:!1,directionLinks:!0,firstText:"First",previousText:"<",nextText:">",lastText:"Last"}).directive("paginationSmartTable",["paginationConfig","templateUrlList",function(b,c){return{restrict:"EA",require:"^smartTable",scope:{numPages:"=",currentPage:"=",maxSize:"="},templateUrl:c.pagination,replace:!0,link:function(c,d,e,f){function g(a,b,c,d){return{number:a,text:b,active:c,disabled:d}}var h=a.isDefined(e.boundaryLinks)?c.$eval(e.boundaryLinks):b.boundaryLinks,i=a.isDefined(e.directionLinks)?c.$eval(e.directionLinks):b.directionLinks,j=a.isDefined(e.firstText)?e.firstText:b.firstText,k=a.isDefined(e.previousText)?e.previousText:b.previousText,l=a.isDefined(e.nextText)?e.nextText:b.nextText,m=a.isDefined(e.lastText)?e.lastText:b.lastText;c.$watch("numPages + currentPage + maxSize",function(){c.pages=[];var a=1,b=c.numPages;c.maxSize&&c.maxSizec.numPages&&(b=c.numPages,a=b-c.maxSize+1));for(var d=a;b>=d;d++){var e=g(d,d,c.isActive(d),!1);c.pages.push(e)}if(i){var f=g(c.currentPage-1,k,!1,c.noPrevious());c.pages.unshift(f);var n=g(c.currentPage+1,l,!1,c.noNext());c.pages.push(n)}if(h){var o=g(1,j,!1,c.noPrevious());c.pages.unshift(o);var p=g(c.numPages,m,!1,c.noNext());c.pages.push(p)}c.currentPage>c.numPages&&c.selectPage(c.numPages)}),c.noPrevious=function(){return 1===c.currentPage},c.noNext=function(){return c.currentPage===c.numPages},c.isActive=function(a){return c.currentPage===a},c.selectPage=function(a){!c.isActive(a)&&a>0&&a<=c.numPages&&(c.currentPage=a,f.changePage({page:a}))}}}}])}(angular); \ No newline at end of file +!function(a,b){"use strict";function c(a,c){function d(a){return this instanceof d?(b.extend(this,a),void 0):new d(a)}this.setDefaultOption=function(a){b.extend(d.prototype,a)},a.headerTemplateUrl=c.defaultHeader,this.setDefaultOption(a),this.$get=function(){return d}}var d=b.module("smartTable.column",["smartTable.templateUrlList"]).constant("DefaultColumnConfiguration",{isSortable:!0,isEditable:!1,type:"text",headerTemplateUrl:"",map:"",label:"",sortPredicate:"",formatFunction:"",formatParameter:"",filterPredicate:"",cellTemplateUrl:"",headerClass:"",cellClass:""});c.$inject=["DefaultColumnConfiguration","templateUrlList"],d.provider("Column",c),a.ColumnProvider=c}(window,angular),function(a){"use strict";a.module("smartTable.directives",["smartTable.templateUrlList","smartTable.templates"]).directive("smartTable",["templateUrlList","DefaultTableConfiguration",function(b,c){return{restrict:"EA",scope:{columnCollection:"=columns",dataCollection:"=rows",config:"="},replace:"true",templateUrl:b.smartTable,controller:"TableCtrl",link:function(d,e,f,g){var h;d.$watch("config",function(e){var f=a.extend({},c,e),h=void 0!==d.columns?d.columns.length:0;if(g.setGlobalConfig(f),"multiple"!==f.selectionMode||f.displaySelectionCheckbox!==!0)for(var i=h-1;i>=0;i--)d.columns[i].isSelectionColumn===!0&&g.removeColumn(i);else g.insertColumn({cellTemplateUrl:b.selectionCheckbox,headerTemplateUrl:b.selectAllCheckbox,isSelectionColumn:!0},0)},!0),d.$watch("columnCollection",function(){if(d.columnCollection)for(var b=0,c=d.columnCollection.length;c>b;b++)g.insertColumn(d.columnCollection[b]);else d.dataCollection&&d.dataCollection.length>0&&(h=d.dataCollection[0],a.forEach(h,function(a,b){"$"!=b[0]&&g.insertColumn({label:b,map:b})}))},!0),d.$watch("dataCollection.length",function(a,b){a!==b&&g.sortBy()})}}}]).directive("smartTableDataRow",function(){return{require:"^smartTable",restrict:"C",link:function(a,b,c,d){b.bind("click",function(){a.$apply(function(){d.toggleSelection(a.dataRow)})})}}}).directive("smartTableHeaderCell",function(){return{restrict:"C",require:"^smartTable",link:function(a,b,c,d){b.bind("click",function(){a.$apply(function(){d.sortBy(a.column)})})}}}).directive("smartTableSelectAll",function(){return{restrict:"C",require:"^smartTable",link:function(a,b,c,d){b.bind("click",function(){d.toggleSelectionAll(b[0].checked===!0)})}}}).directive("stopEvent",function(){return{restrict:"A",link:function(a,b,c){b.bind(c.stopEvent,function(a){a.stopPropagation()})}}}).directive("smartTableGlobalSearch",["templateUrlList",function(a){return{restrict:"C",require:"^smartTable",scope:{columnSpan:"@"},templateUrl:a.smartTableGlobalSearch,replace:!1,link:function(a,b,c,d){a.searchValue="",a.$watch("searchValue",function(a){d.search(a)})}}}]).directive("smartTableDataCell",["$filter","$http","$templateCache","$compile","$parse",function(a,b,c,d,e){return{restrict:"C",link:function(f,g){function h(){j.isEditable?(g.html('
'),d(g.contents())(f)):g.text(f.formatedValue)}var i,j=f.column,k=!j.isEditable,l=f.dataRow,m=a("format"),n=e(j.map);f.$watch("dataRow",function(){f.formatedValue=m(n(l),j.formatFunction,j.formatParameter),k===!0&&g.text(f.formatedValue)},!0),f.$watch("column.cellTemplateUrl",function(a){a?b.get(a,{cache:c}).success(function(a){k=!1,i=f.$new(),g.html(a),d(g.contents())(i)}).error(h):h()})}}}]).directive("inputType",function(){return{restrict:"A",priority:1,link:function(a,b,c){var d=a.$eval(c.type);c.$set("type",d)}}}).directive("editableCell",["templateUrlList","$parse",function(b,c){return{restrict:"EA",require:"^smartTable",templateUrl:b.editableCell,scope:{row:"=",column:"=",type:"="},replace:!0,link:function(b,d,e,f){var g=a.element(d.children()[1]),h=a.element(g.children()[0]),i=c(b.column.map);b.isEditMode=!1,b.$watch("row",function(){b.value=i(b.row)},!0),b.submit=function(){b.myForm.$valid===!0&&(f.updateDataRow(b.row,b.column.map,b.value),f.sortBy()),b.toggleEditMode()},b.toggleEditMode=function(){b.value=i(b.row),b.isEditMode=b.isEditMode!==!0},b.$watch("isEditMode",function(a){a===!0&&(h[0].select(),h[0].focus())}),h.bind("blur",function(){b.$apply(function(){b.submit()})})}}}])}(angular),function(a){"use strict";a.module("smartTable.filters",[]).constant("DefaultFilters",["currency","date","json","lowercase","number","uppercase"]).filter("format",["$filter","DefaultFilters",function(b,c){return function(d,e,f){var g;return g=e&&a.isFunction(e)?e:-1!==c.indexOf(e)?b(e):function(a){return a},g(d,f)}}])}(angular),function(a){"use strict";a.module("smartTable.table",["smartTable.column","smartTable.utilities","smartTable.directives","smartTable.filters","ui.bootstrap.pagination.smartTable"]).constant("DefaultTableConfiguration",{selectionMode:"none",isGlobalSearchActivated:!1,displaySelectionCheckbox:!1,isPaginationEnabled:!0,itemsByPage:10,maxSize:5,sortAlgorithm:"",filterAlgorithm:""}).controller("TableCtrl",["$scope","Column","$filter","$parse","ArrayUtility","DefaultTableConfiguration",function(b,c,d,e,f,g){function h(){var a,c=b.displayedCollection.length;for(a=0;c>a;a++)if(b.displayedCollection[a].isSelected!==!0)return!1;return!0}function i(c){return a.isArray(c)?0===c.length||b.itemsByPage<1?1:Math.ceil(c.length/b.itemsByPage):1}function j(c,e){var g=(b.sortAlgorithm&&a.isFunction(b.sortAlgorithm))===!0?b.sortAlgorithm:d("orderBy");return e?f.sort(c,g,e.sortPredicate,e.reverse):c}function k(c,d,e,f){var g,i;if(a.isArray(c)&&("multiple"===d||"single"===d)&&e>=0&&ej;j++)i=c[j].isSelected,c[j].isSelected=!1,i===!0&&b.$emit("selectionChange",{item:c[j]});g.isSelected=f,b.holder.isAllSelected=h(),b.$emit("selectionChange",{item:g})}}b.columns=[],b.dataCollection=b.dataCollection||[],b.displayedCollection=[],b.numberOfPages=i(b.dataCollection),b.currentPage=1,b.holder={isAllSelected:!1};var l,m={};this.setGlobalConfig=function(c){a.extend(b,g,c)},this.changePage=function(c){var d=b.currentPage;a.isNumber(c.page)&&(b.currentPage=c.page,b.displayedCollection=this.pipe(b.dataCollection),b.holder.isAllSelected=h(),b.$emit("changePage",{oldValue:d,newValue:b.currentPage}))},this.sortBy=function(a){var c=b.columns.indexOf(a);-1!==c&&a.isSortable===!0&&(l&&l!==a&&(l.reverse="none"),a.sortPredicate=a.sortPredicate||a.map,a.reverse=a.reverse!==!0,l=a),b.displayedCollection=this.pipe(b.dataCollection)},this.search=function(a,c){if(c&&-1!==b.columns.indexOf(c))m.$="",c.filterPredicate=a;else{for(var d=0,e=b.columns.length;e>d;d++)b.columns[d].filterPredicate="";m.$=a}for(var d=0,e=b.columns.length;e>d;d++)m[b.columns[d].map]=b.columns[d].filterPredicate;b.displayedCollection=this.pipe(b.dataCollection)},this.pipe=function(c){var e,g=(b.filterAlgorithm&&a.isFunction(b.filterAlgorithm))===!0?b.filterAlgorithm:d("filter");return e=j(f.filter(c,g,m),l),b.numberOfPages=i(e),b.isPaginationEnabled?f.fromTo(e,(b.currentPage-1)*b.itemsByPage,b.itemsByPage):e},this.insertColumn=function(a,d){var e=new c(a);f.insertAt(b.columns,d,e)},this.removeColumn=function(a){f.removeAt(b.columns,a)},this.moveColumn=function(a,c){f.moveAt(b.columns,a,c)},this.toggleSelection=function(a){var c=b.dataCollection.indexOf(a);-1!==c&&k(b.dataCollection,b.selectionMode,c,a.isSelected!==!0)},this.toggleSelectionAll=function(a){var c=0,d=b.displayedCollection.length;if("multiple"===b.selectionMode)for(;d>c;c++)k(b.displayedCollection,b.selectionMode,c,a===!0)},this.removeDataRow=function(a){var c=f.removeAt(b.displayedCollection,a);f.removeAt(b.dataCollection,b.dataCollection.indexOf(c))},this.moveDataRow=function(a,c){f.moveAt(b.displayedCollection,a,c)},this.updateDataRow=function(a,c,d){var f,g=b.displayedCollection.indexOf(a),h=e(c),i=h.assign;-1!==g&&(f=h(b.displayedCollection[g]),f!==d&&(i(b.displayedCollection[g],d),b.$emit("updateDataRow",{item:b.displayedCollection[g]})))}}])}(angular),angular.module("smartTable.templates",["partials/defaultCell.html","partials/defaultHeader.html","partials/editableCell.html","partials/globalSearchCell.html","partials/pagination.html","partials/selectAllCheckbox.html","partials/selectionCheckbox.html","partials/smartTable.html"]),angular.module("partials/defaultCell.html",[]).run(["$templateCache",function(a){a.put("partials/defaultCell.html","{{formatedValue}}")}]),angular.module("partials/defaultHeader.html",[]).run(["$templateCache",function(a){a.put("partials/defaultHeader.html","{{column.label}}")}]),angular.module("partials/editableCell.html",[]).run(["$templateCache",function(a){a.put("partials/editableCell.html",'
\n {{value | format:column.formatFunction:column.formatParameter}}\n\n
\n \n
\n
')}]),angular.module("partials/globalSearchCell.html",[]).run(["$templateCache",function(a){a.put("partials/globalSearchCell.html",'\n')}]),angular.module("partials/pagination.html",[]).run(["$templateCache",function(a){a.put("partials/pagination.html",' ')}]),angular.module("partials/selectAllCheckbox.html",[]).run(["$templateCache",function(a){a.put("partials/selectAllCheckbox.html",'')}]),angular.module("partials/selectionCheckbox.html",[]).run(["$templateCache",function(a){a.put("partials/selectionCheckbox.html",'')}]),angular.module("partials/smartTable.html",[]).run(["$templateCache",function(a){a.put("partials/smartTable.html",'\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\n
\n\n\n')}]),function(a){"use strict";a.module("smartTable.templateUrlList",[]).constant("templateUrlList",{smartTable:"partials/smartTable.html",smartTableGlobalSearch:"partials/globalSearchCell.html",editableCell:"partials/editableCell.html",selectionCheckbox:"partials/selectionCheckbox.html",selectAllCheckbox:"partials/selectAllCheckbox.html",defaultHeader:"partials/defaultHeader.html",pagination:"partials/pagination.html"})}(angular),function(a){"use strict";a.module("smartTable.utilities",[]).factory("ArrayUtility",function(){var b=function(a,b){return b>=0&&b=0&&b=0&&b=0&&c0?b.length-1:0),d=Math.max(0,d),e=Math.min(f+d,b.length);for(var h=f;e>h;h++)g.push(b[h]);return g};return{removeAt:b,insertAt:c,moveAt:d,sort:e,filter:f,fromTo:g}})}(angular),function(a){a.module("ui.bootstrap.pagination.smartTable",["smartTable.templateUrlList"]).constant("paginationConfig",{boundaryLinks:!1,directionLinks:!0,firstText:"First",previousText:"<",nextText:">",lastText:"Last"}).directive("paginationSmartTable",["paginationConfig","templateUrlList",function(b,c){return{restrict:"EA",require:"^smartTable",scope:{numPages:"=",currentPage:"=",maxSize:"="},templateUrl:c.pagination,replace:!0,link:function(c,d,e,f){function g(a,b,c,d){return{number:a,text:b,active:c,disabled:d}}var h=a.isDefined(e.boundaryLinks)?c.$eval(e.boundaryLinks):b.boundaryLinks,i=a.isDefined(e.directionLinks)?c.$eval(e.directionLinks):b.directionLinks,j=a.isDefined(e.firstText)?e.firstText:b.firstText,k=a.isDefined(e.previousText)?e.previousText:b.previousText,l=a.isDefined(e.nextText)?e.nextText:b.nextText,m=a.isDefined(e.lastText)?e.lastText:b.lastText;c.$watch("numPages + currentPage + maxSize",function(){c.pages=[];var a=1,b=c.numPages;c.maxSize&&c.maxSizec.numPages&&(b=c.numPages,a=b-c.maxSize+1));for(var d=a;b>=d;d++){var e=g(d,d,c.isActive(d),!1);c.pages.push(e)}if(i){var f=g(c.currentPage-1,k,!1,c.noPrevious());c.pages.unshift(f);var n=g(c.currentPage+1,l,!1,c.noNext());c.pages.push(n)}if(h){var o=g(1,j,!1,c.noPrevious());c.pages.unshift(o);var p=g(c.numPages,m,!1,c.noNext());c.pages.push(p)}c.currentPage>c.numPages&&c.selectPage(c.numPages)}),c.noPrevious=function(){return 1===c.currentPage},c.noNext=function(){return c.currentPage===c.numPages},c.isActive=function(a){return c.currentPage===a},c.selectPage=function(a){!c.isActive(a)&&a>0&&a<=c.numPages&&(c.currentPage=a,f.changePage({page:a}))}}}}])}(angular); \ No newline at end of file diff --git a/example-app/index.html b/example-app/index.html index 9e30b147..71840dfa 100644 --- a/example-app/index.html +++ b/example-app/index.html @@ -7,6 +7,7 @@ + diff --git a/example-app/js/Smart-Table.debug.js b/example-app/js/Smart-Table.debug.js index 91d34422..ffb60706 100644 --- a/example-app/js/Smart-Table.debug.js +++ b/example-app/js/Smart-Table.debug.js @@ -199,16 +199,21 @@ link: function (scope, element) { var column = scope.column, + isSimpleCell = !column.isEditable, row = scope.dataRow, format = filter('format'), getter = parse(column.map), childScope; //can be useful for child directives - scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter); + scope.$watch('dataRow', function (value) { + scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter); + if (isSimpleCell === true) { + element.text(scope.formatedValue); + } + }, true); function defaultContent() { - //clear content if (column.isEditable) { element.html('
'); compile(element.contents())(scope); @@ -223,6 +228,8 @@ //we have to load the template (and cache it) : a kind of ngInclude http.get(value, {cache: templateCache}).success(function (response) { + isSimpleCell = false; + //create a scope childScope = scope.$new(); //compile the element with its new content and new scope @@ -268,7 +275,9 @@ //init values scope.isEditMode = false; - scope.value = getter(scope.row); + scope.$watch('row', function () { + scope.value = getter(scope.row); + }, true); scope.submit = function () { @@ -285,8 +294,8 @@ scope.isEditMode = scope.isEditMode !== true; }; - scope.$watch('isEditMode', function (newValue, oldValue) { - if (newValue) { + scope.$watch('isEditMode', function (newValue) { + if (newValue === true) { input[0].select(); input[0].focus(); } diff --git a/example-app/js/app.js b/example-app/js/app.js index 85d7bb99..a1d54f20 100644 --- a/example-app/js/app.js +++ b/example-app/js/app.js @@ -1,38 +1,43 @@ 'use strict'; // Declare app level module which depends on filters, and services var app = angular.module('myApp', ['smartTable.table']). - controller('mainCtrl', ['$scope', function (scope) { + controller('mainCtrl', ['$scope', function (scope) { - scope.$on('updateDataRow', function (event, args) { - alert(JSON.stringify(args)); - }); - scope.rowCollection = [ - {id: 0, firstName: 'Laurent', lastName: 'Renard', birthDate: new Date('1987-05-21'), balance: 102, email: 'laurent34azerty@gmail.com', nested: {value: 2323}}, - {id: 1, firstName: 'Blandine', lastName: 'Faivre', birthDate: new Date('1987-04-25'), balance: -2323.22, email: 'laurent34azerty@gmail.com', nested: {value: 123}}, - {id: 2, firstName: 'Francoise', lastName: 'Frere', birthDate: new Date('1955-08-27'), balance: 42343, email: 'laurent34azerty@gmail.com', nested: {value: 565}} - ]; + var + nameAsset = ['Pierre', 'Pol', 'Jacques', 'Laurent', 'Nicolas'], + generateRandomItem = function (id) { + var + age = Math.floor(Math.random() * 100), + balance = Math.random() * 10000, + name = nameAsset[Math.floor(Math.random() * 5)], + email = name + balance + '@' + name + '.com'; - for (var i = 0; i < scope.rowCollection.length; i++) { - var obj = scope.rowCollection[i]; - obj.id = i; - } + return { + id: id, + name: name, + email: email, + age: age, + balance: balance + }; + }; - scope.columnCollection = [ - {label: 'id', map: 'id', isEditable: true}, - {label: 'FirsName', map: 'firstName'}, - {label: 'LastName', map: 'lastName', isSortable: false}, - {label: 'birth date', map: 'birthDate', formatFunction: 'date', type: 'date'}, - {label: 'balance', map: 'balance', isEditable: true, type: 'number', formatFunction: 'currency', formatParameter: '$'}, - {label: 'email', map: 'email', type: 'email', isEditable: true}, - {label: 'nested', map: 'nested.value', formatFunction: 'currency', formatParameter: '$', type: 'number', isEditable: true} - ]; + scope.rowCollection = []; - scope.globalConfig = { - isPaginationEnabled: true, - isGlobalSearchActivated: true, - itemsByPage: 10, - selectionMode: 'single' - }; + for (var i = 0; i < 400; i++) { + scope.rowCollection.push(generateRandomItem(i)); + } + + scope.columnCollection = [ + {label: 'id', map: 'id'}, + {label: 'Name', map: 'name'}, + {label: 'Age', map:'age'}, + {label: 'Balance', map: 'balance', isEditable: true, type: 'number', formatFunction: 'currency', formatParameter: '$'}, + {label: 'Email', map: 'email', type: 'email', isEditable: true} + ]; - }]) - ; \ No newline at end of file + scope.globalConfig = { + isPaginationEnabled: true, + isGlobalSearchActivated: true, + itemsByPage: 200 + }; + }]); \ No newline at end of file diff --git a/package.json b/package.json index 6eb72777..e45238ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "Smart-Table", - "version": "0.1.3", + "version": "0.1.4", "description": "a table/grid for AngularJS", "main": "smart-table-module", "directories": { diff --git a/smart-table-module/js/Directives.js b/smart-table-module/js/Directives.js index e1f0b28c..1c66cd98 100644 --- a/smart-table-module/js/Directives.js +++ b/smart-table-module/js/Directives.js @@ -146,16 +146,21 @@ link: function (scope, element) { var column = scope.column, + isSimpleCell = !column.isEditable, row = scope.dataRow, format = filter('format'), getter = parse(column.map), childScope; //can be useful for child directives - scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter); + scope.$watch('dataRow', function (value) { + scope.formatedValue = format(getter(row), column.formatFunction, column.formatParameter); + if (isSimpleCell === true) { + element.text(scope.formatedValue); + } + }, true); function defaultContent() { - //clear content if (column.isEditable) { element.html('
'); compile(element.contents())(scope); @@ -170,6 +175,8 @@ //we have to load the template (and cache it) : a kind of ngInclude http.get(value, {cache: templateCache}).success(function (response) { + isSimpleCell = false; + //create a scope childScope = scope.$new(); //compile the element with its new content and new scope @@ -215,7 +222,9 @@ //init values scope.isEditMode = false; - scope.value = getter(scope.row); + scope.$watch('row', function () { + scope.value = getter(scope.row); + }, true); scope.submit = function () { @@ -232,8 +241,8 @@ scope.isEditMode = scope.isEditMode !== true; }; - scope.$watch('isEditMode', function (newValue, oldValue) { - if (newValue) { + scope.$watch('isEditMode', function (newValue) { + if (newValue === true) { input[0].select(); input[0].focus(); }