EasyUI Forum
May 16, 2022, 08:21:54 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: [SOLVED]Datagrid dynamic columns from json with formatter  (Read 101 times)
jega
Full Member
***
Posts: 151


View Profile
« on: May 10, 2022, 03:34:57 PM »

Hi.

Can't find a solution for this.

Having some column data with a formatter. The formatter is not always in the same column
[{
   "ID": 1,
   "field": "col1",
   "formatter": "formatNumber"
}, {
   "ID": 2,
   "field": "col2"
}, {
   "ID": 3,
   "field": "col3"
}]


Putting the column data in the grid

var dg = $('#dgList').datagrid({
   columns:[data]
});



Then i have this function

    function formatNumber(value,row){
        if (value < 0){
            return 'background-color:#ffee00;color:red;';
        }
    }


But i get this error:

TypeError: col.formatter is not a function

Any help

Regards Jesper
« Last Edit: May 12, 2022, 01:05:11 AM by jega » Logged
jarry
Administrator
Hero Member
*****
Posts: 2079


View Profile Email
« Reply #1 on: May 11, 2022, 06:19:42 AM »

Please call this code instead.

Code:
[{
   "ID": 1,
   "field": "col1",
   "formatter": function(value,row){
    return value + 'formatted';
   }
}, {
   "ID": 2,
   "field": "col2"
}, {
   "ID": 3,
   "field": "col3"
}]
Logged
jega
Full Member
***
Posts: 151


View Profile
« Reply #2 on: May 11, 2022, 01:23:14 PM »

Hi Jarry.

I already have tried this, but it's not a valid json string. Try put it in jsonlint.com


I do an ajax request to my DB and create the json response like this.


jsonData = jsonData & chr(34) & "field" & chr(34) & ":" & chr(34) & rsData("COLUMN_NAME") & chr(34) & ","
jsonData = jsonData & chr(34) & "title" & chr(34) & ":" & chr(34) & rsData("COLUMN_TITLE") & chr(34) & ","
jsonData = jsonData & chr(34) & "sortable" & chr(34) & ":" & chr(34) & "true" & chr(34) & ","

and then, how to do the rest

jsonData = jsonData & chr(34) & "formatter" & chr(34) & ":" & Huh?


Jesper
Logged
jarry
Administrator
Hero Member
*****
Posts: 2079


View Profile Email
« Reply #3 on: May 11, 2022, 06:58:03 PM »

Convert the 'formatNumber' string to a function before using it.
Code:
var data = [{
   "ID": 1,
   "field": "itemid",
   "formatter": "formatNumber"
}, {
   "ID": 2,
   "field": "col2"
}, {
   "ID": 3,
   "field": "col3"
}];
data.forEach(col => {
  if (col.formatter){
    col.formatter = eval(col.formatter)
  }
})
var dg = $('#dgList').datagrid({
   columns:[data]
});
Logged
jega
Full Member
***
Posts: 151


View Profile
« Reply #4 on: May 12, 2022, 01:04:29 AM »

Hi Jarry

Thanks.

Now it works.
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!