I need to add a different button to rows if the row group has two or more rows ?

I need to add a different button to rows if the row group has two or more rows ?

usbherathusbherath Posts: 10Questions: 4Answers: 0

I need to add buttons to all rows and add a different button if the row group has two or more rows. How can I do it?

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    You could use columns.render for each row, and rowGroup.startRender or rowGroup.endRender for the group.

    If that doesn't answer the question, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • usbherathusbherath Posts: 10Questions: 4Answers: 0

    Here is my code

    $(document).ready(function() {
    
    $('#list').DataTable({
        "lengthMenu": [
            [10, 25, 50, -1],
            [10, 25, 50, 'All'],
        ],
        "data": [
            {
                "Site":       "Tokyo",
                "Date":   "12/10/2020",
                "Score":     4.3,
                "Bid No": 510,
                "Year":     2010,
                "Model":     "Allion",
                "chassis_id":   "4XC-SD65733",
                "FOB":       5421,
            },
            {
                "Site":       "Tokyo",
                "Date":   "12/10/2020",
                "Score":     4.3,
                "Bid No": 510,
                "Year":     2010,
                "Model":     "Allion",
                "chassis_id":   "4XC-SD65733",
                "FOB":       5421,
            },
            {
                "Site":       "Tokyo",
                "Date":   "12/10/2020",
                "Score":     4.3,
                "Bid No": 510,
                "Year":     2010,
                "Model":     "Allion",
                "chassis_id":   "1XC-SD65733",
                "FOB":       5421,
            },
            {
                "Site":       "Kyushu",
                "Date":   "12/10/2020",
                "Score":     4.3,
                "Bid No": 510,
                "Year":     2010,
                "Model":     "Allion",
                "chassis_id":   "1XC-SD65733",
                "FOB":       5421,
            },
            {
                "Site":       "Shikoku",
                "Date":   "12/10/2020",
                "Score":     4.3,
                "Bid No": 510,
                "Year":     2010,
                "Model":     "Allion",
                "chassis_id":   "3XC-SD65733",
                "FOB":       5421,
            }, {
                "Site":       "Shikoku",
                "Date":   "12/10/2020",
                "Score":     6.3,
                "Bid No": 510,
                "Year":     2010,
                "Model":     "Allion",
                "chassis_id":   "3XC-SD65790",
                "FOB":       5421,
            }
    
        ],
    
        fnCreatedRow: function (nRow, aData, iDataIndex) {
            $(nRow).attr('id',aData['chassis_id']);
            $(nRow).attr('class','sub-row');
        },
        rowGroup: {
            dataSrc: 'chassis_id',
            className: 'table-group',
            startRender: function(rows, group, level) {
    
    
                var xcv =  $('<label class="'+(rows.count()>1?'one-row':'more-rows')+'">Chassis ID : '+group +'</label>');
    
                if (rows.count() > 1)
                {
                    $('.table-group').next('.sub-row').addClass("selected");
                }
    
                return xcv;
            },
    
        },
    
        "columns":
            [
                {"data":'Site'},
                {"data":'Date'},
                {"data":'Score'},
                {"data":'Bid No'},
                {"data":'Year'},
                {"data":'Model'},
                {"data":'chassis_id'},
                {"data":'FOB'},
    
                {
                    "mRender": function (data, group, full) {
                        return '<button class="btn btn-danger ripple m-1 xor" type="button">'+ group +'</button>';
                    }
                }
            ],
    
        "initComplete": function (settings, json) {
        }
    
    
    });
    });
    
  • usbherathusbherath Posts: 10Questions: 4Answers: 0

    I have added live datatable here,
    http://live.datatables.net/lamileru/2/

    I need to add a select button if two or more rows in a group.

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Yep, you were close, it just needed that check added: http://live.datatables.net/bixuraqa/1/edit

    Colin

  • usbherathusbherath Posts: 10Questions: 4Answers: 0

    Thanks for your answer, is it possible to add a diffrent button to each row that include two or more rows in row groups instead of remove button?

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    You can add any button that you want, that example is just showing you how to do it. I did that remove button as it was a copy&paste of your existing button.

    Colin

  • usbherathusbherath Posts: 10Questions: 4Answers: 0

    I really need to get output like this image

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    OK, can you update my example to show us what you've got and explain why it isn't working. It's beyond the scope of the support here to code it all for you,

    Colin

This discussion has been closed.