@@ -59,8 +59,10 @@ jQuery( function($) {
5959 nextFocus = prevFocus ;
6060 }
6161 }
62-
63- tr . fadeOut ( 'normal' , function ( ) { tr . remove ( ) ; } ) ;
62+ tr . fadeOut ( 'normal' , function ( ) {
63+ tr . remove ( ) ;
64+ updateTableNavCount ( ) ;
65+ } ) ;
6466
6567 /**
6668 * Removes the term from the parent box and the tag cloud.
@@ -73,7 +75,7 @@ jQuery( function($) {
7375 $ ( 'a.tag-link-' + data . match ( / t a g _ I D = ( \d + ) / ) [ 1 ] ) . remove ( ) ;
7476 nextFocus . trigger ( 'focus' ) ;
7577 message = wp . i18n . __ ( 'The selected tag has been deleted.' ) ;
76-
78+
7779 } else if ( '-1' == r ) {
7880 message = wp . i18n . __ ( 'Sorry, you are not allowed to do that.' ) ;
7981 $ ( '#ajax-response' ) . empty ( ) . append ( '<div class="notice notice-error"><p>' + message + '</p></div>' ) ;
@@ -103,6 +105,53 @@ jQuery( function($) {
103105 tr . find ( ':input, a' ) . prop ( 'disabled' , false ) . removeAttr ( 'tabindex' ) ;
104106 }
105107
108+ /**
109+ * Updates the item count and table navigation after a tag is added or removed.
110+ *
111+ * Tags are added and removed client-side, but the item count, the `.tablenav`
112+ * regions, the search box, and the empty-state row are otherwise only
113+ * reconciled by PHP on a full page reload. This keeps them in sync.
114+ *
115+ * @param {string } [action] Pass 'add' when a tag was added. Any other value,
116+ * including none, is treated as a removal.
117+ *
118+ * @return {void }
119+ */
120+ function updateTableNavCount ( action ) {
121+ var $displayingNum = $ ( '.tablenav-pages .displaying-num' ) ,
122+ currentCount = parseInt ( $displayingNum . first ( ) . text ( ) . replace ( / [ ^ 0 - 9 ] / g, '' ) , 10 ) || 0 ,
123+ itemCount = ( 'add' === action ) ? currentCount + 1 : Math . max ( currentCount - 1 , 0 ) ,
124+ formattedCount = itemCount . toLocaleString ( ) ;
125+
126+ $displayingNum . text (
127+ wp . i18n . sprintf (
128+ /* translators: %s: Number of items. */
129+ wp . i18n . _n ( '%s item' , '%s items' , itemCount ) ,
130+ formattedCount
131+ )
132+ ) ;
133+
134+ if ( itemCount < 1 ) {
135+ // No tags remain: show the empty-state row and hide the navigation.
136+ var $list = $ ( '#the-list' ) ;
137+
138+ if ( ! $list . find ( 'tr.no-items' ) . length ) {
139+ var colspan = $list . closest ( 'table' ) . find ( 'thead > tr' ) . first ( ) . children ( ':not(.hidden)' ) . length ;
140+ $list . append (
141+ '<tr class="no-items"><td class="colspanchange" colspan="' + colspan + '">' +
142+ wp . i18n . __ ( 'No tags found.' ) +
143+ '</td></tr>'
144+ ) ;
145+ }
146+ $ ( '.tablenav > *' ) . hide ( ) ;
147+ $ ( 'p.search-box' ) . hide ( ) ;
148+ } else {
149+ $ ( '#the-list' ) . find ( 'tr.no-items' ) . remove ( ) ;
150+ $ ( '.tablenav > *' ) . show ( ) ;
151+ $ ( 'p.search-box' ) . show ( ) ;
152+ }
153+ }
154+
106155 /**
107156 * Adds a deletion confirmation when removing a tag.
108157 *
@@ -192,6 +241,8 @@ jQuery( function($) {
192241 }
193242
194243 $ ( 'input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="reset"]):visible, textarea:visible' , form ) . val ( '' ) ;
244+
245+ updateTableNavCount ( 'add' ) ;
195246 } ) ;
196247
197248 return false ;
0 commit comments