EasyUI Forum
April 29, 2024, 11:50:43 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: How to change a datgrid column title?  (Read 7780 times)
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« on: August 16, 2016, 11:16:00 AM »

Hi all,

I want to change the title in a datagrid column according context.
I've tried this code, but no luck:

Code:
function changeTitle(dg,field,title) {
  var cols=dg.datagrid('options').columns[0]; // no extra headers in my code
  for (var n=0; n<cols.length;n++) {
    if (cols[n].field!==field) continue; // not found: continue
    cols[n].title=title; // found: set new title
    break; // no need to continue iteration
  }
  dg.datgrid('fitColumns');
}

What am I doing wrong? What's the right way (tm) to change column title in a datagrid
Thanks in advance
Juan Antonio
Logged
wade.zhu
Newbie
*
Posts: 27


View Profile
« Reply #1 on: August 16, 2016, 10:25:06 PM »

try my code  Wink
Code:
function changeColumnTitle(dg, field, title) {
var $panel = dg.datagrid('getPanel');
var $field = $('td[field='+field+']',$panel);
if($field.length){
var $span = $('span', $field).eq(0);
$span.html(title);
}
}
Logged
Juan Antonio Martínez
Jr. Member
**
Posts: 68



View Profile
« Reply #2 on: August 17, 2016, 06:52:05 AM »

Works great, thanks.
Anyway to make sure new title is reflected in 'options', and to add as a dg method, i made a little change:

Code:
$.extend($.fn.datagrid.methods, {
    setTitle: function(jq,data) {  // data: { 'field':fieldname, 'title':newtitle }
        return jq.each(function() {
            // set datagrid 'options' array
            var cols=$(this).datagrid('options').columns[0]; // no extra headers in my code
            for (var n=0; n<cols.length;n++) {
                if (cols[n].field!==data.field) continue; // not found: continue
                cols[n].title=data.title; // found: set new title
                break; // no need to continue iteration
            }
            // change rendered layout
            var panel = $(this).datagrid('getPanel');
            var fld = $('td[field='+data.field+']',panel);
            if(fld.length) $('span', fld).eq(0).html(data.title);
        });
    }

});

Perhaps now not too much complicated elaborate a more generic 'setTitles' method :-)... but out of my scope.
Thanks for the code

Juan Antonio
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!