ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

Need to show concatenated fields in a ng-grid column. Not working.

Open getjewelfast115 opened this issue 13 years ago • 28 comments

Hi, I am new to Angular JS, I am trying to display items in a ng-grid. In a particular column, I have to concatenate two different model fields with a "-" sign. Can somebody please help me to have a solution for this?

I tried like this, field : 'rollnumber' + '-' + 'name'; displayName : 'NameRollNumber'

But it is not working :( .

Thanks & Regards, Jewel Jose

getjewelfast115 avatar Sep 20 '13 20:09 getjewelfast115

Add a function to your row entity

function getRollAndName(){ return this.rollnumber + '-' + this.name; }

field: getRollAndName()

swalters avatar Sep 20 '13 20:09 swalters

Seems it is not working. I created a function and called that function from field, but it is not working. My grid is bind to an object and I am passing that object to function as function parameter and trying to return concatenate values in that object and return it as a string. field : getRollAndName('student'), displayName : 'NameRollNumber'

and function definition is like this, function getRollAndName(student) { return student.rollnumber + '-' + student.name; }

But it is not working .... :(

Can somebody advice...

getjewelfast115 avatar Sep 20 '13 21:09 getjewelfast115

see this plunk http://plnkr.co/edit/HeyAdn?p=preview

swalters avatar Sep 20 '13 21:09 swalters

Hi Swalters,

Thanks very much for the help... It is working now. I made the changes in my code as mentioned by you, and it start working, Thanks Swalters.

getjewelfast115 avatar Sep 23 '13 20:09 getjewelfast115

Hi Shane,

Is there a way to get that plunk working with asynchronous data? Like $scope.myData = InterestingData.query() // query being used from $resource

StevenHarlow avatar Apr 11 '14 00:04 StevenHarlow

@StevenHarlow yep: http://plnkr.co/edit/Tbbx5M?p=preview

c0bra avatar Apr 14 '14 17:04 c0bra

@swalters @c0bra thank you both

nuwan-dammika avatar Sep 17 '14 05:09 nuwan-dammika

How can I make this work in the 3.0 version?. I tried the provided method but it didn't worked.

rcanepa avatar Dec 21 '14 22:12 rcanepa

I think the comment above is reasonably clear - what about it is not working?

You need to add a function to each row in your data array, and then set the field to use that function. Did you check the plunker above: http://plnkr.co/edit/HeyAdn?p=preview

PaulL1 avatar Dec 21 '14 22:12 PaulL1

Yeah Paul, you are right. The example works perfectly fine. I was committing a mistake.

Thank you for your time and quick response!

rcanepa avatar Dec 21 '14 22:12 rcanepa

Is there any way to call a scope level function rather than an object level function?
I don't particularly want to add the function to the object if I can help it. I've tried just defining a function within the field statement but that doesn't seem to work.

jklaus avatar Sep 24 '15 14:09 jklaus

The way I ended of doing scope level field manipulation was using cell templates. Quick google search brings up, http://brianhann.com/6-ways-to-take-control-of-how-your-ui-grid-data-is-displayed/ which has a decent enough explanation of this works.

robsilvia avatar Sep 24 '15 15:09 robsilvia

Yeah I came across that.. I was looking for something more similar to the way you set mData in the (now legacy) DataTables.

For my specific use case I opted to go with a filter.

Initially I thought at some point I'd need to do something similar to executing a function with mData but after a bit more thought I don't see any real instance where implementing a filter or ,as you mentioned, cell template wouldn't get the job done.

I'm in the midst of upgrading an older app to newer versions of things and was weighing DataTables vs ng-Grid (ui-Grid). Obviously ui-Grid has come a long way.

jklaus avatar Sep 24 '15 19:09 jklaus

@c0bra Thank you very much. It works!

Noel-bk avatar Oct 16 '15 10:10 Noel-bk

I think that the easiest way is to use a simple cell template that has a span {name: "NameRollNumber", cellTemplate: "<span>{{row.entity.rollnumber}} - {{row.entity.name}}</span>"}

I think that this does not break the way angular works. Declaring a function just adds unnecessary extra work and dependencies. I had to surround it with a span so that angular understands that it's an html template and not an html file

jjamid avatar Oct 18 '15 07:10 jjamid

@cesee Right. I think It's a better way you want to display a simple template. BTW, cellTemplate: "{{ fieldname }}" will not work.. cellTemplate: "{{ row.entity.fieldname }}" will work. :)

Noel-bk avatar Oct 22 '15 09:10 Noel-bk

@UnnamedSong Thanks for the correction, I updated the answer.

jjamid avatar Oct 22 '15 17:10 jjamid

You're welcome. My pleasure. xD

On Fri, Oct 23, 2015 at 2:43 AM, Cesar Maroun [email protected] wrote:

@UnnamedSong https://github.com/UnnamedSong Thanks for the correction, I updated the answer.

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ui-grid/issues/723#issuecomment-150302334.

Noel-bk avatar Oct 23 '15 14:10 Noel-bk

Thanks a lot @cesee It works :)

If someone needs to align it within the cell, use as shown below.Cheers !

cellTemplate: '<div class="ui-grid-cell-contents">{{row.entity.rollnumber}} - {{row.entity.name}}</div>',

Sampath-Lokuge avatar Dec 16 '15 13:12 Sampath-Lokuge

You could also do this by using a function for your field in the column def - this is covered in the binding tutorial.

Sent from my iPhone

On 17 Dec 2015, at 2:59 AM, Sampath Lokuge <[email protected]mailto:[email protected]> wrote:

Thanks a lot @ceseehttps://github.com/cesee It works :)

If someone needs to align it within the cell, use as shown below.Cheers !

cellTemplate: '

{{row.entity.rollnumber}} - {{row.entity.name}}
',

Reply to this email directly or view it on GitHubhttps://github.com/angular-ui/ui-grid/issues/723#issuecomment-165112809.

PaulL1 avatar Dec 16 '15 17:12 PaulL1