We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add button column to table

Hi,

I'd like to add a column which consists of multiple button. I have a custom ButtonComponent.vue which I would like to render to the first column within the table. The buttons are defined in the ButtonComponent.vue. 
The row data should be passed in to the action methods when one of the button is clicked.

How can I achieve this? 

3 Replies 1 reply marked as answer

SM Shalini Maragathavel Syncfusion Team October 14, 2020 02:04 PM UTC

Hi Dave, 

Greetings from Syncfusion support. 

Based on your query we suspect that you want to bind multiple buttons in a column of the Grid. You can achieve your requirement by using the columnTemplate of the Grid as demonstrated in the below code snippet. 
 
myEmployeeTemplate.vue 
<template> 
  <span> 
    <button v-on:click="handleClick($event)">RowData</button> 
<button v-on:click="handleClick($event)">Btn2</button> 
  </span> 
</template> 
export default { 
  name: "MyEmployeeTemplate", 
 
  data() {}, 
  methods: { 
    handleClick: function (args) { 
      var data = JSON.stringify(this.data); 
      alert(data); 
    }, 
  }, 
}; 

app.vue
 
        <e-column 
          field="CustomerID" 
          headerText="Customer ID" 
          width="220" 
          :template="customerIDHeaderTemplate" 
        ></e-column> 
<script> 
export default Vue.extend({ 
  components: { 
    MyEmployeeTemplate, 
  }, 
  data: () => { 
    return { }, 
      customerIDHeaderTemplate: function (e) { 
        return { 
          template: MyEmployeeTemplate, 
        };}); 
</script> 

In the above sample we get the row data by binding click event to the button. 

Please find the  below sample and documentation for more information. 



Please get back to us if you need further assistance. 

Regards, 
Shalini M. 







DA Dave October 19, 2020 09:58 AM UTC

That worked fine, thank you.

Is it possible to have a listener in the parent component for an $emit action?
For example, if I want to pass the row data to the parent component via $emit instead of props or store.

One more question: Is it possible to pass parameters to the prop section of my column template? For example if I have a button bar component and I want to pass in a list of strings, iterate through them and create a button for each string that's been passed in.




TS Thiyagu Subramani Syncfusion Team October 21, 2020 11:31 AM UTC

Hi Dave, 

Thank for your update. 

Query – 1: Is it possible to have a listener in the parent component for an $emit action? 

Yes, to achieve the above requirement we suggest you to use Vue Global Event Bus concept. By default the EventBus allows us to emit an event from one component and listen for that event in another component. So we can easily communicate with two Vue components by using this EventBus concept.  

Please refer the code example to achieve your requirement and in this below sample we have emitted row details in button click event. 

[App.vue ]  
created() { 
    bus.$on("getdata", (data) => { 
      alert(data); 
    }); 
  }, 

[child component]  

<template> 
  <span> 
    <div 
      class="customer" 
      v-for="customer in data['CustomerID']" 
      v-bind:key="customer"> 
      <button v-on:click="handleClick($event)" class="favorite"> 
        {{ customer }} 
      </button> 
    </div> 
  </span> 
</template> 
<script> 
import { bus } from "/src/main.js"; 
export default { 
 data() {}, 
  methods: { 
    handleClick: function (args) { 
      var data = JSON.stringify(this.data); 
      bus.$emit("getdata", data); 
    }, 
  }, 
}; 
</script>  

For your reference we have prepared the sample with this code and you can find that sample from the below link,  

  
General link for Vue Event Bus concept:  https://alligator.io/vuejs/global-event-bus/  

For more reference please refer below sample. 


Query – 2: I want to pass in a list of strings, iterate through them and create a button for each string that's been passed in. 

Based on your shared information we suspect that you want to render button component in template column and in you mentioned its data as list of strings. By default, the EJ2 Grid column only supports number, string, date, dateTime, Boolean, checkbox type values but it not supports array type value. Refer to the below documentation.   


Using v-for we have rendered button for each string. Please refer to the below code and sample link. 

<template> 
  <span> 
    <div 
      class="customer" 
      v-for="customer in data['CustomerID']" 
      v-bind:key="customer"> 
      <button v-on:click="handleClick($event)" class="favorite"> 
        {{ customer }} 
      </button> 
    </div> 
  </span> 
</template> 

Screenshot:  

 


If you are using columnTemplate feature , we can iterating the array value from dataSource, then bind the value into the columnTemplate .    

But this is used only for the display purpose we cannot perform Grid actions like Filtering, Searching, Grouping, Sorting, etc., to this column(when you are display the custom text through valueAccessor ).  By default in EJ2 Grid the actions (like sorting, grouping ,filtering) will be performed based on its dataSource value.  


Note : The Grid actions like Filtering, Sorting, Searching, Grouping, aggregates, etc., are based on the column field and its dataSource value. We can display our own customized value in the column using its template or valueAccessor  feature . But, the Grid actions are based on the dataSource value not with the customized value. This is the default behavior of EJ2 Grid.    

Please get back to us, if you need any further assistance. 

Regards, 
Thiyagu S 


Marked as answer
Loader.
Up arrow icon