EasyUI Forum
December 05, 2025, 11:40:52 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Add/edit datagrid column dynamically? [SOLVED?]  (Read 22394 times)
aswzen
Sr. Member
****
Posts: 287


Indonesian

aswzen
View Profile WWW Email
« on: March 10, 2016, 05:18:00 AM »

How to add a column to datagrid in dynamic way?
*without rebuild the datagrid (destroy then recreate)

Thank you in advance
« Last Edit: March 14, 2016, 09:31:55 PM by aswzen » Logged

Regards,
Sigit

- Indonesian jEasyUI Facebook page : https://www.facebook.com/groups/jeasyuiid/
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: March 10, 2016, 08:50:44 AM »

No other simple way to accomplish this. You have to construct the 'columns' property value and re-create the datagrid again.
Logged
ink
Newbie
*
Posts: 8


View Profile
« Reply #2 on: March 13, 2016, 06:12:25 PM »

Let us try to think about the setting of the datagrid, for example:

    $('#dg').datagrid({
        url:'datagrid_data.json',
        columns:[[
            {field:'code',title:'Code',width:100},
            {field:'name',title:'Name',width:100},
            {field:'price',title:'Price',width:100,align:'right'}
        ]]
    });

if you can set the "column" as an array, e.g. heading, just like this:

heading = [[
            {field:'code',title:'Code',width:100},
            {field:'name',title:'Name',width:100},
            {field:'price',title:'Price',width:100,align:'right'}
        ]];

then make it as a function:

function SetGrid(HeadingArray){
    $('#dg').datagrid({
        url:'datagrid_data.json',
        columns: HeadingArray
   });
}

call the function:
SetGrid(heading);

It should work...
Logged
aswzen
Sr. Member
****
Posts: 287


Indonesian

aswzen
View Profile WWW Email
« Reply #3 on: March 13, 2016, 07:50:29 PM »

ink: that is "re-create" the datagrid..

one of my point is not to destroy and re create the datagrid..

but thank you anyway Smiley
Logged

Regards,
Sigit

- Indonesian jEasyUI Facebook page : https://www.facebook.com/groups/jeasyuiid/
ink
Newbie
*
Posts: 8


View Profile
« Reply #4 on: March 14, 2016, 08:03:57 AM »

Yes! We all understand that "re-create" the datagrid is the only way to edit the datagrid column dynamically.
Actually, we can use "getData" to store the data into an array,
modify the heading array and
use "loadData" to reload the data back when "re-create" the datagrid to simulate the datagrid column editing.
 Tongue
Logged
aswzen
Sr. Member
****
Posts: 287


Indonesian

aswzen
View Profile WWW Email
« Reply #5 on: March 14, 2016, 09:29:23 PM »

this is my code if you wonder how i achieve it .. (thank you for you suggestion mr ink)

im using moment.js (http://momentjs.com/)

Code:
function makeColumn(){
   var SD = '20-02-2016'; //LOWEST DATE
   var ED = '25-02-2016'; //HIGHEST DATE

   var a = moment(ED , "DD-MM-YYYY");
var b = moment(SD , "DD-MM-YYYY");
var totalday = a.diff(b, 'days');

var Heading = [];
   Heading = [
           {
           field:'CREW_NAME',
            title:'Crew Name',
            width:240
       }
       ];

for(var i = 0; i <= totalday; i++){
b = moment(SD , "DD-MM-YYYY");

var dateNow = b.add(i, 'days');
   var dateFormatRaw = dateNow.format('DD_MM_YYYY');
   var dateFormat = dateNow.format('D MMM YYYY');

DATES = {
field : 'DATE_'+dateFormatRaw,
title : dateFormat,
width : '155px'
}
Heading.push(DATES);
}

SetGrid(Heading);
   }

function SetGrid(HeadingArray){

    TEMP_DATA = $('#CREW_BOOKING_TABLE').datagrid('getRows');
    $('#CREW_BOOKING_TABLE').datagrid({ url: '', columns: [HeadingArray] });
    $('#CREW_BOOKING_TABLE').datagrid('loadData', TEMP_DATA);
}

« Last Edit: March 14, 2016, 11:31:49 PM by aswzen » Logged

Regards,
Sigit

- Indonesian jEasyUI Facebook page : https://www.facebook.com/groups/jeasyuiid/
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!