EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: haltman on April 25, 2012, 01:55:26 AM



Title: create dynamically datagrig
Post by: haltman on April 25, 2012, 01:55:26 AM
Does exist a way to create dynamically a datagrid? I mean to post columns and rows with jquery ajax functions like $.post or $.getJSON

thanks in advance
ciao
h.


Title: Re: create dynamically datagrig
Post by: Ellipsis on December 04, 2012, 12:18:39 PM
I would like the Same feature, it would be enough to be able to create the column of a datagrid with a Json file.

Seems to be pretty straightforward, coverting json to a multidimension array, but an easyui method would be the best way i suppose.


Title: Re: create dynamically datagrig
Post by: stworthy on December 05, 2012, 12:40:50 AM
The 'loadFilter' function will be called before data loaded into datagrid. Here is the simple load filter functionality to dynamic create columns.

Code:
<table class="easyui-datagrid" data-options="loadFilter: loadFilter">
</table>

function loadFilter(data){
if (!this.columns && data.columns){
this.columns = data.columns;
var opts = $(this).datagrid('options');
var url = opts.url;
$(this).datagrid({columns:data.columns,url:null});
setTimeout(function(){
opts.url = url;
},0);
}
return data;
}

The json data to be loaded must contains 'columns' definition, such as:

Code:
{"total":1,"rows":[{...}],"columns":[[...]]}


Title: Re: create dynamically datagrig
Post by: Ellipsis on December 05, 2012, 09:09:19 AM
Ok, got it working.

I have chosen to build a separate method for retrieving columndata and use underscore.js to translate the json to an array [[]] that holds the objects. (many possible columns needed, so I retrieve them all and cache them)

The loadfilter is usable in a whole lot of other scenario's, so I'll try that soon. 
Thank you.

ps. I posted a question about a loadMsg hanging when data retrieve fails, if you have some tips....